freetimestudys@gmail.com          +91-7097633380
  Login Here                        

find the greatest among 3 no’s using nested if.

import java.io.*;
class gtnnestedif
{
public static void main(String args[])throws IOException
{BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a,b,c;
System.out.println("Enter the values");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
if(a>b)
{if(a>c)
{System.out.println("A is greatest");}
else
{
System.out.println("C is greatest");}}
else if (b>c)
{
System.out.println("B is greatest");}
else
{System.out.println("C is greatest");}}}

intput and output

Enter the values:
10 20 34
C is greatest

calculate sum, average and check if pass or fail

import java.io.*;
class mark
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int m1,m2,m3,m4,m5,tot;
float avg;
System.out.println("Enter the mark");
m1=Integer.parseInt(br.readLine());
m2=Integer.parseInt(br.readLine());
m3=Integer.parseInt(br.readLine());
m4=Integer.parseInt(br.readLine());
m5=Integer.parseInt(br.readLine());
tot=m1+m2+m3+m4+m5;avg=tot/5;
System.out.println("Total="+tot);
System.out.println("Average="+avg);
if((m1>=40)&&(m2>=40)&&(m3>=40)&&(m4>+40)&&(m5>=40))
{
System.out.println("Result=Pass");
if (avg>=60){System.out.println("Class=First");}
else if (avg>=50)
{System.out.println("Class=Second");}
else
{System.out.println("Class=Third");
}}
else
{System.out.println("Result=Fail"); }
}
}

intput and output

Enter the mark
80
80
80
80
80
Total=400
Average=80.0
Result=Pass
Class=First

arithmetic operations using switch statement

import java.io.*;
class arith
{public static void main(String args[])throws IOException
{BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
float a,b,c;
System.out.println("Enter the values");
a=Float.parseFloat(br.readLine());
b=Float.parseFloat(br.readLine());
System.out.println("1.Add");
System.out.println("2.Sub");
System.out.println("3.Mul");
System.out.println("4.Div");
System.out.println("Enter your choice");int ch;
ch = Integer.parseInt(br.readLine());c = 0;
switch(ch)
{ case 1 : c=a+b;
break ;
case 2 : c=a-b;
break ;
case 3 : c=a*b;
break ;
case 4 : c=a/b;
break ;
default : System.out.println("Invalid choice");
break ;}
System.out.println("Result is " +c);
}
}

intput and output

Enter the values
10
10
1.Add
2.Sub
3.Mul
4.Div
Enter your choice
3
Result is 100.0