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

LOOP PROGRAM PRACTICE

The document contains multiple Java programs that demonstrate various programming concepts such as loops, conditionals, and input/output operations. Key functionalities include reversing digits of a number, checking for perfect squares, calculating sums, and finding the greatest common divisor (GCD) and least common multiple (LCM). Each program is designed to perform specific tasks based on user input, showcasing practical applications of Java programming.

Uploaded by

hkschool999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

LOOP PROGRAM PRACTICE

The document contains multiple Java programs that demonstrate various programming concepts such as loops, conditionals, and input/output operations. Key functionalities include reversing digits of a number, checking for perfect squares, calculating sums, and finding the greatest common divisor (GCD) and least common multiple (LCM). Each program is designed to perform specific tasks based on user input, showcasing practical applications of Java programming.

Uploaded by

hkschool999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

LOOP PROGRAM

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;

if(c < 0){


break;}
System.out.println("Sum of " + a + " and " + b + " is " + c);
}
System.out.println("Program terminates.");
}
}

// 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();

System.out.println("Input a set of numbers. We will check if they are perfect squares or


not. enter zero to terminate.");
double sqroot;
for(int i = 1; i <= n; i++)
{
int n1 = in.nextInt();
sqroot = Math.sqrt((double)n1);

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]

public class loopf


{
public static void main(String args[])
{
int a = 0;
System.out.println("All even numbers from 1 to 10 are:");

do{
a++;
if(a % 2 != 0)
{
continue;
}
System.out.println(a);
}
while( a <= 10);
}
}

// print some ASCII values and their equivalent characters


public class series
{
public static void main(String args[])
{
System.out.println("number ascii values:");
int asv= 48, asv2 = 65, asv3 = 97;
char a = 'a';
char A = 'A';
int i;
for(i = 1; i<= 10; i++)
{
System.out.println(i + ":" + asv);
asv++;
}
System.out.println("uppercase (A - Z) ascii values:");
for(i = 1; i<= 26; i++)
{
System.out.println(A + ":" + asv2);
asv2++;
A++;
}
System.out.println("lowercase (a - z) ascii values:");
for(i = 1; i<= 26; i++)
{
System.out.println(a + ":" + asv3);
asv3++;
a++;
}
}
}

//wap to store a number an print it in reverse order.


Eg, 789
Print: 987

import java.util.*;

public class ReverseOrder {


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 num = n;
int digits = 0;

while (num > 0) {


num /= 10;
digits++;
}

num = n;

for (int i = 1; i <= digits; i++) {


int digit = num % 10;
System.out.print(digit);
num /= 10;
}
}
}

//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);
}
}

//wap to enter a number check if it is prime number or not.

[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;

for(int i = 2; i < n; i++)


{
if(n % i == 0)
{
count++;
}
}
if(count == 0)
{
System.out.println("This is a prime number.");
}
else
{
System.out.println("This is not a prime number.");
}
}
}

// Write Java program to Find the (GCD) Greatest Common Divisor

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();

System.out.printf("Enter the Number 2 : ");


int num2 = in.nextInt();

int rem = 0, x = 0, y = 0;

if(num1 > num2)


{
x = num1;
y = num2;
}
else
{
x = num2;
y = num1;
}

rem = x % y;
while(rem != 0)
{
x = y;
y = rem;
rem = x % y;
}
System.out.println("HCF = " + y);
}
}

// Write Java program to Find the (LCM) Lowest Common Multiple

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();

System.out.printf("Enter the Number 2 : ");


int num2 = in.nextInt();

int rem = 0, x = 0, y = 0;

if(num1 > num2)


{
x = num1;
y = num2;
}
else
{
x = num2;
y = num1;
}

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);

System.out.println("Do you want to continue adding numbers? Ik its pointless but


please just for the sake of seeing if this thing works or not, please just type 'y'.");
choice = in.next().charAt(0);
}
while(choice == 'y' || choice == 'Y');
System.out.println("program terminates.");
}
}

//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.

public class FindMaxMin


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

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();

if(number > max)


{
max = number;
}

if(number < min)


{
min = number;
}

System.out.print("Do you want to continue y/n? ");


choice = console.next().charAt(0);

}while(choice=='y' || choice == 'Y');


System.out.println("Largest number: " + max);
System.out.println("Smallest number: " + min);
}
}

You might also like