0% found this document useful (0 votes)
16 views

Practice Questions On Loops in Java

Uploaded by

Gaurav Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Practice Questions On Loops in Java

Uploaded by

Gaurav Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PRACTICE QUESTIONS ON LOOPS IN JAVA

import java.util.Scanner;

public class loops {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Example 1(Countings)
System.out.print("Enter the last point of countings: ");
int a = sc.nextInt();
int i = 1;
while (i <= a) {
System.out.println(i);
i++;
}

// Example 2(Reverse Counting)


System.out.print("Enter a Starting point of counting: ");
int b=sc.nextInt();
for(int i=b;i>=1;i--){
System.out.println(i);;
}

// Example 3(Table)
System.out.print("Enter a number: ");
int a=sc.nextInt();
int i=1;
do{
System.out.println(a+" x "+i+" = "+a*i);
i++;
}
while(i<=10);

// Question 1(Factorial)
System.out.print("Enter the number: ");
int a=sc.nextInt();
int fact=1;
if(a==0 || a==1){
System.out.println("Factorial: 1");
}else{
for (int i=1;i<=a;i++){
fact*=i;
}
System.out.println("Factorial: "+fact);
}

//Question 2(Prime)
System.out.print("Enter a number: ");
int b=sc.nextInt();
int i=2;
boolean isprime=true;
while (i<b){
if (b%i==0){
isprime=false;
break;
}
i++;

}
if (isprime){
System.out.println("Prime");
}else{
System.out.println("Not prime");
}

// Question 3(sum)
System.out.print("Enter the end point: ");
int a=sc.nextInt();
int i=0;
int sum=0;
do{
sum+=i;
i++;
}while(i<=a);
System.out.println("Sum: "+sum);

// Question 4(Sum of digits)


System.out.print("Enter a number: ");
int a=sc.nextInt();
int sum=0;
int rem;
while (a!=0) {
rem=a%10;
sum+=rem;
a=a/10;

}
System.out.println("Sum of digits: "+sum);

// Question 5(prime factors)


System.out.print("Enter a number: ");
int a = sc.nextInt();
int i = 2;
while (i < a) {
while (a % i == 0) {
System.out.println(i);
a = a / i;
}
i++;
}
if (a > 2) {
System.out.println(a);
}

// Question 6(Fibonacci series)


System.out.print("Enter a numbers of terms in series: ");
int n=sc.nextInt();
int a=0;
int b=1;
int c=a+b;
System.out.println(a+"\n"+b);
for (int i=0;i<n-2;i++){
System.out.println(c);
a=b;
b=c;
c=a+b;

// Question 7(Palindrome number)


System.out.print("Enter a number: ");
int n = sc.nextInt();
int sum = 0;
int rem;
int temp=n;
while (n != 0) {
rem = n % 10;
sum = sum * 10 + rem;
n = n / 10;
}
if (sum == temp) {
System.out.println("Palindrome");

} else {
System.out.println("Not palindrome");
}

// Question 8(armstrong Number)


System.out.print("Enter a number: ");
int n=sc.nextInt();
int sum=0;
int rem;
int temp=n;
while(n!=0){
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;

}if(sum==temp){
System.out.println("Armstrong");
}else{
System.out.println("Not Armstrong");
}

// Question 9(Perfect Number)


System.out.print("Enter a number: ");
int n = sc.nextInt();
int i = 1;
int sum = 0;
while (i < n) {
if (n % i == 0) {
sum += i;
}
i++;
}
if (sum == n) {
System.out.println("Perfect Number");
} else {
System.out.println("Not a Perfect Number");
}

// Question 10(Strong number)


System.out.print("Enter a number: ");
int n = sc.nextInt();
int sum = 0;
int rem;
int temp = n;
while (n != 0) {
rem = n % 10;
int i = 1;
int fact = 1;
while (i <= rem) {
fact *= i;
i++;
}
sum += fact;
n = n / 10;
}
if (sum == temp) {
System.out.println("Strong Number");
} else {
System.out.println("Not a Strong Number");
}

// Question 11
System.out.print("Enter two numbers: ");
int a=sc.nextInt();
int b=sc.nextInt();
int gcd=0;
int i=1;
int lcm;
while(i<=a && i<=b){
if(a%i==0 && b%i==0){
gcd=i;
}
i++;
}
System.out.println("GCD: "+gcd);
lcm=(a*b)/gcd;
System.out.println("LCM: "+lcm);

sc.close();

You might also like