BIIT

Computer Application

JAVA for ICSE Board (Class X)

Solutions for Unsolved Programming Problems

Prog 1:
Write a program to calculate the value of given expression:
1/a2+ 2/b2 + 3/c2
                        
import java.util.*;
class Prog1
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int res,a,b,c;
            System.out.print("Enter the values of a b and c ");
            a=sc.nextInt();
            b=sc.nextInt();
            c=sc.nextInt();
            res=Math.round((1.0f/(a*a))+(2.0f/(b*b))+(3.0f/(c*c)));
            System.out.println("Result = "+res);
        }
}

                        
                        
                        
                        
Prog 2:
For every natural number m>1; 2m, m2-1 and m2+1 form a Pythagorean triplet. Write a program to enter a number from the console to display a Pythagorean triplet.
Sample input: 3
Then 2m=6, m2-1=8 and m2+1=10
Thus 6,8,10 form Pythagorean triple.
                        
import java.util.*;
class Prog2
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int m;
            System.out.print("Enter the values of m (m>1) ");
            m=sc.nextInt();
   System.out.println((2*m)+", "+(m*m-1)+", "+(m*m+1)+" are Pythagorean Triplet");
        }
}

                        
                        
                        
                        
Prog 3:
Write a program to input a number. Calculate its square root and cube root. Finally display the result by rounding off.
Sample input: 5
Square root of 5= 2.2306679
Rounded form= 2
Cube root of 5= 1.7099795
Rounded form=2
                        
import java.util.*;
class Prog3
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n;
            System.out.print("Enter any number ");
            n=sc.nextInt();
            System.out.println("Square root of "+n+" = "+Math.round(Math.sqrt(n)));
            System.out.println("Cube root of "+n+" = "+Math.round(Math.cbrt(n)));
        }
}
                        
                        
                        
Prog 4:
The volume of cube is calculated by using the formula: v=4/3 * 22/7 * r3
Write the program to calculate the radius of the sphere by taking volume as input.
Hint: Radius = ∛(volume* 3/4*7/22)
                        
import java.util.*;
class Prog4
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            double r,v;
            System.out.print("Enter volume of any sphere ");
            v=sc.nextDouble();
            r=Math.cbrt(v*(3.0/4)*(7.0/22));
            System.out.println("Radius = "+r);
        }
}
                        
                        
                        
Prog 5:
A trigonometrical expression is given as : (tan⁡ A-tan⁡ B)/(1+tan A*tan⁡B )
Write a program to calculate the value of the given expression by taking the values of angles A and B as input.
Hint: radian=(22/(7*180)) * degree
                        
import java.util.*;
class Prog5
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            double a,b,res;
            System.out.print("Enter the values of a and b ");
            a=sc.nextDouble();
            b=sc.nextDouble();
            a=a*(22/7*180);
            b=b*(22/7*180);
            res=(Math.tan(a)-Math.tan(b))/(1+Math.tan(a)*Math.tan(b));
            System.out.println("Result = "+res);
        }
}
                        
                        
                        
Prog 6:
The standard form of quadratic equation is represented as:
ax2+bx+c=0
where d=b2-4ac, known as discriminant of the equation.
Write a program to input the values a, b and c. Calculate the value of discriminant and display the output of the nearest whole number.
                        
import java.util.*;
class Prog6
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            double a,b,c,disc;
            System.out.print("Enter the values of a,b and c ");
            a=sc.nextDouble();
            b=sc.nextDouble();
            c=sc.nextDouble();
            disc=Math.pow(b,2)-4*a*c;
            System.out.println("Value of Discriminant = "+Math.round(disc));
        }
}
                        
                        
                        
Prog 1:
Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise, display they are equal. The program also displays whether numbers entered by the user are all ‘positive’ ,’ negative’ or ‘mixed numbers’.
Sample input: 56, -15, 12
Sample output: The greatest number is 56.
Entered numbers are mixed numbers.
                        
import java.io.*;
class Prog1
{
        public static void main(String s[])throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int x,y,z;
            System.out.print("Enter First number ");
            x=Integer.parseInt(br.readLine());
            System.out.print("Enter Second number ");
            y=Integer.parseInt(br.readLine());
            System.out.print("Enter Third number ");
            z=Integer.parseInt(br.readLine());
            if(x==y & y==z)
                    System.out.println("All are equal");
            else if(x>y & x>z)
                    System.out.println("x is greatest");   
            else if(y>z)
                    System.out.println("y is greatest");   
            else        
                   System.out.println("z is greatest");
            if(x>=0 & y>=0 & z>=0)
               System.out.print("All Positive");   
            else if(x<0 & y<0 && z<0)
                   System.out.println("All Negative");   
            else
                   System.out.println("Mixed Number");                  
        }
}
             
                        
                        
                        
                        
Prog 2:
A triangle is said to be an ‘equable triangle‘, if the area of the triangle is equal to its perimeter. Write a program to input the three sides of a triangle. Check and print whether the triangle ids equable or not.
For example, a right angled triangle with sides 5,12,13 has its area and perimeter both equal to 30.
                        
import java.util.*;
class Prog2
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            double s,a,b,c,ar,pr;
            System.out.print("Enter three sides of triangle ");
            a=sc.nextDouble();
            b=sc.nextDouble();
            c=sc.nextDouble();
            pr=a+b+c;
            s=pr/2;
            ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
            if(ar==pr)
                    System.out.println("Equable Triangle");
            else 
                    System.out.println("Non Equable Triangle");   
        }
}

                        
Prog 3:
A special two digit number is such that when the sum of its digit is added to the product of its digits, the result is equal to the original number.
Example: Consider the number 59.
Sum of the digits = 5+9=14
Product of the digits = 5*9=45
Total the sum of its sum and product = 59
Write a program to accept two digit number . Add the sum of its digits to the product of its digits. If the value is equal to the number input by the user, then display ‘It is a special 2-digit number’ otherwise display the message ‘Not a special 2-digit number’.

import java.util.*;
class Prog3
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int num,sum,prod,gsum,dig1,dig2;
            System.out.print("Enter any two digit number ");
            num=sc.nextInt();
            dig1=num/10;
            dig2=num%10;
            sum=dig1+dig2;
            prod=dig1*dig2;
            gsum=sum+prod;
            if(gsum==num)
                System.out.println("Special Two Digit Number");
            else 
                System.out.println("Not a Special Two Digit Number");
        }
}

                        
Prog 4:
The standard form of quadratic equation is given by: ax2+bx+c=0, where d=b2- 4ac, is known as discriminant which determines the nature of the root of the equation as:
ConditionNature
If d>=0Roots are real
If d<0Roots are imagenary
Write a program to determine the nature and roots of the equation. Taking a, b and c as the input, if d=b2-4ac is greater than or equal to zero, then display “The roots are real”, otherwise display ”The roots are imaginary”. The roots of the quadratic equation are determined by the formula as: r1=(-b+√(b^2-4ac))/2a , r2=(-b-√(b^2-4ac))/2a
                        
import java.util.*;
class Prog4
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            double a,b,c,d,r1,r2;
            System.out.print("Enter the values of a, b and c for Quadratic Eq ");
            a=sc.nextDouble();
            b=sc.nextDouble();
            c=sc.nextDouble();
            d=b*b-4*a*c;
            if(d>=0)
            {
                System.out.println("Roots are real ");
                r1=(-b+Math.sqrt(d))/2*a;                
                r2=(-b-Math.sqrt(d))/2*a;
                System.out.println("Root1 = "+r1);
                System.out.println("Root2 = "+r2);                
            }
            else
            {
                System.out.println("Roots are imaginary ");
            }
        }
}

                        
Prog 5:
An Air conditioned bus charges fare from the passenger based on the distance travelled as per the tariff given below:
Distance travelledFare
Upto 10 kmFixed charge Rs 80
11 km to 20 kmRs 6/km
21 km to 30 kmRs 5/km
31 km and aboveRs 4/km

Design a program to input the distance travelled by the passenger. Calculate and display the fare to be paid.
                        
import java.util.*;
class Prog5
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int dist,fare;
            System.out.print("Enter the distance covered by passanger ");
            dist=sc.nextInt();
            if(dist<=10)
                fare=80;
            else if(dist<=20)
                fare=80+(dist-10)*6;
            else if(dist<=30)
                fare=140+(dist-20)*5;//80+60
            else
                fare=190+(dist-30)*4;//80+60+50;
            System.out.println("Bus Fare = "+fare);   
        }
}

                        
Prog 6:
An ICSE School displays a notice on the school notice board regarding the admission in class XI for choosing the stream according to the marks obtained in English , math and science in class 10 council examination.
Marks obtained in different subjectsStream
English, Math and Science >=80%Pure Science
English and Science >=80%, Math >=60%Bio Science
English, Math and Science >=60%Commerce

Print the appropriate stream allotted to the student. Write a program to accept the marks in English , math and science from the console.
                        
import java.util.*;
class Prog6
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            double em,mm,sm;
            System.out.print("Enter the marks in english, maths and science ");
            em=sc.nextDouble();
            mm=sc.nextDouble();
            sm=sc.nextDouble();
            if(em>=80 & mm>=80 & sm>=80)
                System.out.println("Pure Science");
            else if(em>=80 & sm>=80 & mm>=60)
                System.out.println("Bio Science");
            else if(em>=60 & sm>=60 & mm>=60)
                System.out.println("Commerce");
        }
}

                        
Prog 7:
A bank announces new rates for Term Deposit Schemes for their costumers and senior citizens as given below:
TermRate of interest(general)Rate of interest(Seniors)
Up to 1 year7.5%8.0%
Up to 2 years8.5%9.0%
Up to 3 years9.5%10.0%
More than 3 years10.0%11.0%

The ‘senior citizen’ rates are applicable to the costumers whose age is 60 years or mare. Write a program to accept the sum in the term deposit scheme , age if the costumer and the term. The program displays the program in the following format:
Amount depositedTermAgeInterest earnedAmount paid
XXXXXXXXXXXXXXX

                        
import java.util.*;
class Prog7
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            double p,age,term,rate,intt,amt;
            System.out.print("Enter the sum, age and term ");
            p=sc.nextDouble();
            age=sc.nextDouble();
            term=sc.nextDouble();
            if(age>=60)
            {
                if(term<=1)
                    rate=8.0;
                else if(term<=2)
                    rate=9.0;
                else if(term<=3)
                    rate=10.0;
                else
                    rate=11.0;
            }    
            else 
            {
                if(term<=1)
                    rate=7.5;
                else if(term<=2)
                    rate=8.5;
                else if(term<=3)
                    rate=9.5;
                else
                    rate=10.0;
            }
            intt=p*rate*term/100;
            amt=p+intt;
            System.out.println("AmtDip\tTerm\tAge\tIntErn\tAmt");
            System.out.println(p+"\t"+term+"\t"+age+"\t"+intt+"\t"+amt);
        }
}

                        
Prog 8:
A courier company charges differently for ordinary booking and express booking based on the weight of the parcel as per the given tariff:
Weight of parcelOrdinary bookingExpress booking
Up to 100gmRs80Rs100
101 to 500gmRs150Rs200
501 to 1000gmRs210Rs250
More than 1000gmRs250Rs300

Write a program to input the weight of the parcel and the type of booking (‘O’ for ordinary booking and ‘E’ for express booking). Calculate and print the charges accordingly.
                        
import java.util.*;
class Prog8
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            char ch;
            double wt,charge;
            System.out.print("Enter Your choice O/E ");
            ch=sc.next().charAt(0);
            System.out.print("Enter the weight of parcel ");
            wt=sc.nextDouble();
            if(ch=='O'||ch=='o')
            {
                if(wt<=100)
                    charge=80.0;
                else if(wt<=500)
                    charge=150.0;
                else if(wt<=1000)
                    charge=210.0;
                else 
                    charge=250.0;                
            }    
            else 
            {
                if(wt<=100)
                    charge=100.0;
                else if(wt<=500)
                    charge=200.0;
                else if(wt<=1000)
                    charge=250.0;
                else 
                    charge=300.0;                
            }    
            System.out.println("Charge of Parcel = "+charge);
        }
}
                        
Prog 9:
Write the menu driven program to calculate:
a. Area of the circle = πr^2
b. Area of the square = side*side
c. Area of the rectangle = length*breadth
Enter ‘c’ to calculate area of circle , ‘s’ to calculate area of square and ’r’ to calculate area of rectangle
                        
import java.util.*;
class Prog9
{
   
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            char ch;
            double r,s,l,b,p=3.14,ar;
            System.out.println("Menu\nc. Circle\ns.Square\nr.Rectangle");
            System.out.print("Enter Your choice ");
            ch=sc.next().charAt(0);
            switch(ch)
            {
                case 'c':
                    System.out.print("Enter the radius of circle ");
                    r=sc.nextDouble();
                    ar=p*r*r;
                    System.out.print("Area of Circle = "+ar);
                    break;
                case 's':
                    System.out.print("Enter the side of square ");
                    s=sc.nextDouble();
                    ar=s*s;
                    System.out.print("Area of Square = "+ar);
                    break;
                case 'r':
                    System.out.print("Enter the Length and Breadth of Rectangle ");
                    l=sc.nextDouble();
                    b=sc.nextDouble();
                    ar=l*b;
                    System.out.print("Area of Rectangle = "+ar);
                    break;
                default:
                    System.out.println("Wrong choice");
            }    
        }
}

                        
Prog 10:
Write the program using switch case to find the volume if a cube, cuboid and a sphere. For an incorrect choice an appropriate error message should be displayed.
a. Volume of cube = s*s*s
b. Volume of sphere = 4/3* π *r*r*r
c. Volume of cuboid = l*b*h
                        
import java.util.*;
class Prog10
{
   
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int ch;
            double r,s,l,b,h,p=22.0/7,v;
            System.out.println("Menu\n1. Cube\n2.Sphere\n3.Cuboid");
            System.out.print("Enter Your choice ");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                    System.out.print("Enter the side of Cube ");
                    s=sc.nextDouble();
                    v=s*s*s;
                    System.out.print("Volume of Cube = "+v);
                    break;
                case 2:
                    System.out.print("Enter the radius of Sphere ");
                    r=sc.nextDouble();
                    v=(4*p*r*r*r)/3;
                    System.out.print("Volume of Sphere = "+v);
                    break;
                case 3:
               System.out.print("Enter the Length, Breadth & Height of Cuboid ");
                    l=sc.nextDouble();
                    b=sc.nextDouble();
                    h=sc.nextDouble();
                    v=l*b*h;
                    System.out.print("Volume of Cuboid = "+v);
                    break;
                default:
                    System.out.println("Wrong choice");
            }    
        }
}

                        
Prog 11:
The relative velocity of two trains travelling in opposite direction is calculated by adding their speeds. In case they are travelling in the same direction the relative velocity is the difference of their speeds. Write a program to input the velocity and the length of the trains. Write a menu driven program to calculate the relative velocities and the time taken to cross each other.
                        
import java.util.*;
class Prog11
{
   
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int ch;
            double v1,v2,rv=0,l1,l2,t=0;
            System.out.print("Enter length and velocity of First Train ");
            l1=sc.nextDouble();
            v1=sc.nextDouble();
            System.out.print("Enter length and velocity of Second Train ");
            l2=sc.nextDouble();
            v2=sc.nextDouble();
            System.out.println("Menu\n1. Same Direction\n2. Opposit Direction");
            System.out.print("Enter Your choice ");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                    rv=Math.abs(v1-v2);
                    t=(l1+l2)/rv;
                    break;
                case 2:
                    rv=Math.abs(v1+v2);
                    t=(l1+l2)/rv;
                    break;
                default:
                    System.out.println("Wrong choice");
            }  
            System.out.println("Relative Velocity = "+rv);
            System.out.println("Time Taken to cross each other = "+t);            
        }
}

                        
Prog 12:
In order to purchase an old car, the depreciated value can be calculated as per the given tariff:
No. of years usedRate of depreciation
110%
220%
330%
450%
More than 460%

Write a menu driven program to input the showroom price and number of years the car is used (‘1’ for one year, ‘2’ for two years and so on). Calculate the depreciated value. Display the original price of the car, depreciated value and the amount to be paid.
                        
import java.util.*;
class Prog12
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int yr;
            double oprice,amt,dep;
            System.out.print("Enter Years used and original price of car ");
            yr=sc.nextInt();
            oprice=sc.nextDouble();
            switch(yr)
            {
                case 1:
                    dep=oprice*0.1;
                    break;
                case 2:
                    dep=oprice*0.2;
                    break;
                case 3:
                    dep=oprice*0.3;
                    break;
                case 4:
                    dep=oprice*0.5;
                    break;
                default:
                    dep=oprice*0.6;
            }  
            amt=oprice-dep;
            System.out.println("Original Price = "+oprice);
            System.out.println("Depreciation value = "+dep);            
            System.out.println("Amount to be paid = "+amt);            
        }
}

                        
Prog 1:
Write the programs in java to display the first ten terms of the following series:
(i) 0, 1, 2, 3, 6………………………………
                        
class Prog1_a
{
        public static void main(String s[])
        {
            int i,a,b,c,d;
            a=0;
            b=1;
            c=2;
            System.out.print(a+", "+b+", "+c);
            for(i=1;i<=7;i++)
            {
                d=a+b+c;
                System.out.print(", "+d);
                a=b;
                b=c;
                c=d;
            }        
        }
}
                        
(ii) 1, -3, 5, -7, 9…………………………….
                        
class Prog1_b
{
        public static void main(String s[])
        {
            int i,j=1;
            for(i=1;i<=10;i++)
            {
                if(i%2==0)
                     System.out.print(-j+", ");
                else
                     System.out.print(j+", ");
                j=j+2;
            }        
        }
}
                        
(iii) 0, 3, 8, 15………………………………
                        
class Prog1_c
{
        public static void main(String s[])
        {
            int i;
            for(i=1;i<=10;i++)
            {
                System.out.print(i*i-1+", ");
            }        
        }
}
                        
(iv) 1, 11, 111, 1111………………………
                        
class Prog1_d
{
        public static void main(String s[])
        {
            int i,j=0;
            for(i=1;i<=10;i++)
            {
                j=j*10+1;
                System.out.print(j+", ");
            }        
        }
}
                        
(v) 1, 12, 123, 1234………………………
                        
class   prog1_e
{
        public static void main(String s[])
        {
            int i,j=0;
            for(i=1;i<=10;i++)
            {
                j=j*10+i;
                System.out.print(j+", ");
            }        
        }
}
                        
Prog 2:
Write the programs in java to find the sum of the following series:
(i) S= 1 + 1 + 2 + 3 + 5 +……………………………to n terms
                        
import java.util.*;
class Prog2_a
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int i,n,sum,a,b,c;
            System.out.print("Enter the no. of terms ");
            n=sc.nextInt();
            a=1;
            b=1;
            sum=a+b;
            for(i=1;i<=n-2;i++)
            {
                c=a+b;
                sum+=c;
                a=b;
                b=c;
            }
            System.out.print("Sum of series = "+sum);
        }
}
                        
(ii)S= 2 - 4 + 6 - 8 +……………………………………to n terms
                        
import java.util.*;
class Prog2_b
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int i,n,sum=0;
            System.out.print("Enter the no. of terms ");
            n=sc.nextInt();
            for(i=2;i<=n*2;i+=2)
            {
                if(i%4==0)
                    sum-=i;
                else
                    sum+=i;
            }
            System.out.print("Sum of series = "+sum);
        }
}

                        
(iii) S = 1 + (1+2) + (1+2+3) +……………………+ (1+2+3………+n)
                        
import java.util.*;
class Prog2_c
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int i,n,sum=0,j=0;
            System.out.print("Enter the no. of terms ");
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                j=j+i;
                sum=sum+j;
            }
            System.out.print("Sum of series = "+sum);
        }
}

                        
(iv) S = 1 + (1*2) + (1*2*3) +……………………+(1*2*3………*n)
                        
import java.util.*;
class Prog2_d
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int i,n,sum=0,j=1;
            System.out.print("Enter the no. of terms ");
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                j=j*i;
                sum=sum+j;
            }
            System.out.print("Sum of series = "+sum);
        }
}

                        
S= 1+ (1+2)/(1*2) + (1+2+3)/(1*2*3) + ……………………………..+ (1+2+3……..+n)/(1*2*3……….*n)
                        
import java.util.*;
class Prog2_e
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int n,i;
            float sum=0,s=0,p=1;
            System.out.print("Enter the no. of terms ");
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                p=p*i;
                s=s+i;
                sum=sum+s/p;
            }
            System.out.print("Sum of series = "+sum);
        }
}
                        
Prog 3:
Write the programs to find the sum of the following series:
(i) S = a + a2 + a3 + ……………………………………+ an
                        
import java.util.*;
class Prog3_a
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int i,a,n,sum=0;
            System.out.print("Enter the values of a & n ");
            a=sc.nextInt();
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                sum+=Math.pow(a,i);
            }
            System.out.print("Sum of series = "+sum);
        }
}
                        
(ii) S = (a+1) + (a+2) + (a+3) +…………………………………+ (a+n)
                        
import java.util.*;
class Prog3_b
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int i,a,n,sum=0;
            System.out.print("Enter the values of a & n ");
            a=sc.nextInt();
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                sum+=(a+i);
            }
            System.out.print("Sum of series = "+sum);
        }
}

                        
(iii) S = a/2 + a/5 + a/8 + a/11 +…………………………………………….+ a/20
                        
import java.util.*;
class Prog3_c
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            double i,a,sum=0;
            System.out.print("Enter the values of a ");
            a=sc.nextDouble();
            for(i=2;i<=20;i+=3)
            {
                sum+=a/i;
            }
            System.out.print("Sum of series = "+sum);
        }
}
                        
(iv) S = 1/a + 2/a2 + 3/a3 + .................. to n
                        
import java.util.*;
class Prog3_d
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            double n,i,a,sum=0;
            System.out.print("Enter the values of a & n ");
            a=sc.nextDouble();
            n=sc.nextDouble();
            for(i=1;i<=n;i++)
            {
                sum+=i/Math.pow(a,i);
            }
            System.out.print("Sum of series = "+sum);
        }
}
                        
(v) S = a - a3 + a5 - a7 +.................... to n
                        
import java.util.*;
class Prog3_e
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            double n,i,a,sum=0,j=1;
            System.out.print("Enter the values of a & n ");
            a=sc.nextDouble();
            n=sc.nextDouble();
            for(i=1;i<=n;i++)
            {
                if(i%2==0)
                    sum-=Math.pow(a,j);
                else
                    sum+=Math.pow(a,j);
                j+=2;
            }
            System.out.println("Sum of series = "+sum);            
        }
}
                        
Prog 4:
Write a program to enter two numbers and check whether they are co-prime or not.
[Two numbers are said to be co-prime if their HCF is 1.]
Sample Input: 14, 15
Sample Output: They are co-prime.
                        
import java.util.*;
class Prog4
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int x,y,i,hcf=0,m;
            System.out.print("Enter the two values ");
            x=sc.nextInt();
            y=sc.nextInt();
            m=Math.min(x,y);
            for(i=1;i<=m;i++)
            {
                if(x%i==0 & y%i==0)
                {
                    hcf=i;
                    break;
                }
            }
            if(hcf==1)
                System.out.println("Co Prime");
            else
                System.out.println("Non Co Prime");            
        }
}

                        
Prog 5:
Write a program to input a number. Display the product of the successors of even digits of the number entered by the user.
Sample input: 2745
Sample output: 15
                        
import java.util.*;
class Prog5
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n,r,p=1;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            while(n>0)
            {
                r=n%10;
                if(r%2==0)
                {
                    p*=(r+1);
                }
                n=n/10;
            }   
            System.out.println("product is "+p);                
        }
}

                        
Prog 6:
Write a program to input a number and check and print whether it is a pronic number or not.
[Pronic number is the number which is the product of the two consecutive integers.]
Examples: 12=3*4
20=4*5
42=6*7
                        
import java.util.*;
class Prog6_
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n,i,flag=0;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            for(i=1;i<n/2;i++)
            {
                if((i*(i+1))==n)
                {
                    flag=1;
                    System.out.println("Pronic Number");
                    break;
                }
            }
            if(flag==0)
                    System.out.println("Non Pronic Number");           
        }
}

                        
Prog 7:
A prime number is known as twisted prime only if the new number obtained after reversing the digits is also a prime number. Write a program to input a number and check whether the number is twisted prime or not.
Sample input: 167
Sample output: 761
167 is a twisted prime number.
                        
import java.util.*;
class Prog7
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int mm,n,i,flag=0,org,rev=0,r;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            org=n;
            while(n>0)
            {
                r=n%10;
                n=n/10;
                rev=rev*10+r;
            }
            mm=Math.min(org,rev);    
            for(i=2;i<mm/2;i++)
            {
                if(org%i==0 || rev%i==0)
                {
                    flag=1;
                    System.out.println("Not a Twisted Prime Number");
                    break;
                }
            }
            if(flag==0)
                    System.out.println("Twisted Prime Number");           
        }
}

                        
Prog 8:
Write a program to input a number and check whether it is a prime number or not. If it is not a prime number then display the next number which is prime.
Sample input: 14
Sample output: 17
                        
import java.util.*;
class Prog8
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n,i,flag=0,j;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            for(i=2;i<=n/2;i++)
            {
                if(n%i==0)
                {
                    flag=1; 
                    break;
                }
            }
            if(flag==0)
                    System.out.println("Prime Number");           
            else
            {
                i=n+1;
                while(true)
                {
                    flag=0;
                    for(j=2;j<=i/2;j++)
                    {
                        if(i%j==0)
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0)
                    {
                       System.out.println("Next Prime Number = "+i);                           
                       break;
                    }
                    i++;
                }
            }
        }
    }

                        
Prog 9:
A number is said to be duck if the digit zero is present in it. Write a program to accept a number and check whether it is a duck number of not.
The program displays the message accordingly.(The number must not begin with zero)
Sample input: 5063
Sample output: It is a duck number.
Sample input: 7453
Sample output: It is not a duck number.
                        
import java.util.*;
class Prog9_116
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n,dig,flag=0;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            while(n>0)
            {
                dig=n%10;
                n=n/10;
                if(dig==0)
                {
                    flag=1; 
                    break;
                }
            }
            if(flag==0)
                    System.out.println("Not a Duck Number");           
            else
                System.out.println("Duck Number");                           
            }
        }

                        
Prog 10:
Write a program that inputs number of runs made by a cricket player on each ball.Entering the runs as -1 should display the message that the player is out. Finally the program displays the number of runs made and the balls played by the player.
                        
import java.util.*;
class Prog10
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int run,balls=0,total_runs=0;
            do
            {
                System.out.print("Enter the runs ");
                run=sc.nextInt();
                if(run!=-1)
                total_runs+=run;
                balls++;
            }while(run!=-1);
            System.out.println("Number of Balls = "+balls);
            System.out.println("Total Number of runs made = "+total_runs);                
        }
}

                        
Prog 11:
A computerized bus charges fare from its passengers based on the distance travelled as per the tariff given below:
Distance (in km)Charges
First 5 kmRs 80
Next 10 kmRs 10/km
More than 15 kmRs 8/km

As the passenger enters the bus, the computer prompts "Enter the distance you intend to travel". On entering the distance the computer prints its ticket and control goes back for the next passenger. At the end of the journey the computer prints the following:
(i) the number of passenger travelled
(ii)total fare received.
Write the program to perform the following task.

                        
import java.util.*;
class Prog11
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int fare,t_Fare=0,nop=0,dist;
            char ch;
            do
            {
                System.out.print("Enter the Distance ");
                dist=sc.nextInt();
                if(dist<=5)
                    fare=80;
                else if(dist<=15)
                    fare=(dist-5)*10+80;
                else
                    fare=(dist-15)*8+180;
                    
                t_Fare+=fare;
                nop++;
                System.out.println("Total Distance = "+dist);
                System.out.println("Total Fare to be paid = "+fare);                
                System.out.println("More Passenger ");
                ch=sc.next().charAt(0);    
            }while(ch=='y'||ch=='Y');
            System.out.println("Total Number of Passengers = "+nop);
            System.out.println("Total Fare received = "+t_Fare);                
        }
}

                        
Prog 12:
A special two-digit number is such that when the sum of its digit is added to the product of its digit, the result is equal to the original two digit number.
Example: consider the number 59
Sum of the digits = 5+9=14
Product of the digits = 5*9=45
Total the sum of its sum and product = 59
Write a program to accept two digit number . Add the sum of its digits to the product of its digits. If the value is equal to the number input by the user, then display ‘It is a special 2-digit number’ otherwise display the message ‘Not a special 2-digit number’.
                        
import java.util.*;
class Prog12
{
        public static void main(String sa[])
        {
            Scanner sc=new Scanner(System.in);
            int num,sum,prod,gsum,dig1,dig2;
            System.out.print("Enter any two digit number ");
            num=sc.nextInt();
            dig1=num/10;
            dig2=num%10;
            sum=dig1+dig2;
            prod=dig1*dig2;
            gsum=sum+prod;
            if(gsum==num)
                System.out.println("Special Two Digit Number");
            else 
                System.out.println("Not a Special Two Digit Number");
        }
}
                        
Prog 13:
Write a program to input a number. Check and display whether it is a niven number or not.
(A number is said to be niven if it is divisible by the sum of its digits)
Sample input: 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
                        
import java.util.*;
class Prog13_116
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n,r,sum=0,org;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            org=n;
            while(n>0)
            {
                r=n%10;
                n=n/10;
                sum+=r;
            }
            if(org%sum==0)
                    System.out.println("Niven Number");           
            else
                System.out.println("not a Niven Number");                           
        }
    }
                        
Prog 14:
Write a program to accept a number and check whether it is a spy number or not.
(A number is said to be spy if the sum of its digits is equal to the product of its digits.)
Sample input: 1124
Sum of its digits = 1 + 1 + 2 + 4 = 8
Product of its digits = 1 * 1 * 2 * 4 = 8
                        
import java.util.*;
class Prog14_116
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int n,r,sum=0,prod=1;
            System.out.print("Enter the number ");
            n=sc.nextInt();
            while(n>0)
            {
                r=n%10;
                n=n/10;
                sum+=r;
                prod*=r;
            }
            if(sum==prod)
                    System.out.println("Spy Number");           
            else
                System.out.println("not a Spy Number");                           
        }
    }
                        
Prog 15:
You can multiply two numbers 'm' and 'n' by repeated addition method.
For example, 5*3=15 can be performed by adding 5 three times = 5 + 5 + 5 = 15
Similarly, successive substraction of two numbers can produce quotient and remainder when a number 'a' is divided by 'b' (a>b).
For example, 5/2 = Quotient = 2 and remainder = 1.
Follow steps shown below:
ProcessResultCounter
5 - 231
3 - 212

Sample output: The last counter value represents Quotient = 2
The last result value represents remainder = 1
Write the program to accept two numbers. perform the multiplication and division of the numbers as per the process shown above by using switch case statement.
                        
import java.util.*;
class Prog15
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int m,n,i,min,max,ch,d,cnt=0,prod=0;
            System.out.print("Enter the values of m and n ");
            m=sc.nextInt();
            n=sc.nextInt();
            min=Math.min(m,n);
            max=Math.max(m,n);
            System.out.println("Menu\n1.Product\n2.Divide");
            System.out.println("Enter the choice ");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                    for(i=1;i<=min;i++)
                    {
                        prod+=max;
                    }
                    System.out.println("Product is "+prod);
                    break;
                case 2:
                    do
                    {
                        d=max-min;
                        max=d;
                        cnt++;
                    }while(max>=min);
                    System.out.println("Quotient is "+cnt);
                    System.out.println("Remainder is "+d);
                    break;
                 default:
                    System.out.println("Sorry");                           
            }
        }
    }
                        
Prog 16:
Using a switch statement, write a menu driven program to:
(a) generate and display the first 10 terms of the Fibonacci series
0, 1, 1, 2, 3, 5 ...
The first two Fibonacci numbers are 0 and 1, and each subsequent number 1s the sum of the previous two.
(b) find the sum of the digits of an integer that is input by the user.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be displayed.
                        
import java.util.*;
class Prog16
{
        public static void main(String s[])
        {
            Scanner sc=new Scanner(System.in);
            int a,b,c,sum=0,i,ch;
         System.out.println("Menu\n1. Fibonacci Series\n2. Sum of Digits");
            System.out.print("Enter your choice ");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                    a=0;b=1;
                    System.out.print(a+", "+b);
                    for(i=1;i<=8;i++)
                    {
                        c=a+b;
                        System.out.print(", "+c);
                        a=b;
                        b=c;
                    }
                    break;
                case 2:
                    System.out.print("Enter the number ");
                    a=sc.nextInt();
                    while(a>0)
                    {
                        b=a%10;
                        a=a/10;
                        sum+=b;
                    }   
                    System.out.println("Sum of digits = "+sum);           
                    break;
                 default:
                    System.out.println("Sorry");                           
            }
        }
    }

Prog 1:
Write programs to find the sum of the following series:
(a) S = 1 + 3/2! + 5/3! + 7/4! + .......................... to n
                        
import java.util.*;
class Prog1_a
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        double i,j,k,f,sum=0,n;
        System.out.print("Enter the number ");
        n=sc.nextDouble();
        for(i=1,k=1;i<=n;i+=2,k++)
        {
            f=1;
            for(j=1;j<=k;j++)
            {
                f*=j;
            }
            sum=sum+i/f;            
        }
        System.out.println("Sum of series = "+sum);
    }
}
                        
(b) S = a + a/2! + a/3! + a/4! +............................. + a/n!
                        
import java.util.*;
class Prog1_b
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        double a,i,j,f,sum=0,n;
        System.out.print("Enter the values of a and n ");
        a=sc.nextDouble();
        n=sc.nextDouble();
        for(i=1;i<=n;i++)
        {
            f=1;
            for(j=1;j<=i;j++)
            {
                f*=j;
            }
            sum=sum+a/f;            
        }
        System.out.println("Sum of series = "+sum);
    }
}
                        
(c) S = a - a/2! + a/3! - a/4! +....................................... + to n
                        
import java.util.*;
class Prog1_c
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        double a,i,f=1,sum=0,n;
        System.out.print("Enter the values of a and n ");
        a=sc.nextDouble();
        n=sc.nextDouble();
        for(i=1;i<=n;i++)
        {
            f*=i;
            if(i%2==0)
                sum=sum-a/f;
            else
                sum=sum+a/f;            

        }
        System.out.println("Sum of series = "+sum);
    }
}
                        
(d) S = a/2! - a/3! + a/4! - a/5! +.................................. + a/10!
                        
import java.util.*;
class Prog1_d
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        double a,i,f=1,sum=0;
        System.out.print("Enter the values of a ");
        a=sc.nextDouble();
        for(i=2;i<=10;i++)
        {
            f*=i;
            if(i%2==0)
                sum=sum+a/f;
            else
                sum=sum-a/f;            

        }
        System.out.println("Sum of series = "+sum);
    }
}
                        
(e) S = 2/a + 3/a2 + 5/a3 + 7/a4 +........................................ to n
                        
import java.util.*;
class Prog1_e
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int a,i,n,p=2,j,flag;
        double sum=0;
        System.out.print("Enter the values of a & n ");
        a=sc.nextInt();
        n=sc.nextInt();
        for(i=1;i<=n;i++)
        {
            for(;;p++)
            {
               flag=0;
               for(j=2;j<=p/2;j++)
                {
                    if(p%j==0)
                    {
                        flag=1;
                        break;
                    }    
                }
                if(flag==0)
                {
                    sum=sum+p/Math.pow(a,i);
                    p++;
                    break;
                }  
            }
        }
        System.out.println("Sum of series = "+sum);
    }
}
                        
Prog 2:
Write a program to input two numbers and check whether they are twin prime numbers or not.
Hint: Twin prime numbers are those prime numbers whose difference is two.
For example: (5,7) , (11,13) ................................. so on.
                        
import java.util.*;
class Prog2
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int x,y,d,flag=0,max,i;
        System.out.print("Enter the two numbers ");
        x=sc.nextInt();
        y=sc.nextInt();
        d=Math.abs(x-y);
        if(d==2)
        {
            max=Math.max(x,y);
            for(i=2;i<=max/2;i++)
            {
                if(x%i==0 || y%i==0)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                System.out.println("Twin Prime Nos");
            }
            else
            {
                System.out.println("Non Twin Prime");
            }
        }
        else
        {
            System.out.println("Non Twin Prime");
        }
    }
}
       
                        
Prog 3:
Write a program to display all the numbers` between 100 and 200 which do not contain zero at any position.
For example: 111, 112, 113....................................199.
                        
class Prog3
{
    public static void main(String s[])
    {
        int i,n,flag,r;
            for(i=101;i<200;i++)
            {
                flag=0;
                n=i;
                while(n>0)
                {
                    r=n%10;
                    if(r==0)
                    {
                        flag=1;
                        break;
                    }
                    n=n/10;
                }
                if(flag==0)
                {
                    System.out.println(i);
                }
            }
    }
}
       
                        
Prog 4:
Write a program to display all prime palindrome number between 10 and 1000.
Hint : A number which is prime as well as palindrome is said to be prime palindrome number.
For example: 11,101,131,151.................................
                        
class Prog4
{
    public static void main(String s[])
    {
            int j,i,n,org,flag,r,rev;
            for(i=10;i<1000;i++)
            {
                n=i;
                org=i;
                rev=0;
                while(n>0)
                {
                    r=n%10;
                    n=n/10;
                    rev=rev*10+r;
                }
                if(org==rev)
                {
                    flag=0;
                    for(j=2;j<=i/2;j++)
                    {
                        if(i%j==0)
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0)
                    {
                        System.out.println(i);
                    }
                }               
            }
    }
}
       
                        
Prog 5:
In an entrance examination students have been appeared in english , maths and science examinations. Write a program to calculate and display the average marks obtained by all the students. Take the number of students appeared and marks obtained in all three subjects by every student along with the name as input. Display the name , marks obtained in three subjects and the average of all the students.
                        
import java.util.*;
class Prog5
{
    public static void main(String s[])
    {
            Scanner sc=new Scanner(System.in);
            int i,n;
            float mm,ms,me,avg;
            String name;
            System.out.print("Enter the number of students appeared ");
            n=sc.nextInt();
            for(i=1;i<=n;i++)
            {
                System.out.print("Enter your name ");
                name=sc.next();
                System.out.print("Enter the marks in three subjects ");
                mm=sc.nextFloat();
                ms=sc.nextFloat();
                me=sc.nextFloat();
                avg=(mm+ms+me)/3;
                System.out.println("Name : "+name);
                System.out.println("Average : "+avg);
            }
    }    
}       
                
     
                        
Prog 6:
Write a program in java to enter a number containing three digits or more. Arrange the digits of the entered number in ascending order and display the result.
Sample input: Enter a number 4972
Sample output: 2,4,7,9.
                        
import java.util.*;
class Prog6
{
    public static void main(String s[])
    {
            Scanner sc=new Scanner(System.in);
            int i,n,r,org;;
            System.out.print("Enter any number ");
            n=sc.nextInt();
            org=n;                
            for(i=0;i<=9;i++)
            {
                while(n>0)
                {
                    r=n%10;
                    if(r==i)
                       System.out.print(r);
                    n=n/10;
                }            
                n=org;
            }           
    }    
}
                        
Prog 7:
Write a program to input a number and check whether it is Magic Number or not.
Display the message accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample lnput: 55
Then, 5 + 5 = 10, 1 + 0 = 1
Sample Output: Hence, 55 is a Magic Number.
Similarly, 289 is a Magic Number.

                        
import java.util.*;
class Prog7
{
    public static void main(String s[])
    {
            Scanner sc=new Scanner(System.in);
            int i,n,r,sum;
            System.out.print("Enter any number ");
            n=sc.nextInt();
            do
            {
                sum=0;
                while(n>0)
                {
                    r=n%10;
                    sum=sum+r;
                    n=n/10;
                }
                n=sum;                
            }while(n>9);
            if(sum==1)
                    System.out.println("Magic no.");
            else
                System.out.println("Not a magic number");                       
    }    
}       

                        
Prog 8:
A number is said to be Multiple Harshad number, when divided by the sum of its digits, produces another Harshad Number'. Write a program to input a number and check whether it is a Multiple Harshad Number or not.
(When a number is divisible by the sum of its digit, it is called Harshad Number').
Sample Input: 6804
Hint: 6804 = 6+8+0+4 = 18 -> 6804/18 = 378
378 = 3+7+8 = 18 -> 378/18 = 21
21 = 2+1 = 3 -> 21/3 = 7
Sample Output: Multiple Harshad Number
                        
import java.util.*;
class Prog8
{
    public static void main(String s[])
    {
            Scanner sc=new Scanner(System.in);
            int n,r,sum,org;
            System.out.print("Enter any number ");
            n=sc.nextInt();
            do
            {
                org=n;                
                sum=0;
                while(n>0)
                {
                    r=n%10;
                    sum=sum+r;
                    n=n/10;
                }
                n=org/sum;
                if(org%sum!=0)
                    break;                                
            }while(n<9);
            if(n<=9)
                  System.out.println("Multiple Harshad no.");
            else
                System.out.println("Not a Multiple Harshad no.");                       
    }    
}       
      

                        
Prog 9:
Write the programs to display the following patterns:
(a)
1
3 1
5 3 1
7 5 3 1
9 7 5 3 1

                        
class Prog9a
{
    public static void main(String s[])
    {
        int i,j;
        for(i=1;i<=9;i+=2)
        {
            for(j=i;j>=1;j-=2)
            System.out.print(j+" ");
            System.out.println();
        }
    }
}
                        
(b)
1   2   3   4   5
6   7   8   9
10 11 12
13 14
15
                        
                        
                        
class Prog9b
{
    public static void main(String s[])
    {
        int i,j,k=1;
        for(i=5;i>=1;i--)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(k+"\t");
                k++;
            }
            System.out.println();
        }
    }
}
                        
(c)
15 14 13 12 11
10  9   8   7
6   5   4
3   2
1
                        
class Prog9c
{
    public static void main(String s[])
    {
        int i,j,k=15;
        for(i=5;i>=1;i--)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(k+"\t");
                k--;
            }
            System.out.println();
        }
    }
}
                        
(d)
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
                        
class Prog9d
{
    public static void main(String s[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                if(j%2==0)
                System.out.print("0 ");
                else
                System.out.print("1 ");
            }
            System.out.println();
        }
    }
}
                        
(e)
5 5 5 5 5
   4 4 4 4
     3 3 3
       2 2
         1
                        
class Prog9e
{
    public static void main(String s[])
    {
        int i,j;
        for(i=5;i>=1;i--)
        {
            for(j=5;j>i;j--)
            System.out.print("  ");
            for(j=1;j<=i;j++)
            System.out.print(i+" ");
            System.out.println();
        }
    }
}
                        
(f)
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
                        
class Prog9f
{
    public static void main(String s[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            System.out.print(i+" ");
            for(j=i+1;j<=5;j++)
            System.out.print(j+" ");
            System.out.println();
        }
    }
}
                        
(g)
*
* #
* # *
* # * #
* # * # *
                        
class Prog9g
{
    public static void main(String s[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                if(j%2==0)
                System.out.print("# ");
                else
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}
                        
(h)
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
                        
class Prog9h
{
    public static void main(String s[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=5;j>=i;j--)            
                System.out.print(j+" ");            
            System.out.println();
        }
    }
}
                        
(h)
1
2   3
4   5   6
7   8   9   10
11 12 13 14 15
                        
class Prog9h
{
    public static void main(String s[])
    {
        int i,j,k=1;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(k+"\t");
                k++;
            }
            System.out.println();
        }
    }    
}
                        
Prog 10:
Write aprogram to generate a triangle or an inverted triangle till n terms based upon user's choice.
Example 1:Example 2:
Input: Type 1 for a triangle and Type 2 for inverted triangle
Enter your choice 1
Enter the number of terms 5
Input: Type 1 for a triangle and Type 2 for inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Sample output:
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
                        
import java.util.*;
class Prog10
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int i,j,ch;
        System.out.println("Menu\n1.Triangle\n2.Inverted Triangle");
        System.out.println("Enter your choice ");
        ch=sc.nextInt();
        switch(ch)
        {
                case 1:
                    for(i=1;i<=5;i++)//row
                    {
                        for(j=1;j<=i;j++)
                        {
                            System.out.print(i+" ");
                        }
                        System.out.println();
                    }
                    break;
                case 2:
                    for(i=6;i>=1;i--)//row
                    {
                        for(j=1;j<=i;j++)
                        {
                            System.out.print(i+" ");
                        }
                        System.out.println();
                    }
                    break;
                default:
                    System.out.println("Wrong choice");
        }
    }    
}
                        
Prog 11:
Using the switch statement, write the menu driven program for the following:
(a) To print the floyd's triangle:
1
2   3
4   5   6
7   8   9   10
11 12 13 14 15
(b) To displaythe following pattern:
I
I C
I C S
I C S E
For an incorect choice a approproate error message should be displayed.
                        
import java.util.*;
class Prog11
{
    public static void main(String s[])
    {
        Scanner ob=new Scanner(System.in);
        int ch,i,j,k=1;
        String str;
        System.out.println("Menu:\n1.Floyd's Triangle\n2.String Triangle");
        System.out.println("Enter your choice");
        ch=ob.nextInt();
        switch(ch)
        {
            case 1:
            for(i=1;i<=5;i++)//row
            {
                for(j=1;j<=i;j++)
                {
                    System.out.print(k+"\t");
                    k++;
                }
                System.out.println();
            }
            break;
            case 2:
            System.out.print("Enter any string ");
            str=ob.next();
            for(i=0;i<str.length();i++)
            {
                for(j=0;j<=i;j++)
                {
                    System.out.print(" "+str.charAt(j));
                }
                System.out.println();
            }
            break;
            default:
            System.out.println("Wrong choice");
        }
    }
}
   
                        
Prog 1:
Write a program in java to input a character. Find and display the next tenth character in ASCII table.
                        
import java.util.*;
class Prog1
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           char ch,nwchr;
           System.out.print("Enter the character ");
           ch=sc.next().charAt(0);
           nwchr=(char)(ch+10);
           System.out.println("10th character = "+nwchr);
       }
}
                        
Prog 2:
Write a program in java to input a character. Display next 5 characters.
                        
import java.util.*;
class Prog2
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           char ch;
           int i;
           System.out.print("Enter the character ");
           ch=sc.next().charAt(0);
           for(i=1;i<=5;i++)
                System.out.print((char)(ch+i)+",");
       }
}
                        
Prog 3:
Write a program in java to generate all alternate characters in the range of letters from A to Z.
                        
class Prog3
{
       public static void main(String s[])
       {
           for(int i=65;i<=90;i+=2)
                System.out.print((char)i+",");
       }
}
                        
Prog 4:
Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display the number of vowels and number of consonants present in the set of the given letters.
                        
import java.util.*;
class Prog4
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           char ch;
           int i,cntc=0,cntv=0;
           for(i=1;i<=20;i++)
           {
               System.out.print("Enter the character ");
               ch=sc.next().charAt(0);
               ch=Character.toUpperCase(ch);
               switch(ch)
               {
                   case 'A':
                   case 'E':
                   case 'I':
                   case 'O':
                   case 'U':
                        cntv++;
                        break;
                    default:
                        cntc++;
               }
           }
           System.out.println("No. of Vowels = "+cntv);
           System.out.print("No. of Consonants = "+cntc);           
       }
}
                        
Prog 5:
Write a program in java to accept an integer number N such that 0<N<27. Display the corresponding letter of the alphabet(i.e. letter at the positon N)
Hint: if N=1 then display A.
                        
import java.util.*;
class Prog5
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           int num;
           char ch;
           System.out.print("Enter any number(0<n<27) ");
           num=sc.nextInt();
           ch=(char)(64+num);
           System.out.print("Equivalent Character = "+ch);           
       }
}
                        
Prog 6:
Write a program to input two characters from the keyboard. Find the difference (d) between their ASCII codes. Display the following messages:
If d=0:    both the characters are same.
If d<0:    first character is smaller.
If d>0:    second character is smaller.
Sample Input :D  P
Sample Output : d= (68-80) = -12
First character is smaller
                        
import java.util.*;
class Ex6_162
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           int d;
           char ch1,ch2;
           System.out.print("Enter two characters ");
           ch1=sc.next().charAt(0);
           ch2=sc.next().charAt(0);
           d=ch1-ch2;
           if(d==0)
                System.out.println("Both characters are same");
           else if(d<0)
                System.out.println("First character is smaller");
           else 
                System.out.println("Second character is smaller");
            
       }
}
                        
Prog 7:
Write a program to input a set of any ten integer numbers. Find the sum and the product of the numbers. Join the sum and the product of the numbers to form a single number. Display the concatenated number.
[Hint: let the sum = 245 and the product = 1346 then the number after joining sum and product will be 2451346]
                        
import java.util.*;
class Prog7
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           String str;
           int i,sum=0,prod=1,num;
           for(i=1;i<=10;i++)
           {
               System.out.print("Enter number ");
               num=sc.nextInt();
               sum+=num;
               prod*=num;
           }
           str=Integer.toString(sum)+Integer.toString(prod);
           System.out.println("Content(after concatenation) = "+str);            
                  
       }
}
                        
Prog 8:
Write a menu driven program to generate the upper case letters from Z to A and lower case letters from a to z as per the user's choice.
Enter 1 to display upper case letters from Z to A and 2 to display the lower case letters from a to z.
                        
import java.util.*;
class Prog8
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           int ch,i;
           System.out.println("Menu\n1. Z to A\n2. a to z");
           System.out.print("Enter your choice ");
           ch=sc.nextInt();
           switch(ch)
           {
               case 1:
                    for(i=90;i>=65;i--)
                    System.out.print((char)i+",");
                    break;
               case 2:
                    for(i=97;i<=122;i++)
                    System.out.print((char)i+",");
                    break;
                default:
                    System.out.println("Wrong choice");
           }
      }
}
                        
Prog 9:
Write a program to input a letter. Find its ASCII code. Reverse the ASCII code and display the equivalent character.
Sample input: Y
Sample output: ASCII code = 89
Reverse the ASCII code = 98
Equivalent character: b
                        
import java.util.*;
class Prog9
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           char ch;
           int ascii,rev=0,r;
           System.out.print("Enter any character ");
           ch=sc.next().charAt(0);
           ascii=(int)ch;
           while(ascii>0)
           {
               r=ascii%10;
               rev=rev*10+r;
               ascii=ascii/10;
           }
           System.out.print("Equivalent character = "+(char)rev);
      }
}
                        
Prog 10:
Write a menu driven program to display
(i) first five upper case letters
(ii) last five lower case letters as per the user's choice.
Enter '1' to display upper case letters and enter '2' to display the lower case letters.
                        
import java.util.*;
class Prog10
{
       public static void main(String s[])
       {
           Scanner sc=new Scanner(System.in);
           int ch,i;
           System.out.println("Menu\n1. I 5 UpperCase \n2. Last 5 LowerCase");
           System.out.print("Enter your choice ");
           ch=sc.nextInt();
           switch(ch)
           {
               case 1:
                    for(i=65;i<70;i++)
                    System.out.print((char)i+",");
                    break;
               case 2:
                    for(i=118;i<=122;i++)
                    System.out.print((char)i+",");
                    break;
                default:
                    System.out.println("Wrong choice");
           }
      }   
}
                        
Prog 11:
Write a program in java to display the following patterns:
(i)
A
a b
A B C
a b c d
A B C D E

                        
class Prog11a
{
       public static void main(String s[])
       {
           int i,j;
           for(i=1;i<=5;i++)//row
           {
               for(j=1;j<=i;j++)//col
               {
                   if(i%2!=0)
                        System.out.print((char)(64+j));
                   else
                        System.out.print((char)(96+j));
               }
               System.out.println();
           }
       }
}
                        
(ii)
Z Y X W V
Z Y X W
Z Y X
Z Y
Z
                        
class Prog11b
{
       public static void main(String s[])
       {
           int i,j;
           for(i=5;i>=1;i--)//row
           {
               for(j=1;j<=i;j++)//col
               {
                   System.out.print((char)(91-j));
               }
               System.out.println();
           }
       }
}

                        
(iii)
A B C D E
A B C
A
                        
class Prog11c
{
       public static void main(String s[])
       {
           int i,j;
           for(i=5;i>=1;i-=2)//row
           {
               for(j=1;j<=i;j++)//col
               {
                   System.out.print((char)(64+j));
               }
               System.out.println();
           }
       }
}

                        
(iv)
P R T V
P R T
P R
P
                        
class Prog11d
{
       public static void main(String s[])
       {
           int i,j;
           for(i=4;i>=1;i--)//row
           {
               for(j=1;j<=2*i;j+=2)//col
               {
                   System.out.print((char)(79+j));
               }
               System.out.println();
           }
       }
}
                        
(v)
A*B*C*D*E*
A*B*C*D*
A*B*C*
A*B*
A*
                        
class Prog11e
{
       public static void main(String s[])
       {
           int i,j;
           for(i=5;i>=1;i--)//row
           {
               for(j=1;j<=i;j++)//col
               {
                   System.out.print((char)(64+j)+"*");
               }
               System.out.println();
           }
       }
}
                        
(vi)
a a a a a
b b b b b
A A A A A
B B B B B
                        
class Prog11f
{
       public static void main(String s[])
       {
           int i,j;
           for(i=1;i<=4;i++)//row
           {
               for(j=1;j<=5;j++)//col
               {
                   if(i<=2)
                        System.out.print((char)(96+i));
                   else
                        System.out.print((char)(62+i));                   
               }
               System.out.println();
           }
       }
}
                        
Prog 1:
Write a program in java to store 20 numbers(even and odd numbers) in a single Dimensional Array. Calculate and display the sum of all even numbers and all odd numbers saperately.
                        
import java.util.*;
class Prog1
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int arr[]=new int[20];
        int i,sume=0,sumo=0;
        for(i=0;i<20;i++)
        {
            System.out.print("Enter the number ");
            arr[i]=sc.nextInt();
        }
        for(i=0;i<20;i++)
        {
            if(arr[i]%2==0)
                sume+=arr[i];
            else
                sumo+=arr[i];            
        }
        System.out.println("Sum of even numbers "+sume);
        System.out.println("Sum of odd numbers "+sumo);        
    }
}
                        
Prog 2:
Write a program in java to store 20 temperatures in fahrenheit in a single dimensional and display all the temperatures after converting them to celsius.
Hint: c/5=(f-32)/9
                        
import java.util.*;
class Prog2
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        float fah[]=new float[20];
        float cel;
        int i;
        for(i=0;i<20;i++)
        {
            System.out.print("Enter temp in fah ");
            fah[i]=sc.nextFloat();
        }
        for(i=0;i<20;i++)
        {
            cel=((fah[i]-32)*5)/9;
            System.out.println("Converted Temp in Cel "+cel);
        }
    }
}
                        
Prog 3:
Write a program in java to store ten numbers (including positive and negative numbers) in a single dimensional array. Display all the negative numbers followed by the positive numbers without changing the order of the numbers
Sample input:
n[0]n[1]n[2]n[3]n[4]n[5]n[6]n[7]n[8]n[9]
1521-32-41546171-19-4452

Sample output: -32, -41, -19, -44, 15, 21, 54, 61, 71, 52.
                        
import java.util.*;
class Prog3
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int arr[]=new int[10];
        int i;
        for(i=0;i<10;i++)
        {
            System.out.print("Enter element ");
            arr[i]=sc.nextInt();
        }
        for(i=0;i<10;i++)
        {
            if(arr[i]<0)
                System.out.print(arr[i]+", ");            
        }
        for(i=0;i<10;i++)
        {
            if(arr[i]>=0)
                System.out.print(arr[i]+", ");            
        }
    }
}
                        
Prog 4:
Write a program in java to store 20 numbers in single dimensional array. Display the numbers which are prime.
Sample input:

n[0]n[1]n[2]n[3]n[4]n[5]n[6]n[7]n[8]n[9]
45657771906782193132

Sample output: 71, 67, 19, 31.
                        
import java.util.*;
class Prog4
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int arr[]=new int[20];
        int i,j,flag;
        for(i=0;i<20;i++)
        {
            System.out.print("Enter element ");
            arr[i]=sc.nextInt();
        }
        for(i=0;i<20;i++)
        {
            flag=0;
            for(j=2;j<=arr[i]/2;j++)
            {
                if(arr[i]%j==0)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
                System.out.print(arr[i]+", ");            
        }        
    }
}
                        
Prog 5:
Write a program to accept name and total marks of N number of students in two sungle dimensional arrays name[] and totalmarks[].
Calculate and print:
(i) The average of total marks obtained by N number of students
(ii) Deviation of each students totalmarks with the average.
                        
import java.util.*;
class Prog5
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int n;        
        System.out.println("Enter the number of students ");
        n=sc.nextInt();
        float m[]=new float[n];
        String name[]=new String[n];
        float tot=0,avg,dev;
        int i;
        for(i=0;i<n;i++)
        {
            System.out.print("Enter the name and marks ");
            name[i]=sc.next();
            m[i]=sc.nextFloat();
            tot+=m[i];
        }
        avg=tot/n;
        System.out.println("Average marks of all students "+avg);
        for(i=0;i<n;i++)
        {
            System.out.println("Deviation in marks with average "+(m[i]-avg));
        }                
    }
}
                        
Prog 6:
The marks obtained by 50 students in a subject are tabulated as follows:
NameMarks
........................................
............................................
...........................................

Write a program to input the name and marks of the students in the subject.
Calculate and display:
(i) The subject average marks(subject average marks = subject tatal/50)
(ii) The highest marks in the subject and the name of the student. (The maximum marks in the subject are 100.)
                        
import java.util.*;
class Prog6
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        float m[]=new float[50];
        String name[]=new String[50];
        float tot,avg=0,max;
        String tName="";
        int i;
        for(i=0;i<50;i++)
        {
            System.out.print("Enter the name and marks ");
            name[i]=sc.next();
            m[i]=sc.nextFloat();
        }
        max=m[0];
        tot=m[0];
        tName=name[0];
        for(i=1;i<50;i++)
        {
            tot+=m[i];
            if(max<m[i])
            {
                max=m[i];
                tName=name[i];
            }
        }
        avg=tot/50;
        System.out.println("Average marks of all students "+avg);
        System.out.println(tName+" Scored Highest Marks = "+max);
    }
}

                        
Prog 7:
Write a program in java using arrays:
(i) To store the roll no., name and marks in six subjects for 100 students.
(ii) Calculate the percentage of marks obtained by each candidate. The maximum marks in each subject are 100.
(iii) Calculate the grade as per the given criteria:
Percentage marksGrade
From 80 to 100A
From 60 to 79B
From 40 to 59C
Less 40D
                        
import java.util.*;
class Prog7
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int rn[]=new int[100];
        float m[][]=new float[100][6];
        String name[]=new String[100];
        float tot,per;
        int i,j;
        for(i=0;i<100;i++)
        {
            System.out.print("Enter the rn & name ");
            rn[i]=sc.nextInt();
            name[i]=sc.next();
            for(j=0;j<6;j++)
            {
                System.out.println("Enter the marks ");
                m[i][j]=sc.nextFloat();
            }
        }
        for(i=0;i<100;i++)
        {   
            tot=0;
            for(j=0;j<6;j++)
            {
                tot+=m[i][j];
            }
            per=tot/6;
            System.out.println("Your percentage of marks = "+per);
            if(per>=80)
                System.out.println("Grade A");
            else if(per>=60)
                System.out.println("Grade B");
            else if(per>=40)
                System.out.println("Grade C");
            else
                System.out.println("Grade D");
        }
    }
}

                        
Prog 8:
Write a program to input a list of 20 integers. Sort the first ten numbers in ascending order and the next ten numbers in descending order by using bubble sort technique. Finally print the complete list of integers.
                        
import java.util.*;
class Prog8
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int arr[]=new int[20];
        int temp,i,k;
        for(i=0;i<20;i++)
        {
            System.out.print("Enter the number ");
            arr[i]=sc.nextInt();
        }
        for(k=1;k<10;k++)//Passes
        {
            for(i=0;i<10-k;i++)//Steps
            {
                if(arr[i]>arr[i+1])
                {
                    temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                }
            }
        }
        for(k=1;k<10;k++)//Passes
        {
            for(i=10;i<20-k;i++)//Steps
            {
                if(arr[i]<arr[i+1])
                {
                    temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                }
            }
        }
        for(i=0;i<20;i++)
            System.out.println(arr[i]);
    }
}

                        
Prog 9:
The class teacher wants to store the marks obtained in english, maths and science of her class having 40 students. Write a program to input marks in eng, maths and science by using three single dimemsional arrays. Calculate and print the following information:
(i) Average marks secured by each student
(ii) Class average in each subject.
                        
import java.util.*;
class Prog9
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        float me[]=new float[40];
        float ms[]=new float[40];
        float mm[]=new float[40];
        float tot,avg,se=0,sm=0,ss=0;
        int i,j;
        for(i=0;i<40;i++)
        {
            System.out.print("Enter the marks in eng, maths & Sc ");
            me[i]=sc.nextFloat();
            mm[i]=sc.nextFloat();
            ms[i]=sc.nextFloat();           
        }
        for(i=0;i<40;i++)
        {
            tot=me[i]+mm[i]+ms[i];
            avg=tot/3;
            System.out.println("Average of student "+avg);
            se+=me[i];
            sm+=mm[i];
            ss+=ms[i];
        }
        System.out.println("Average of English = "+se/40);
        System.out.println("Average of Maths = "+sm/40);
        System.out.println("Average of Science = "+ss/40);
    }
}

                        
Prog 10:
Write a program to accept 20 numbers in single dimensional array arr[20]. Transfer and store all the even numbers in an array even[] and all odd numbers in another array odd[]. Finally print the elements of both the arrays.
                        
import java.util.*;
class Prog10
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int all[]=new int[20];
        int even[]=new int[20];
        int odd[]=new int[20];
        int i,j=0,k=0;
        for(i=0;i<20;i++)
        {
            System.out.print("Enter element ");
            all[i]=sc.nextInt();
        }
        for(i=0;i<20;i++)
        {
            if(all[i]%2==0)
            {
                even[j]=all[i];
                j++;
            }
            else
            {
                odd[k]=all[i];
                k++;
            }
        }
        System.out.println("\nEven Numbers");
        for(i=0;i<j;i++)
            System.out.print(even[i]+", ");
        System.out.println("\nOdd Numbers");
        for(i=0;i<k;i++)
            System.out.print(odd[i]+", ");            
    }
} 
                        
Prog 11:
Write a program to store 20 numbers in a single dimensional array. Now display only those numbers which are perfect square.
n[0]n[1]n[2]n[3]n[4]n[5]n[6]n[7]n[8]n[9]
12454978647781994533

Sample output: 49,64,81.
                        
import java.util.*;
class Prog11
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int num[]=new int[20];
        int i,sq;
        double dif,sqr;
        for(i=0;i<20;i++)
        {
            System.out.print("Enter element ");
            num[i]=sc.nextInt();
        }
        for(i=0;i<20;i++)
        {
            sqr=Math.sqrt(num[i]);
            sq=(int)sqr;
            dif=sqr-sq;
            if(dif==0)
                System.out.print(num[i]+", ");
        }        
    }
} 
                        
Prog 12:
To get promotion in a Science stream, a student must pass in English and should pass in any of the two subjects (i.e.; Physics, Chemistry or Maths). The passing mark in each subject is 35. Write a program in a Single Dimensional Array to accept the roll numbers and marks secured in the subjects for all the students. The program should check and display the roll numbers along with a message whether "Promotion is Granted" or "Promotion is not Granted". Assume that there are 40 students in the class.
                        
import java.util.*;
class Prog12
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int rn[]=new int[40];
        float me[]=new float[40];
        float mm[]=new float[40];
        float mp[]=new float[40];
        float mc[]=new float[40];
        int i;
        for(i=0;i<40;i++)
        {
            System.out.print("Enter the rn & marks in (E, P, C, & M) ");
            rn[i]=sc.nextInt();
            me[i]=sc.nextFloat();
            mp[i]=sc.nextFloat();
            mc[i]=sc.nextFloat();
            mm[i]=sc.nextFloat();
        }
        for(i=0;i<40;i++)
        {   
            if((me[i]>=35) & ((mp[i]>=35 & mc[i]>=35)||(mp[i]>=35 & mm[i]>=35)||(mm[i]>=35 & mc[i]>=35)))
                System.out.println("Promotion is granted to Mr. "+rn[i]);
            else
                System.out.println("Promotion is Not granted to Mr. "+rn[i]);
        }
    }
}

                        
Prog 13:
Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional Array (say, A). Truncate the fractional part of each number of the array A and store their integer part in another array (say, B).
                        
import java.util.*;
class Prog13
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        double a[]=new double[10];
        int b[]=new int[10];
        int i;
        for(i=0;i<10;i++)
        {
            System.out.print("Enter element ");
            a[i]=sc.nextDouble();
        }
        for(i=0;i<;10;i++)
        {
            b[i]=(int)a[i];
        }
        for(i=0;i<10;i++)
        {
            System.out.print(b[i]+", ");
        }        
    }
} 
                        
Prog 14:
The annual examination result of 50 students in a class is tabulated in a single dimensional array as follows:
Roll no.Subject ASubject BSubject C
................................
................................
................................
Write a program to read the data, calculate and display the following:
(a)Average marks obtained by each student.
(b)Print the roll number and the average marks of the students whose average is above 80.
(c)Print the roll number and the average marks of the students whose average is below 80.
                        
import java.util.*;
class Prog14
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int rn[]=new int[50];
        float m1[]=new float[50];
        float m2[]=new float[50];
        float m3[]=new float[50];
        float avg[]=new float[50];
        int i;
        for(i=0;i<50;i++)
        {
            System.out.print("Enter the rn & marks in three subjects ");
            rn[i]=sc.nextInt();
            m1[i]=sc.nextFloat();
            m2[i]=sc.nextFloat();
            m3[i]=sc.nextFloat();
        }
        for(i=0;i<50;i++)
        {   
            avg[i]=(m1[i]+m2[i]+m3[i])/3;
            System.out.println("Average of Mr. "+rn[i]+" = "+avg[i]);
        }
        for(i=0;i<50;i++)
        {   
            if(avg[i]>80)
                System.out.println("Average of Mr. "+rn[i]+" is more than 80");
        }
        for(i=0;i<50;i++)
        {   
            if(avg[i]<80)
                System.out.println("Average of Mr. "+rn[i]+" is less than 80");
        }
    }
}

                        
Prog 15:
Write a program to store 6 elements in array P and 4 elements in an array Q and produce a third array R, containing all the elements of the array P and Q. Display the resultant array.
InputInputOutput
P[]Q[]R[]
4194
6236
171
282
33
1010
19
23
7
8
                        
import java.util.*;
class Prog15
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int P[]=new int[6];
        int Q[]=new int[4];
        int R[]=new int[10];
        int i,j=0;
        System.out.println("For Array P ");
        for(i=0;i<6;i++)
        {
            System.out.print("Enter element  ");
            P[i]=sc.nextInt();
        }
        System.out.println("For Array Q ");
        for(i=0;i<4;i++)
        {
            System.out.print("Enter element ");
            Q[i]=sc.nextInt();
        }
        for(i=0;i<10;i++)
        {   
            if(i<P.length)
                R[i]=P[i];
            else
            {
                R[i]=Q[j];
                j++;
            }
        }
        for(i=0;i<10;i++)
        {   
            System.out.print(R[i]+",");
        }
    }
}

                        
Prog 16:
Write a program to accept the year of graduation from school as an integer from the user. Using the binary search technique on the sorted array of integers given below output the message "Record exists" if the value input is located in the array. if not, output the message "Record does not exists".
Sample input:
n[0]n[1]n[2]n[3]n[4]n[5]n[6]n[7]n[8]n[9]
1982198719931996199920032006200720092010
                        
import java.util.*;
class Prog16
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int yog[]={1982,1987,1993,1996,1999,2003,2006,2007,2009,2010};
        int mid,beg,end,year,loc=0;
        System.out.print("Enter the Graduation year ");
        year=sc.nextInt();
        beg=0;
        end=9;
        while(beg<=end)
        {
            mid=(beg+end)/2;
            if(yog[mid]==year)
            {
                loc=mid+1;
                break;
            }
            else if(yog[mid]>year)
              end=mid-1;
            else
                beg=mid+1;
        }
        if(loc==0)
            System.out.print("Record does not exist");
        else
            System.out.println("Record exists");
    }
}

                        
Prog 17:
Write a program to input and roll numbers, names and marks in 3 subjects of n number of students in five single dimensional arrays and display all the remarks based on average marks as given below:
Average marksRemarks
85-100Excellent
75-84Distinction
60-74First class
40-59Pass
less than 40Poor
                        
import java.util.*;
class 17
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int n;
        System.out.print("Enter the no. of students ");
        n=sc.nextInt();
        int rn[]=new int[n];
        String name[]=new String[n];
        float m1[]=new float[n];
        float m2[]=new float[n];
        float m3[]=new float[n];
        float avg;
        int i;
        for(i=0;i<n;i++)
        {
            System.out.print("Enter the rn, name & marks in 3 subjects ");
            rn[i]=sc.nextInt();
            name[i]=sc.next();
            m1[i]=sc.nextFloat();
            m2[i]=sc.nextFloat();
            m3[i]=sc.nextFloat();
        }
        for(i=0;i<n;i++)
        {   
            avg=(m1[i]+m2[i]+m3[i])/3;
            System.out.println("Roll Number = "+rn[i]);
            System.out.println("Name = "+name[i]);
            if(avg>=85)
                System.out.println("Excellent");
            else if(avg>=75)
                System.out.println("Distinction");
            else if(avg>=60)
                System.out.println("First Class");
            else if(avg>=40)
                System.out.println("Pass");
            else
                System.out.println("Poor");
        }
    }
}

                        
Prog 18:
A double dimensional array is defined as N[4][4] to store the numbers. Write the program to find the sum of all even numbers and product of all odd numbers of the elements stored in DDA.
12101517
30113271
17142931
41334051
Sample output:
Sum of all even numbers:........
Sum of all odd numbers:.........
                        
import java.util.*;
class Prog18
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        int m[][]=new int[4][4];
        int i,j,sum=0,prod=1;
        for(i=0;i<4;i++)//row
        {
            for(j=0;j<4;j++)//col
            {
                System.out.print("Enter element  ");
                m[i][j]=sc.nextInt();
            }
        }
        for(i=0;i<4;i++)//row
        {
            for(j=0;j<4;j++)//col
            {
                if(m[i][j]%2==0)
                    sum+=m[i][j];
                else
                    prod*=m[i][j];
            }            
        }
        System.out.println("Sum of Even numbers = "+sum);                   
        System.out.println("Product of Odd numbers = "+prod);                   
    }
}

                        
Prog 19:
A departmental store has 5 stores and 6 departments. The monthly sale of the department is kept in double dimensional array as m[5][6].
The manager of the shop wants to know the total monthly sale of each store and each department at any time. Write a program to perform the following task.
(Hint : Number of stores as rows and number of department as columns.)
                        
import java.util.*;
class Prog19
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        double sale[][]=new double[5][6];
        int i,j;
        for(i=0;i<5;i++)//Stores
        {
            for(j=0;j<6;j++)//Departments
            {
                System.out.print("Enter Monthly Sale of Store #"+(i+1));
                System.out.print("# Deptt #"+(j+1)+"# = ");
                sale[i][j]=sc.nextDouble();
            }
        }
        for(i=0;i<5;i++)//Stores
        {
            for(j=0;j<6;j++)//Departments
            {
                System.out.print("Monthly Sale of Store #"+(i+1));
                System.out.println("# Deptt #"+(j+1)+"# = "+sale[i][j]);       
            }            
        }
    }
}

                        
Prog 20:
A meteropolitan hotel have five floor and 10 rooms on each floor. The names of the visitors are entered in a double dimensional array as M [5][10].
The hotel manager wants to know from the enquiry about the position of the visitors(i.e. floor number and room number) as soon as he enters the name of the visitor. Write a program in java to perform the above task.
                        
                        
Prog 21:
A class teacher wants to keep the record of 40 students of her class along with their name and marks obtained in english , hindi, maths, science and computer science in a double dimensional array as M[40][5].
When the teacher enters the name of a student as an input, the program must display the name, marks obtained in five subjects and the total. Write the program in java to perform the task.
                        
import java.util.*;
class Prog21
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        String name[]=new String[40];
        int m[][]=new int[40][5];
        int i,j,tot=0;
        String nm;
        for(i=0;i<40;i++)
        {
            System.out.print("Enter the name ");
            name[i]=sc.next();
            for(j=0;j<5;j++)
            {
                System.out.print("Enter Marks ");
                m[i][j]=sc.nextInt();
            }
        }
        System.out.print("Enter the name to be searched ");
        nm=sc.next();
        for(i=0;i<40;i++)
        {
            if(nm.equalsIgnoreCase(name[i])==true)
            {
                System.out.println("Name : "+name[i]);
                for(j=0;j<5;j++)
                {
                    System.out.println("Marks : "+m[i][j]);
                    tot+=m[i][j];
                }
                System.out.println("Total Marks : "+tot);       
            }            
        }
    }
}

                        
Prog 22:
If arrays M and M+N are as shown below, write a program in java to find the array N.
M =
-102
-3-16
43-1
M+N=
-694
450
1-2-3
                        
class Prog22
{
    public static void main(String s[])
    {
        int M[][]={{-1,0,2},{-3,-1,6},{4,3,-1}};
        int MPN[][]={{-6,9,4},{4,5,0},{1,-2,-3}};
        int N[][]=new int[3][3];
        int i,j;
        System.out.println("Matrix N");
        for(i=0;i<3;i++)//row
        {
            for(j=0;j<3;j++)//col
            {
                N[i][j]=MPN[i][j]-M[i][j];                
                System.out.print("\t"+N[i][j]);
            }
            System.out.println();            
        }
    }
}

                        
Prog 1:
Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence.
(ii) Number of letters present in the sentence.
                        
import java.util.Scanner;
class Prog1
{
    public static void main(String s[])
    {
        Scanner ob=new Scanner(System.in);
        String str;
        int i,l,w=0,le=0;
        char ch;
        System.out.println("Enter a sentence");
        str=ob.nextLine();
        l=str.length();
        for(i=0;i<l;i++)
        {
            ch=str.charAt(i);
            if(Character.isLetter(ch))
            le++;
            if(ch==' ')
            w++;
        }
        System.out.println("Number of words = "+(w+1));
        System.out.println("Number of letters = "+le);
    }
}
                        
Prog 2:
Write a program in java to accept a word or string and display the new string after removing all the vowels present in it.
Sample input: COMPUTER APPLICATION.
Sample output: CMPTR PPLCTNS
                        
import java.util.*;
class Prog2
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        String st;
        int l,i;
        System.out.println("Enter the string in Capital letter ");
        st=sc.nextLine();
        l=st.length();
        for(i=0;i<l;i++)
        {
            switch(st.charAt(i))
            {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    break;
                default:
                    System.out.print(st.charAt(i));
            }
        }
    }
}
                        
Prog 3:
Write a program in java to accept a name(Containing three words) and display only the initials (i.e. first letter of each word).
Sample input: LAL KRISHNA ADVANI
Sample output: L K A.
                        
import java.util.*;
class Prog3
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        String st;
        int ind1,ind2;
        System.out.println("Enter the string");
        st=sc.nextLine();
        ind1=st.indexOf(' ');
        ind2=st.lastIndexOf(' ');
        System.out.print(st.charAt(0)+" "+st.charAt(ind1+1)+" "+st.charAt(ind2+1));
    }
}
                        
Prog 4:
Write a program in java to accept a name containing three words and display the surname first, followed by the first and the middle name.
Sample input: MOHANDAS KARAMCHAND GANDHI
Sample output: GANDHI MAHANDAS KARAMCHAND
                        
import java.util.*;
class Prog4
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        String st,st1,st2;
        int ind;
        System.out.println("Enter the string");
        st=sc.nextLine();
        ind=st.lastIndexOf(' ');
        st1=st.substring(0,ind);
        st2=st.substring(ind+1);
        System.out.print(st2+" "+st1);
    }
}
                        
Prog 5:
Write a program in java to accept a string/sentence display the longest word and the length of the longest word present in the string
Sample input: "TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN"
Sample output: The longest word: FOOTBALL: The length of the word is: 8
                        
import java.util.*;
class Prog5
{
    public static void main(String s[])
    {
        Scanner sc=new Scanner(System.in);
        String str,st="",maxst="";
        int max=0,l,i;
        System.out.println("Enter the string ");
        str=sc.nextLine();
        str+=" ";
        for(i=0;i<str.length();i++)
        {
            if(str.charAt(i)!=' ')
            {
                st+=str.charAt(i);
            }
            else
            {
                l=st.length();
                if(max<l)
                {
                    max=l;
                    maxst=st;
                }
                st="";             
            }        
        }
        System.out.println("Longest Word = "+maxst);
    }
}
                        
Prog 6:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program
                        
Prog no.:
question
                        program