0% found this document useful (0 votes)
1 views21 pages

Java Solution Sheet 2 Loops

The document contains a collection of Java programs demonstrating basic programming concepts such as loops, conditionals, and arithmetic operations. It includes examples using while, do-while, and for loops to perform tasks like printing numbers, calculating sums, checking for prime numbers, and manipulating strings. Each program is accompanied by input and output examples to illustrate its functionality.

Uploaded by

agarwalji80050
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)
1 views21 pages

Java Solution Sheet 2 Loops

The document contains a collection of Java programs demonstrating basic programming concepts such as loops, conditionals, and arithmetic operations. It includes examples using while, do-while, and for loops to perform tasks like printing numbers, calculating sums, checking for prime numbers, and manipulating strings. Each program is accompanied by input and output examples to illustrate its functionality.

Uploaded by

agarwalji80050
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

1.

Print Numbers from 1 to 10


public class Main {
public static void main(String[] args) {
int i = 1;

while (i <= 10) {


[Link](i);
i++;
}
}
}

2. Print Numbers from 10 to 1


public class Main {
public static void main(String[] args) {
int i = 10;

while (i >= 1) {
[Link](i);
i--;
}
}
}

3. Print Even Numbers from 1 to 20


public class Main {
public static void main(String[] args) {
int i = 2;

while (i <= 20) {


[Link](i);
i += 2;
}
}
}

4. Print Odd Numbers from 1 to 20


public class Main {
public static void main(String[] args) {
int i = 1;

while (i <= 20) {


[Link](i);
i += 2;
}
}
}
5. Sum of First 10 Natural Numbers
public class Main {
public static void main(String[] args) {
int i = 1, sum = 0;

while (i <= 10) {


sum += i;
i++;
}

[Link]("Sum = " + sum);


}
}

Output:

Sum = 55

6. Multiplication Table of a Number


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int n = [Link]();

int i = 1;

while (i <= 10) {


[Link](n + " x " + i + " = " + (n * i));
i++;
}
}
}

Input:

Output:

5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

7. Count Digits in a Number


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int num = [Link]();

int count = 0;

while (num != 0) {
count++;
num = num / 10;
}

[Link]("Digits = " + count);


}
}

Input:

12345

Output:

Digits = 5

8. Reverse a Number
import [Link];

public class Main {


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

[Link]("Enter a number: ");


int num = [Link]();

int reverse = 0;

while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}

[Link]("Reverse = " + reverse);


}
}

Input:

1234

Output:
Reverse = 4321

9. Sum of Digits of a Number


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int num = [Link]();

int sum = 0;

while (num != 0) {
sum += num % 10;
num = num / 10;
}

[Link]("Sum of digits = " + sum);


}
}

Input:

123

Output:

Sum of digits = 6

10. Check Palindrome Number


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int num = [Link]();

int original = num;


int reverse = 0;

while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}

if (original == reverse) {
[Link]("Palindrome Number");
} else {
[Link]("Not a Palindrome Number");
}
}
}

Input:

121

Output:

Palindrome Number

10 Basic Java Programs Using do-while Loop

1. Print Numbers from 1 to 10


public class Main {
public static void main(String[] args) {
int i = 1;

do {
[Link](i);
i++;
} while (i <= 10);
}
}

2. Print Numbers from 10 to 1


public class Main {
public static void main(String[] args) {
int i = 10;

do {
[Link](i);
i--;
} while (i >= 1);
}
}

3. Print Even Numbers from 1 to 20


public class Main {
public static void main(String[] args) {
int i = 2;

do {
[Link](i);
i += 2;
} while (i <= 20);
}
}

4. Print Odd Numbers from 1 to 20


public class Main {
public static void main(String[] args) {
int i = 1;

do {
[Link](i);
i += 2;
} while (i <= 20);
}
}

5. Sum of First 10 Natural Numbers


public class Main {
public static void main(String[] args) {
int i = 1, sum = 0;

do {
sum += i;
i++;
} while (i <= 10);

[Link]("Sum = " + sum);


}
}
Output
Sum = 55

6. Multiplication Table of a Number


import [Link];

public class Main {


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

[Link]("Enter Number: ");


int n = [Link]();

int i = 1;

do {
[Link](n + " x " + i + " = " + (n * i));
i++;
} while (i <= 10);
}
}
Input
5
Output
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

7. Count Digits in a Number


import [Link];

public class Main {


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

[Link]("Enter Number: ");


int num = [Link]();

int count = 0;

do {
count++;
num = num / 10;
} while (num != 0);

[Link]("Digits = " + count);


}
}
Input
12345
Output
Digits = 5

8. Reverse a Number
import [Link];

public class Main {


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

[Link]("Enter Number: ");


int num = [Link]();

int reverse = 0;

do {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
} while (num != 0);

[Link]("Reverse = " + reverse);


}
}
Input
1234
Output
Reverse = 4321

9. Sum of Digits of a Number


import [Link];

public class Main {


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

[Link]("Enter Number: ");


int num = [Link]();

int sum = 0;

do {
sum += num % 10;
num = num / 10;
} while (num != 0);

[Link]("Sum = " + sum);


}
}
Input
123
Output
Sum = 6

10. Check Palindrome Number


import [Link];

public class Main {


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

[Link]("Enter Number: ");


int num = [Link]();

int original = num;


int reverse = 0;

do {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
} while (num != 0);

if (original == reverse)
[Link]("Palindrome Number");
else
[Link]("Not a Palindrome Number");
}
}
Input
121
Output
Palindrome Number

11. Factorial of a Number


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int n = [Link]();

int fact = 1;
int i = 1;

do {
fact = fact * i;
i++;
} while (i <= n);

[Link]("Factorial = " + fact);


}
}

12. Fibonacci Series up to N Terms


import [Link];

public class Main {


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

[Link]("Enter number of terms: ");


int n = [Link]();

int a = 0, b = 1, i = 1;

do {
[Link](a + " ");
int c = a + b;
a = b;
b = c;
i++;
} while (i <= n);
}
}
Input
5
Output
0 1 1 2 3

13. Armstrong Number Check


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int num = [Link]();

int original = num;


int sum = 0;

do {
int digit = num % 10;
sum += digit * digit * digit;
num = num / 10;
} while (num != 0);

if (sum == original)
[Link]("Armstrong Number");
else
[Link]("Not an Armstrong Number");
}
}
Input
153
Output
Armstrong Number

14. Product of Digits of a Number


import [Link];

public class Main {


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

[Link]("Enter a number: ");


int num = [Link]();

int product = 1;

do {
product *= (num % 10);
num = num / 10;
} while (num != 0);
[Link]("Product = " + product);
}
}
Input
123
Output
Product = 6

15. Sum of Even Numbers from 1 to N


import [Link];

public class Main {


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

[Link]("Enter N: ");
int n = [Link]();

int i = 2;
int sum = 0;

do {
sum += i;
i += 2;
} while (i <= n);

[Link]("Sum = " + sum);


}
}
Input
10
Output
Sum = 30

16. Menu Driven Calculator using Do-While & Switch


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


int choice;

do {
[Link]("\n1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("5. Exit");

[Link]("Enter Choice: ");


choice = [Link]();

if(choice >= 1 && choice <= 4) {


[Link]("Enter First Number: ");
int a = [Link]();

[Link]("Enter Second Number: ");


int b = [Link]();

switch(choice) {
case 1:
[Link]("Sum = " + (a + b));
break;

case 2:
[Link]("Difference = " + (a - b));
break;

case 3:
[Link]("Product = " + (a * b));
break;

case 4:
[Link]("Division = " + (a / b));
break;
}
}

} while(choice != 5);

[Link]("Program Ended");
}
}

17. Print ASCII Values of A to Z


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

char ch = 'A';

do {
[Link](ch + " = " + (int)ch);
ch++;
} while(ch <= 'Z');
}
}
Output
A = 65
B = 66
...
Z = 90

18. Largest Digit in a Number


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter Number: ");
int num = [Link]();

int largest = 0;

do {
int digit = num % 10;

if(digit > largest)


largest = digit;

num = num / 10;

} while(num != 0);

[Link]("Largest Digit = " + largest);


}
}
Input
5829
Output
Largest Digit = 9

19. Prime Number Check


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int n = [Link]();

int i = 2;
boolean prime = true;

if(n <= 1)
prime = false;
else {
do {
if(n % i == 0 && i != n) {
prime = false;
break;
}
i++;
} while(i < n);
}

if(prime)
[Link]("Prime Number");
else
[Link]("Not a Prime Number");
}
}
Input
7
Output
Prime Number

20. Reverse a String using Do-While Loop


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter String: ");


String str = [Link]();

int i = [Link]() - 1;

do {
[Link]([Link](i));
i--;
} while(i >= 0);
}
}
Input
JAVA
Output
AVAJ

20 Basic Java Programs Using for Loop

1. Print Numbers from 1 to 10


public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 10; i++) {
[Link](i);
}
}
}

2. Print Numbers from 10 to 1


public class Main {
public static void main(String[] args) {
for(int i = 10; i >= 1; i--) {
[Link](i);
}
}
}
3. Print Even Numbers from 1 to 20
public class Main {
public static void main(String[] args) {
for(int i = 2; i <= 20; i += 2) {
[Link](i);
}
}
}

4. Print Odd Numbers from 1 to 20


public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 20; i += 2) {
[Link](i);
}
}
}

5. Sum of First 10 Natural Numbers


public class Main {
public static void main(String[] args) {
int sum = 0;

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


sum += i;
}

[Link]("Sum = " + sum);


}
}

6. Multiplication Table of a Number


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int n = [Link]();

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


[Link](n + " x " + i + " = " + (n * i));
}
}
}
7. Count Digits in a Number
import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int count = 0;

for(; num != 0; num = num / 10) {


count++;
}

[Link]("Digits = " + count);


}
}

8. Reverse a Number
import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int reverse = 0;

for(; num != 0; num = num / 10) {


reverse = reverse * 10 + num % 10;
}

[Link]("Reverse = " + reverse);


}
}

9. Sum of Digits of a Number


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int sum = 0;

for(; num != 0; num = num / 10) {


sum += num % 10;
}

[Link]("Sum = " + sum);


}
}

10. Palindrome Number Check


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int original = num;


int reverse = 0;

for(; num != 0; num = num / 10) {


reverse = reverse * 10 + num % 10;
}

if(original == reverse)
[Link]("Palindrome Number");
else
[Link]("Not a Palindrome Number");
}
}

11. Factorial of a Number


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int n = [Link]();

int fact = 1;

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


fact *= i;
}
[Link]("Factorial = " + fact);
}
}

12. Fibonacci Series


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Terms: ");


int n = [Link]();

int a = 0, b = 1;

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


[Link](a + " ");
int c = a + b;
a = b;
b = c;
}
}
}

13. Armstrong Number Check


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int temp = num;


int sum = 0;

for(; num != 0; num /= 10) {


int digit = num % 10;
sum += digit * digit * digit;
}

if(sum == temp)
[Link]("Armstrong Number");
else
[Link]("Not Armstrong Number");
}
}
14. Product of Digits
import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int product = 1;

for(; num != 0; num /= 10) {


product *= num % 10;
}

[Link]("Product = " + product);


}
}

15. Sum of Even Numbers from 1 to N


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter N: ");
int n = [Link]();

int sum = 0;

for(int i = 2; i <= n; i += 2) {
sum += i;
}

[Link]("Sum = " + sum);


}
}

16. ASCII Values of A to Z


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

for(char ch = 'A'; ch <= 'Z'; ch++) {


[Link](ch + " = " + (int)ch);
}
}
}
17. Largest Digit in a Number
import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int largest = 0;

for(; num != 0; num /= 10) {


int digit = num % 10;

if(digit > largest)


largest = digit;
}

[Link]("Largest Digit = " + largest);


}
}

18. Prime Number Check


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter Number: ");


int n = [Link]();

boolean prime = true;

if(n <= 1)
prime = false;

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


if(n % i == 0) {
prime = false;
break;
}
}

if(prime)
[Link]("Prime Number");
else
[Link]("Not Prime Number");
}
}
19. Reverse a String
import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter String: ");


String str = [Link]();

for(int i = [Link]() - 1; i >= 0; i--) {


[Link]([Link](i));
}
}
}

20. Check String Palindrome


import [Link];

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter String: ");


String str = [Link]();

String rev = "";

for(int i = [Link]() - 1; i >= 0; i--) {


rev += [Link](i);
}

if([Link](rev))
[Link]("Palindrome String");
else
[Link]("Not a Palindrome String");
}
}

You might also like