L10: Loop-II (for and do-while)
Q1. Write a program to print the numbers from 1 to 10.
class Test {public static void main(String[ ] args) {
for(int i=1;i<=10;i++)
System.out.println(i);
}
}
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 sum=0;
for(int i=1;i<=n;i++) {
sum=sum+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;
for(int i=1; i<=n; i++) {
product=product*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();
System.out.println("The natural numbers are:");
for (int i=n1+1; i < n2; i++) {
System.out.print(i +"\t");
}
}
}
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 a=ip.nextInt();
int b=ip.nextInt();
System.out.println("The even numbers in the range are:");
for(int i=a+1; i < b; i++) {
if (i % 2==0)
System.out.print(i +"\t");
}
}
}
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;
for (int i=2; i < n; i=i+2) {
sum=sum+i;
}
System.out.println("The sum is: "+sum);
}
}
Q7. 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();
boolean isPrime=true;
for(int i=2; i <= Math.sqrt(n); i++) {
if(n%i==0) {
isPrime=false;
break;
}
}
if(isPrime)
System.out.println("Prime");
else
System.out.println("Not a prime");
}
}
Q8. Write a program that repeatedly asks the user to input integers until the user enters 0. When the user inputs 0 the program do not ask for new integer inputs but displays the sum of previosly entered integers.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
int data;
int sum=0;
do{
System.out.print("Enter a number: ");
data=ip.nextInt();
sum=sum+data;
}while(data!=0);
System.out.println("The sum is "+sum);
}
}
Q9. Which loop is preferable in which condition ?
The for loop and while loop are known as pretest loops because in these loops the loop continuation condition is checked before executing the body of the loop. On the other hand, do-while loop is known as posttest loop because in do-while loop the loop continuation condition is checked after executing the body of the loop. The expressive power of for, while and do-while are same. Thus, each for loop can be represented in while loop and also, in do-while loop. Similarly, each while loop is equivalent to some for loop and also, some do-while loop. Each do-while loop is equivalent to some for loop and while loop.However, a for loop is preferable if the number of iterations is known in advance e.g. 10 times, 20 times, 100 times, n times, p times, etc. A while loop is suitable when the number of iterations is uncertain e.g. GCD program using Euclid's algorithm, program to count number of digits of a given integer, etc. A do-while loop is preferable when you want to execute body of the loop at least once, irrespective of the condition.