0% found this document useful (0 votes)
15 views11 pages

Java Loop Programs for Beginners

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Java Loop Programs for Beginners

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link].*;
public class loopb
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int n= [Link]();
[Link]("Digits of the number in reverse order are:");

int digit;
while(n > 0)
{
digit = n % 10;
[Link](digit + " ");
n/= 10;
}

}
}

2. //WAP in java to input a number. Display all the digits of the number in reverse
order.
[HARD PROGRAM]

import [Link].*;
public class loopb
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int n= [Link]();
[Link]("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;
[Link](digit + " ");
n%= divisor;
divisor/= 10;
}
[Link]();
}
}

// 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 [Link].*;
public class loopc
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int n= [Link]();
[Link]("Digits of the number in reverse order are:");

int digit;
while(n > 0)
{
digit = n % 10;
if(digit % 2 == 0)
[Link](digit + ":an even digit");
else
[Link](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 [Link].*;
public class loopd
{
public static void main(String args[])
{
Scanner in = new Scanner ([Link]);
int a, b, c;
for(int i = 1; i <= 10; i++)
{
[Link]("Enter any two number:");
a = [Link]();
b = [Link]();
c = a + b;

if(c < 0){


break;}
[Link]("Sum of " + a + " and " + b + " is " + c);
}
[Link]("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 [Link].*;
public class loope
{
public static void main (String args[])
{
Scanner in = new Scanner([Link]);
int n = [Link]();

[Link]("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 = [Link]();
sqroot = [Link]((double)n1);

if (n1 == 0){
break;
}
if(sqroot * sqroot == n1)
{
[Link]("perfect square number.");
}
else
{
[Link]("not a perfect square number.");
}

}
[Link]("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;
[Link]("All even numbers from 1 to 10 are:");

do{
a++;
if(a % 2 != 0)
{
continue;
}
[Link](a);
}
while( a <= 10);
}
}

// print some ASCII values and their equivalent characters


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

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


Eg, 789
Print: 987

import [Link].*;

public class ReverseOrder {


public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int n = [Link]();
[Link]("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;
[Link](digit);
num /= 10;
}
}
}

//wap to enter a 4 digit number and print the sum of all of its digits

import [Link].*;
public class SumOfDigits
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int n = [Link]();

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;
[Link]("Sum of all of its digits: " + s);
}
}

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

[work on 1]

import [Link].*;
public class primeNumber1
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int n = [Link]();
int count = 0;

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


{
if(n % i == 0)
{
count++;
}
}
if(count == 0)
{
[Link]("This is a prime number.");
}
else
{
[Link]("This is not a prime number.");
}
}
}

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

import [Link].*;
public class gcD
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter the Number 1 : ");
int num1 = [Link]();

[Link]("Enter the Number 2 : ");


int num2 = [Link]();

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;
}
[Link]("HCF = " + y);
}
}

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

import [Link].*;
public class gcD
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter the Number 1 : ");
int num1 = [Link]();

[Link]("Enter the Number 2 : ");


int num2 = [Link]();

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;
[Link]("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 [Link].*;
public class EvenOdd1
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Yo bro, enter the number of integers you want to enter");
int n = [Link]();

int se = 0, so = 0;
for(int i = 1; i <= n; i++)
{
int n1 = [Link]();
if(i % 2 == 0)
{
se+= n1;
}
if(i % 2 != 0)
{
so+= n1;
}
}
[Link]("Sum of even numbers:" + se);
[Link]("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 [Link].*;
public class DOWHILELOOP
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
char choice;
do
{
[Link]("Enter number1:");
int a = [Link]();

[Link]("Enter number2:");
int b = [Link]();
int c = a + b;
[Link]("Sum of the two numbers:" + c);

[Link]("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 = [Link]().charAt(0);
}
while(choice == 'y' || choice == 'Y');
[Link]("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([Link]);

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
{
[Link]("Enter the number ");
number = [Link]();

if(number > max)


{
max = number;
}

if(number < min)


{
min = number;
}

[Link]("Do you want to continue y/n? ");


choice = [Link]().charAt(0);

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


[Link]("Largest number: " + max);
[Link]("Smallest number: " + min);
}
}

Common questions

Powered by AI

The do-while loop is used to ensure that the calculation of two numbers occurs at least once and allows the user to continue performing the addition based on their input. After calculating the sum of two integers provided by the user, it prompts whether the user desires to repeat the process. If the user inputs 'y' or 'Y', the loop repeats. This use of do-while ensures that operations continue as long as the user desires, providing a straightforward means of interaction .

Using a scanner for input allows dynamic and real-time data entry, making the program interactive and flexible. However, its robustness is contingent on user behavior; non-integer inputs can cause runtime exceptions unless validated or handled specifically. While it simplifies receiving data, ensuring robust error-checking mechanisms enhances program resilience .

The program employs a do-while loop to iterate over numbers from 1 to 10. Within the loop, a continue statement is used to skip over numbers that are not even, i.e., not divisible by 2. This logic efficiently filters and prints only the even numbers during its iteration, demonstrating an effective control flow to conditionally execute statements .

The break statement serves as a control mechanism to terminate the loop pre-maturely if the sum of two numbers becomes negative. This is critical in a scenario where the subsequent logic or iterations rely on the fact that all processed sums are non-negative, ensuring program stability and preventing further unnecessary computation cycles .

For the GCD, the program uses the Euclidean algorithm, which efficiently calculates the greatest common divisor by recursively applying the modulus operation until the remainder is zero. The final divisor provides the GCD. For the LCM, it calculates using the relation lcm = num1 * num2 / gcd, leveraging the precomputed GCD to determine the least common multiple. This combination of methods is efficient because the Euclidean algorithm is computationally effective for finding GCD, directly aiding in the faster calculation of LCM without integer overflow .

A for loop is used, iterating through each input number until a zero is encountered, which terminates the program. This structure is effective because it allows the function to check each number in a sequence without prematurely ending the process once a number is identified as a perfect square or not. The loop repeatedly computes the square root using Math.sqrt() and verifies if squaring that root results back in the original number .

The program initializes two variables, max and min, with Integer.MIN_VALUE and Integer.MAX_VALUE, respectively. Inside the do-while loop, each entered number is compared against current max and min values. If a number is greater than max, max is updated, and if smaller than min, min gets updated. The loop continues based on user input, ensuring all numbers are checked for comparison, ultimately displaying the largest and smallest numbers provided .

The program takes an integer input and utilizes a while loop to access and reverse the digits by continually reducing the number by division (n/=10) and getting each digit (digit = n % 10). It then evaluates whether each digit is even or odd using a conditional statement (if(digit % 2 == 0)). The program relies on the user to input valid integers as there is no explicit validation for non-integer inputs .

The program initially reads the four-digit number and repeatedly calculates each digit using division and modulus operations to break the number into its individual digits. These are stored and summed into a variable in a structured sequence, ensuring data integrity before summation. The accurate extraction and processing of each digit ensure that the sum is computed correctly .

The program prints ASCII values alongside their corresponding characters by iterating over defined ranges for numbers and alphabetic characters ('A' to 'Z', 'a' to 'z'). This conversion demonstrates character encoding in Java, showcasing how each character corresponds to a numerical ASCII value. It's significant as it reinforces understanding of character data types and encoding, crucial for many text manipulation and data processing tasks .

You might also like