L9: Loop-I (while)
Q1. Write a program to print the numbers from 1 to 10.
class Test {public static void main(String[ ] args) {
int num=1;
while(num<=10) {
System.out.println(num);
num=num+1;
}
}
}
Q2. Write a program to find the sum of first n natural numbers.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n=ip.nextInt();
int i=1;
int sum=0;
while(i<=n) {
sum=sum+i;
i++;
}
System.out.println("The sum is: "+sum);
}
}
Q3. Write a program to find the product of first n natural numbers.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
long product=1;
int i=1;
while(i<=n) {
product=product*i;
i++;
}
System.out.println ("The product is: "+product);
}
}
Q4. Write a program that reads two numbers and prints all the natural numbers present within the two input numbers.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter two numbers: ");
int n1=ip.nextInt();
int n2=ip.nextInt();
n1++;
System.out.println("The natural numbers are:");
while(n1 < n2) {
System.out.print(n1 +"\t");
n1++;
}
}
}
Q5. Write a program to find the even numbers within a range.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter two numbers: ");
int i=ip.nextInt();
int j=ip.nextInt();
i++;
System.out.println("The even numbers in the range are:");
while(i < j) {
if (i % 2==0)
System.out.print(i +"\t");
i++;
}
}
}
Q6. Write a program that reads a number and find the sum of all even numbers between 1 and the given number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int sum=0;
int i=2;
while (i < n) {
sum=sum+i;
i=i+2;
}
System.out.println("The sum is: "+sum);
}
}
Q7. Write a program to count the number of digits of a given number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int count=0;
while(n > 0) {
count++;
n=n/10;
}
System.out.println("Number of digits= "+count);
}
}
Q8. Write a program to find sum of digits of a given number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int sum=0;
int rem;
while(n > 0) {
rem=n%10;
sum=sum+rem;
n=n/10;
}
System.out.println("Sum of digits= "+sum);
}
}
Q9. Write a program to reverse a given number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int rev=0;
while(n > 0) {
int rem=n%10;
rev=rev*10+rem;
n=n/10;
}
System.out.println("Reverse number: "+rev);
}
}
In this program, we have declared the variable rev outside the loop because if we declare this inside the body of the loop then the previous value of the variable is going to be lost. But, in this program we need the previous value of rev during its updation to the new value. On the otherhand, we have declared the variable rem inside the body of the loop because it is not required to keep track the old values of rem.
Q10. Write a program to check whether a given number is a palindrome or not. A number is said to be palindrome if the reverse of that number is equal to the number e.g. 575, 37873, etc.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int temp=n;
int rev=0;
while(n > 0) {
int rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(rev==temp)
System.out.println("The input number is a palindrome");
else
System.out.println("The input number is not a palindrome");
}
}
Here, the value of the input number n becomes zero when the loop terminates. Hence, to compare the value of rev with the input number n, we need a temporary variable temp to store the value of n.
Q11. Write a program to find the maximum digit of a number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int sum=0;
int max=0;
while(n>0){
int rem=n%10;
if(rem>max)
max=rem;
n=n/10;
}
System.out.println("Maximum digit is: "+max);
}
}
Q12. Write a program to print the first n Fibonacci numbers e.g. 0 1 1 2 3 5 8 13 21 34 .....
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n=ip.nextInt();
int first=0;
int second=1;
System.out.println("Fibonacci numbers are:");
System.out.print(first +"\t"); //print first number in the sequence
System.out.print(second +"\t"); //print second number in the sequence
while(n>2) {
/*loop-continuation condition is n>2 because we have printed
first two numbers of the sequence outside the loop*/
int next=first+second;
System.out.print(next +"\t");
first=second;
second=next;
n--;
}
}
}
Q13. Write a program to find the GCD of two numbers.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a=ip.nextInt();
int b=ip.nextInt();
while(b>0) {
int temp=a%b;
a=b;
b=temp;
}
System.out.println("GCD is: "+a);
}
}
Q14. Write a program to check whether a given number is a prime or not.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
int i=2;
boolean isPrime=true;
while(i <= Math.sqrt(n)) {
if(n%i==0) {
isPrime=false;
break;
}
i++;
}
if(isPrime)
System.out.println("Prime");
else
System.out.println("Not a prime");
}
}
Q15. Discuss the use of break and continue statements.
When a break statement is executed in a loop (while, for, do-while) or in switch, it makes immediate exit from that statement. In other words, the execution of break statement terminates the current loop. e.g. the following code snippet prints 1, 2 because when the value of i becomes 3, the break statement gets executed and the loop gets terminated.for(int i=1;i<=5;i++) {
if(i==3)
break;
System.out.println(i);
}
The execution of continue statement inside a loop skips the execution of remaining statements of the current iteration and moves to the next iteration. In while and do-while the loop continuation condition is evaluated immediately after executing the continue statement. After continue statement in for loop the updation of loop control variable is executed and then the loop continuation condition is evaluated. e.g. the following code snippet prints 1, 2, 4, 5 because when the value of i becomes 3, the continue statement gets executed and the control moves to the next iteration without printing 3.
for(int i=1;i<=5;i++) {
if(i==3)
continue;
System.out.println(i);
}