LOOP PROGRAM PRACTICE
LOOP PROGRAM PRACTICE
PRACTICE
Sun 14th Jan
1.
//WAP in java to input a number. Display all the digits of the number in reverse order.
import java.util.*;
public class loopb
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n= in.nextInt();
System.out.println("Digits of the number in reverse order are:");
int digit;
while(n > 0)
{
digit = n % 10;
System.out.println(digit + " ");
n/= 10;
}
}
}
2. //WAP in java to input a number. Display all the digits of the number in reverse
order.
[HARD PROGRAM]
import java.util.*;
public class loopb
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n= in.nextInt();
System.out.println("Digits of the number in original order are:");
int divisor = 1;
int digit;
while(n/ divisor >= 10)
{
divisor*= 10;
}
while(n > 0)
{
digit = n / divisor;
System.out.println(digit + " ");
n%= divisor;
divisor/= 10;
}
System.out.println();
}
}
// write a program to input a number and display all the digits of the number by stating
weather it is an even or an odd digit by using do while loop.
import java.util.*;
public class loopc
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n= in.nextInt();
System.out.println("Digits of the number in reverse order are:");
int digit;
while(n > 0)
{
digit = n % 10;
if(digit % 2 == 0)
System.out.println(digit + ":an even digit");
else
System.out.println(digit + ":an odd digit");
n/= 10;
}
}
}
// write a program in Java to display the sum of any two numbers for 10 iterations. If the
sum of two numbers is negative then the program will terminate.
import java.util.*;
public class loopd
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int a, b, c;
for(int i = 1; i <= 10; i++)
{
System.out.println("Enter any two number:");
a = in.nextInt();
b = in.nextInt();
c = a + b;
// write a program to input a set of numbers. The program checks whether each number is a
perfect square number or not. The program will terminate when zero is entered by the user.
import java.util.*;
public class loope
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n1 == 0){
break;
}
if(sqroot * sqroot == n1)
{
System.out.println("perfect square number.");
}
else
{
System.out.println("not a perfect square number.");
}
}
System.out.println("Program terminates.");
}
}
// write a program to display all even numbers from 1 to 10 by using continue statement in
a do while loop.
[HARD PROGRAM]
do{
a++;
if(a % 2 != 0)
{
continue;
}
System.out.println(a);
}
while( a <= 10);
}
}
import java.util.*;
int num = n;
int digits = 0;
num = n;
//wap to enter a 4 digit number and print the sum of all of its digits
import java.util.*;
public class SumOfDigits
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int fd = n / 1000;
n = n % 1000;
int sd = n / 100;
n = n % 100;
int td = n / 10;
n = n % 10;
int ld = n;
int s = fd + sd + td + ld;
System.out.println("Sum of all of its digits: " + s);
}
}
[work on 1]
import java.util.*;
public class primeNumber1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int count = 0;
import java.util.*;
public class gcD
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.printf("Enter the Number 1 : ");
int num1 = in.nextInt();
int rem = 0, x = 0, y = 0;
rem = x % y;
while(rem != 0)
{
x = y;
y = rem;
rem = x % y;
}
System.out.println("HCF = " + y);
}
}
import java.util.*;
public class gcD
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.printf("Enter the Number 1 : ");
int num1 = in.nextInt();
int rem = 0, x = 0, y = 0;
rem = x % y;
while(rem != 0)
{
x = y;
y = rem;
rem = x % y;
}
lcm = num1 * num2 / y;
System.out.printf("Lowest Common Multiple is : "+lcm);
}
}
// Write a program that reads a set of integers, and then prints the sum of the even and odd
integers.
import java.util.*;
public class EvenOdd1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Yo bro, enter the number of integers you want to enter");
int n = in.nextInt();
int se = 0, so = 0;
for(int i = 1; i <= n; i++)
{
int n1 = in.nextInt();
if(i % 2 == 0)
{
se+= n1;
}
if(i % 2 != 0)
{
so+= n1;
}
}
System.out.println("Sum of even numbers:" + se);
System.out.println("Sum of odd numbers:" + so);
}
}
// Write a do-while loop that asks the user to enter two numbers. The numbers should be
added and the sum displayed. The loop should ask the user whether he or she wishes to
perform the operation again. If so, the loop should repeat; otherwise it should terminate.
[HARD PROGRAM]
import java.util.*;
public class DOWHILELOOP
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
char choice;
do
{
System.out.println("Enter number1:");
int a = in.nextInt();
System.out.println("Enter number2:");
int b = in.nextInt();
int c = a + b;
System.out.println("Sum of the two numbers:" + c);
//Write a program to enter the numbers till the user wants and at the end the program
should display the largest and smallest numbers entered.
int number;
int max = Integer.MIN_VALUE; // Intialize max with minimum value
int min = Integer.MAX_VALUE; // Intialize min with maximum value
char choice;
do
{
System.out.print("Enter the number ");
number = console.nextInt();