Take 10 integers from keyboard using loop and print their average value on the screen.
importjava.util.Scanner;classAns{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);intsum=0;for(inti=0;i<10;i++){System.out.println("Enter a number");sum=sum+s.nextInt();}System.out.println("Sum is "+sum);}}
2.
Print the following patterns using loop : a. * ** *** **** b. * *** ***** *** * c. 1010101 10101 101 1
3.
Print multiplication table of 24, 50 and 29 using loop.
Print ASCII values and their equivalent characters. ASCII value vary from 0 to 255.
5.
Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.- 4! = 1*2*3*4 = 24 3! = 3*2*1 = 6 2! = 2*1 = 2 Also, 1! = 1 0! = 0 Write a Java program to calculate factorial of a number.
importjava.util.Scanner;classAns{publicstaticvoidmain(String[]args){Scanners=newScanner(System.in);System.out.println("Enter a number");intx=s.nextInt();intfact=1;for(inti=x;i>=1;i--){fact=fact*i;}System.out.println("Factorial is "+fact);}}
6.
Write a program to find greatest common divisor (GCD) or highest common factor (HCF) of given two numbers
7.
Take integer inputs from user until he/she presses q ( Ask to press q to quit after every integer input ). Print average and product of all numbers.
importjava.util.*;classAns{publicstaticvoidmain(String[]args){Stringchoice="";intsum=0;intproduct=1;intcount=0;Scannerinput=newScanner(System.in);while(!choice.equals("q")){System.out.println("Enter a number or q to quit");choice=input.next();if(!choice.equals("q")){intnumber=Integer.parseInt(choice);sum=sum+number;product=product*number;count++;}}System.out.println("Product is: "+product+"\nAverage is: "+((float)sum/count));}}
8.
Write an infinite loop. A inifinte loop never ends. Condition is always true.
Level 2
1.
Take as input a fraction in the form a/b. Convert the same into lowest terms and print.
(Lowest terms examples 3/12 = 1/4).
importjava.util.*;classAns{publicstaticvoidmain(String[]args){intnum;intden;Scannerinput=newScanner(System.in);System.out.println("Enter numerator");num=input.nextInt();System.out.println("Enter denominator");den=input.nextInt();//calculating HCFinta,b,gcd,t;if(num>den){a=num;b=den;}else{b=num;a=den;}while(b!=0){t=b;b=a%b;a=t;}gcd=a;System.out.println("In lowest form = "+num/gcd+"/"+den/gcd);}}
2.
Calculate the sum of digits of a number given by user. E.g.- INUPT : 123 OUPUT : 6 INUPT : 12345 OUPUT : 15
3.
A three digit number is called Armstrong number if sum of cube of its digit is equal to number itself. E.g.- 153 is an Armstrong number because (13)+(53)+(33) = 153. Write all Armstrong numbers between 100 to 500.
Write a program to print all prime number in between 1 to 100.
5.
Write a program to find prime factor of a number. If a factor of a number is prime number then it is its prime factor.
importjava.util.*;classAns{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);intx;System.out.println("Enter the number");x=input.nextInt();System.out.println("Prime factors of "+x+" are:");for(inti=2;i<=x;i++){//checking for factorif(x%i==0){//checking if i is prime or notintp=0;for(intj=2;j<i;j++){if(i%j==0){//i is not primep++;break;}}if(p==0){//if p is 0//then i is primeSystem.out.println(i);}}}}}
6.
Write a program in java to find the sum of the even and odd digits of the number which is given as input.