0% found this document useful (0 votes)
2 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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

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 java.util.*;

public class Exp1a

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

if(n%2!=0)

System.out.println("Weird");

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

System.out.println("Not Weird");

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

System.out.println("Not Weird");

else

System.out.println("Weird");

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

Output:
Program: Nested ternary operator

import java.util.*;

public class Exp1b2

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter 3 numbers");

int a=sc.nextInt();

int b=sc.nextInt();

int c=sc.nextInt();

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

System.out.println(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 java.util.*;

public class Exp1c

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

int count=0;

if(n<0)

n*=-1;

while(n!=0)

n/=10;

count++;

System.out.println("The number of digits are "+count);

Output:

d. Write a menu driven program using switch case to perform mathematical operations.
Program:
import java.util.Scanner;
public class Exp1d
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers:");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println("Enter the operator (+,-,*,/):");
char op = sc.next().charAt(0);
double result = 0;
switch (op)
{
case '+':
{
result= num1 + num2;
System.out.println(num1+"+"+num2+"="+result);
break;
}
case '-':
{
result= num1 - num2;
System.out.println(num1+"-"+num2+"="+result);
break;
}
case '*':
{
result= num1 * num2;
System.out.println(num1+"*"+num2+"="+result);
break;
}
case '/':
{
result= num1 / num2;
System.out.println(num1+"/"+num2+"="+result);
break;
}
default:
System.out.println("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 java.util.*;
public class Exp1e1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter percentage marks");
int percentage = sc.nextInt();
if(percentage >= 85)
System.out.println("Grade A");
else if(percentage < 85 && percentage >= 70)
System.out.println("Grade B");
else if(percentage < 70 && percentage >= 55)
System.out.println("Grade C");
else if(percentage < 55 && percentage >= 40)
System.out.println("Grade D");
else
System.out.println("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 java.util.*;

public class Exp1f

public static void main(String args[])


{

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

double result=0.0;

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

result+=1.0/(i*i);

System.out.println("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 java.util.*;

public class Exp1g2


{

public static void main(String args[])

Scanner sc=new Scanner(System.in );

System.out.println("Enter the no of lines:");

int n=sc.nextInt();

int k=65;

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

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

System.out.print(" ");

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

System.out.print((char)j);

System.out.println();

k=k+i;

Ouput:

You might also like