# Practice questions on Loop loop loop

## Level 1

1.
Take 10 integers from keyboard using loop and print their average value on the screen.
```import java.util.Scanner;
class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int sum = 0;
for(int i = 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.
```class Ans{
public static void main(String[] args){
for(int i = 1; i<=10;i++){
System.out.println("24 * "+i+"\t=\t"+(24*i));
}
}
}
```

4.
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.
```import java.util.Scanner;
class Ans{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int x = s.nextInt();
int fact  =  1;
for(int i = 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.
```import java.util.*;

class Ans{
public static void main(String[] args) {

String choice = "";

int sum = 0;
int product = 1;
int count = 0;
Scanner input = new Scanner(System.in);

while(!choice.equals("q")){
System.out.println("Enter a number or q to quit");
choice = input.next();

if(!choice.equals("q")){
int number = 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).
```import java.util.*;

class Ans{
public static void main(String[] args) {

int num;
int den;

Scanner input = new Scanner(System.in);

System.out.println("Enter numerator");
num = input.nextInt();
System.out.println("Enter denominator");
den = input.nextInt();

//calculating HCF
int a,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.
```class Ans{
public static void main(String[] args) {
for(int i=1;i<=500;i++){
int sum = 0;
int t = i;
while(t!=0){
sum = sum+((t%10)*(t%10)*(t%10));
t = t/10;
}
if(sum==i){
System.out.println(i);
}
}
}
}
```

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

class Ans{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int x;

System.out.println("Enter the number");
x = input.nextInt();

System.out.println("Prime factors of "+x+" are:");

for(int i=2;i<=x;i++){
//checking for factor
if(x%i==0){
//checking if i is prime or not
int p = 0;
for(int j=2;j<i;j++){
if(i%j==0){
//i is not prime
p++;
break;
}
}
if(p==0){
//if p is 0
//then i is prime
System.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.