0% found this document useful (0 votes)
24 views10 pages

Java Experiment1

The document contains Java programs demonstrating various control statements, including conditionals, loops, and command-line arguments. It includes examples for checking odd/even numbers, finding the largest of three numbers, counting digits, performing mathematical operations using switch-case, grading based on percentage, summing a series, and displaying specific patterns. Each section provides a program with corresponding output expectations.

Uploaded by

reez678912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

Java Experiment1

The document contains Java programs demonstrating various control statements, including conditionals, loops, and command-line arguments. It includes examples for checking odd/even numbers, finding the largest of three numbers, counting digits, performing mathematical operations using switch-case, grading based on percentage, summing a series, and displaying specific patterns. Each section provides a program with corresponding output expectations.

Uploaded by

reez678912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

To implement Java control statements, loops, and command line


arguments
a. Given an integer, n, perform the following conditional actions:
 If n is odd, print Weird
 If n is even and in the inclusive range of 2 to 5, print Not Weird
 If n is even and in the inclusive range of 6 to 20, print Weird
 If n is even and greater than 20, print Not Weird
Program:

import [Link].*;

public class Exp1a

public static void main(String args[])

Scanner sc=new Scanner([Link]);

[Link]("Enter a number");

int n=[Link]();

if(n%2!=0)

[Link]("Weird");

else if(n%2==0&&n>=2&&n<=5)

[Link]("Not Weird");

else if(n%2==0&&n>=6&&n<=20)

[Link]("Not Weird");

else

[Link]("Weird");

Output:
b. WAP to find largest of 3 numbers using nested if else and nested ternary operator.
Program: Nested if else
import [Link].*;
import [Link].*;
public class Exp1b1
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter 3 numbers");
int a=[Link]();
int b=[Link]();
int c=[Link]();
if(a>b)
{
if(b>c)
[Link](a+" is the largest number");
else if(a>c)
[Link](a+" is the largest number");
else
[Link](c+" is the largest number");
}
else
{
if(a>c)
[Link](b+" is the largest number");
else if(b>c)
[Link](b+" is the largest number");
else
[Link](c+" is the largest number");
}
}
}

Output:
Program: Nested ternary operator

import [Link].*;

public class Exp1b2

public static void main(String args[])

Scanner sc=new Scanner([Link]);

[Link]("Enter 3 numbers");

int a=[Link]();

int b=[Link]();

int c=[Link]();

int max=(a>b)?(a>c?a:c):(b>c?b:c);

[Link](max+" is the largest number");

Output:

c. Write a Java program that reads a positive integer from command line and count the number of
digits the number (less than ten billion) has.
Program:

import [Link].*;

public class Exp1c

public static void main(String args[])

Scanner sc=new Scanner([Link]);

[Link]("Enter a number");

int n=[Link]();

int count=0;

if(n<0)

n*=-1;

while(n!=0)

n/=10;

count++;

[Link]("The number of digits are "+count);

Output:

d. Write a menu driven program using switch case to perform mathematical operations.
Program:
import [Link];
public class Exp1d
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the numbers:");
int num1 = [Link]();
int num2 = [Link]();
[Link]("Enter the operator (+,-,*,/):");
char op = [Link]().charAt(0);
double result = 0;
switch (op)
{
case '+':
{
result= num1 + num2;
[Link](num1+"+"+num2+"="+result);
break;
}
case '-':
{
result= num1 - num2;
[Link](num1+"-"+num2+"="+result);
break;
}
case '*':
{
result= num1 * num2;
[Link](num1+"*"+num2+"="+result);
break;
}
case '/':
{
result= num1 / num2;
[Link](num1+"/"+num2+"="+result);
break;
}
default:
[Link]("Wrong Input");
}
}
}

Output:

e. WAP to find grade of student from input marks using if else ladder and switch case.
Program: If else ladder
import [Link].*;
public class Exp1e1
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter percentage marks");
int percentage = [Link]();
if(percentage >= 85)
[Link]("Grade A");
else if(percentage < 85 && percentage >= 70)
[Link]("Grade B");
else if(percentage < 70 && percentage >= 55)
[Link]("Grade C");
else if(percentage < 55 && percentage >= 40)
[Link]("Grade D");
else
[Link]("Failed!");
}
}

Output:

f. WAP to print the sum of following series 1+1/2^2+1/3^2+1/4^2……+1/n^2

Program:

import [Link].*;

public class Exp1f

public static void main(String args[])


{

Scanner sc=new Scanner([Link]);

[Link]("Enter a number");

int n=[Link]();

double result=0.0;

for(int i=1;i<=n;i++)

result+=1.0/(i*i);

[Link]("The result is: "+result);

Output:

g. WAP to display the following patterns:


1
2 1
1 2 3
4 3 2 1
1 2 3 4 5
6 5 4 3 2 1
1 2 3 4 5 6 7
A
CB
FED
JIHG

Program:

import [Link].*;

public class Exp1g2


{

public static void main(String args[])

Scanner sc=new Scanner([Link] );

[Link]("Enter the no of lines:");

int n=[Link]();

int k=65;

for(int i=1;i<=n;i++)

for(int j=n-i;j>=1;j--)

[Link](" ");

for(int j=k+i-1;j>=k;j--)

[Link]((char)j);

[Link]();

k=k+i;

Ouput:

You might also like