10 Basics Programs using for(), while() and do-while() (Java)
So today I am going to give you some of the programs using for() loop, while() loop and do-while() loop. If you know the basics of the for() loop, while() loop and do-while() loop or if you have followed our earlier blogs then you will be able to write the codes which I am going to give. The programs which I am going to give, first you try to do it by yourself and then you can match the solution which I have provided. So without any further delay lets get started.
Q1. Write a program to calculate and print the sum of first n natural numbers. The integer n is to be entered by the user.
Solution:
import java.util.*; class Sum { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a integer: "); int n = sc.nextInt(); int sum = 0; for(int i = 1;i <= n;i++) { sum = sum+i; } System.out.println("Sum = " + sum); } }
Output: Input a integer: 5 Sum = 15
Q2. Write a program to calculate and print the sum of odd numbers and sum of even numbers for the first n natural numbers. The integer n is to be entered by the user.
Solution:
import java.util.*; class Sum { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a integer: "); int n = sc.nextInt(); int sumEven = 0,sumOdd = 0; for(int i = 1;i <= n;i++) { if(i%2 == 0) { sumEven = sumEven+i; } else { sumOdd = sumOdd+i; } } System.out.println("Sum of Even Numbers = " + sumEven); System.out.println("Sum of Odd Numbers = " + sumOdd); } }
Output: Input a integer: 10 Sum of Even Numbers = 30 Sum of Odd Numbers = 25
Q3. Write a program to calculate factorial of a number using for() loop, which is calculated by multiplying all natural numbers starting from q to the given number or reverse of it i.e. from the given number to 1.
For Example. Factorial of 4 => 1*2*3*4 = 24 or 4*3*2*1=24.
Solution:
import java.util.*; class Factorial_For_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int c = n; System.out.print(c); for(int i = n-1;i > 0;i--) { c = c*i; System.out.print("*" + i); } System.out.println("=" + c); } }
Output: Enter a number: 4 4*3*2*1=24
Q4. Write a program to calculate factorial of a number using do-while() loop, which is calculated by multiplying all natural numbers starting from q to the given number or reverse of it i.e. from the given number to 1.
For Example. Factorial of 4 => 1*2*3*4 = 24 or 4*3*2*1=24.
Solution:
import java.util.*; class Factorial_Do_While_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int c = n; System.out.print(c); int i = n-1; do { c = c*i; System.out.print("*" + i); i--; } while(i > 0); System.out.print("=" + c); } }
Output: Input a number: 4 4*3*2*1=24
Q5. Write a program to input a number and check whether it is a prime number or not using for() loop.
[A number is said to be prime if the number has only two factors i.e. the number is divisible by one and itself and no other factor or a number is divisible by 1 and itself and not divisible by any other factor of it.]
For Example: 2,3,5,7...etc are prime numbers.
Solution:
import java.util.*; class PrimeNumber_For_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int count = 0; for(int i = 1;i <= n;i++) { if(n%i == 0) { count++; } } if(count == 2) { System.out.println(n + " is a Prime Number"); } else { System.out.println(n + " is not a Prime Number"); } } }
Output: Input a number: 5 5 is a Prime Number
Q6. Write a program to input a number and check whether it is a prime number or not using while() loop.
[A number is said to be prime if the number has only two factors i.e. the number is divisible by one and itself and no other factor or a number is divisible by 1 and itself and not divisible by any other factor of it.]
For Example: 2,3,5,7...etc are prime numbers.
Solution:
import java.util.*; class PrimeNumber_While_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int i = 1,count = 0; while(i <= n) { if(n%i == 0) { count++; } i++; } if(count == 2) { System.out.println(n + " is a Prime Number"); } else { System.out.println(n + " is not a Prime Number"); } } }
Output: Input a number: 7 7 is a Prime Number
Q7. Write a program to input an integer. Print whether the integer is an Armstrong number or not using for() loop.
[If the original number and sum of cubes of all its digit are same then the number is Armstring.]
For Example: 135 is an Armstrong Number, as 1*1*1+3*3*3+5*5*5=135.
Solution:
import java.util.*; class ArmstrongNumber_For_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int reminder,sum = 0; for(int i = n;i > 0;i = i/10) { reminder = i%10; sum = sum+reminder*reminder*reminder; } if(sum == n) { System.out.println(n + " is an Armstrong Number"); } else { System.out.println(n + " is not an Armstrong Number"); } } }
Output: Input a number: 153 153 is an Armstrong Number
Q8. Write a program to input an integer. Print whether the integer is an Armstrong number or not using while() loop.
[If the original number and sum of cubes of all its digit are same then the number is Armstring.]
For Example: 135 is an Armstrong Number, as 1*1*1+3*3*3+5*5*5=135.
Solution:
import java.util.*; class ArmstrongNumber_While_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int i = n,reminder,sum = 0; while(i > 0) { reminder = i%10; sum = sum+reminder*reminder*reminder; i = i/10; } if(sum == n) { System.out.println(n + " is an Armstrong Number"); } else { System.out.println(n + " is not an Armstrong Number"); } } }
Output: Input a number: 153 153 is an Armstrong Number
Q9. Write a program to input an integer. Print whether the integer is a Palindrome number or not using for() loop.
[If the original number and its reverse are same then number is Palindrome.]
For Example: 151 and its reverse is 151, So 151 is a Palindrome number.
Solution:
import java.util.*; class PalindromeNumber_For_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int reminder,sum = 0; for(int i = n;i > 0;i = i/10) { reminder = i%10; sum = sum*10+reminder; } if(n == sum) { System.out.println(n + " is a Palindrome Number"); } else { System.out.println(n + " is not a Palindrome Number"); } } }
Output: Input a number: 151 151 is a Palindrome Number
Q10. Write a program to input an integer. Print whether the integer is a Palindrome number or not using while() loop.
[If the original number and its reverse are same then number is Palindrome.]
For Example: 151 and its reverse is 151, So 151 is a Palindrome number.
Solution:
import java.util.*; class PalindromeNumber_While_Loop { public void main() { Scanner sc = new Scanner(System.in); System.out.println("Input a number: "); int n = sc.nextInt(); int i = n,reminder,sum = 0; while(i > 0) { reminder = i%10; sum = sum*10+reminder; i = i/10; } if(n == sum) { System.out.println(n + " is a Palindrome Number"); } else { System.out.println(n + " is not a Palindrome Number"); } } }
Output: Input a number: 151 151 is a Palindrome Number
So that is it for the for() loop, while() loop and do-while() loop programs. Hope you find it useful and if you have any query do comment in the comment box and I will try to solve your query.
Thank You.
10 Basics Programs using for(), while() and do-while() (Java)
Reviewed by Get2Know
on
September 15, 2020
Rating:

No comments: