Loops statements
loops are used to execute a block of code repeatedly until a certain condition is met.
- Types of Loops statements :
- for loop
- while loop
- do-while loop
- for loop :
- The for loop is the most commonly used loop in Java.
- It has a control structure that allows you to initialize a variable, specify a condition for termination, and define how the variable is updated after each iteration.
#parts of for loop:
- Initialization : It is the initial condition which is executed once when the loop starts. It has a control structure that allows you to initialize a variable.
- Condition : The condition statement in loops is a Boolean expression that evaluates to either true or false. This condition is used to determine whether the loop should continue executing its body or terminate. If the condition evaluates to true, the loop continues to execute; if it evaluates to false, the loop stops.
- Increment/Decrement : It increments or decrements the variable value. It is an optional condition.
#Syntax :
for(initialization ; condition ; increment/decrement){
Statements;
}
#example:
for(int i=0 ; i<10 ; i++){
System.out.print(" "+i);//" " for gap in between digits
}
*OutPut : 0 1 2 3 4 5 6 7 8 9
Flowchart
- while loop:
- A while loop is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition is true.
initialization;
while(condition){
statements;
}
#flow chart : #example :
- The condition is a boolean expression that is evaluated before each iteration of the loop.
- If the condition is true, the code block inside the loop is executed.
- After the code block executes, the program goes back to the beginning of the loop and re-evaluates the condition.
- If the condition is still true, the loop continues; otherwise, if the condition becomes false, the loop terminates, and the program continues with the code after the loop.
int i=0;
while(i<10){
System.out.print(" "+i);//" " for gap in between digits
i++ ;
}
*OutPut : 0 1 2 3 4 5 6 7 8 9
- do-while loop :
- do-while loop is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition remains true
#Syntax : initialization; do{ statements; }while(condition);#flow chart :- The code block inside the do is executed first.
- After the code block is executed, the condition specified in the while is evaluated.
- If the condition is true, the loop continues, and the code block is executed again.
- If the condition is false, the loop terminates, and the program continues with the next statement after the do-while loop.
#example : int i=0; do{ System.out.print(" "+i);//" " for gap in between digits i++ ; }while(i<10)
- Questions
#For loop Questions: 1.WAP to print "hello world" ten times using for loop.
public class Question_answer{ public static void main(String[]args){ for(int i=1 ; i<=10 ; i++){ System.out.println("Hello World"); } }}
2.WAP to print 10 to 1 numbers using for loop.
public class Question_answer{ public static void main(String[]args){ for(int i=10 ; i>=1 ; i--){ System.out.print(" "+ i); } }}3.WAP to print Multiplication table of given number using for loop.
public class Question_answer{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); System.out.println("------enter a number to print table------"); int n =sc.nextInt(); for(int i=1 ; i<=10 ; i++){ System.out.println(n + " * " + i + " = " +n*i); } } }
4.WAP to find the factorial of a given number using for loop.
public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("enter the number"); int number = sc.nextInt();
int fact = 1 ;
for(int i = 1 ; i<=number ; i++){ fact = fact * i; } System.out.print("Answer = " + fact); }
5.Write a program in Java to display n terms of natural numbers and their sum.public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); int n = sc.nextInt(); int sum = 0; for(int i=1 ; i<=n ;i++){
System.out.print( " "+i);
sum = sum+i; } System.out.println("sum is " + sum); }6.Write a program in Java to read given numbers from the keyboard and find their sum and average.public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); int count = sc.nextInt();//5 int sum = 0; for(int i=1 ; i<=count ;i++){
System.out.println( " "+i);
sum = sum+i; } System.out.println("sum is " + sum); int average = sum/count;// 15/5=3 System.out.println("average is " + average);}
7.Write a program in Java to display the cube of the number up to an integer.public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt();
for(int i=1 ; i<=n ; i++){ int cube = i*i*i; System.out.println(i + " cube is " + cube); } }8.Write a program in Java to display the multiplier table vertically from 1 to n.public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("enter the number of tables"); int n = sc.nextInt();
for(int i=1 ; i<=n ; i++){ for(int j=1 ; j<=10 ; j++){ int a = i*j;
System.out.println(i +" * "+ j +" = "+ a ); } System.out.println(" "); } }
#while loop Questions:
Same questions preformed my While loop.
1.WAP to print Multiplication table of given number using while loop.public static void main(String[] args) { int n = 2 ;
int i = 1; while(i<=10){ int a = n * i; i++; System.out.println(a); } }2.WAP to find the factorial of a given number using while loop.public static void main(String[] args) { int fact = 1; int n = 5 ;
int i = 1; while(i<=n){ fact = fact * i; i++; } System.out.println(fact); }3.Write a program in Java to display the multiplier table vertically from 1 to n.public static void main(String[] args) { int n = 10; int i = 1 ; while(i<=n){//11<=10 int j = 1; while(j<=10){//1<=10 int a = i * j ; //a = 2 * 1 = 2 j++;// System.out.println(a);// 1 } i++;//11 System.out.println(" "); }
}
#do while loop Question :
1.WAP to print 10 to 1 numbers using do-while loop. public static void main(String[] args) { int i = 10; do{ System.out.println(i); i--; }while(i>=1); }
2.Write a program in Java to print fibonacci series of the give length by do - while loop. public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = 0 , y = 1 , i = 0 ; System.out.println(x +"\n" +y); do { int a = x + y; System.out.println(a); x = y ; y = a ; i++; } while (i <= n-2); }
- do-while loop is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition remains true
#Syntax :
initialization;
do{
statements;
}while(condition);
#flow chart :
- The code block inside the do is executed first.
- After the code block is executed, the condition specified in the while is evaluated.
- If the condition is true, the loop continues, and the code block is executed again.
- If the condition is false, the loop terminates, and the program continues with the next statement after the do-while loop.
#example :
int i=0;
do{
System.out.print(" "+i);//" " for gap in between digits
i++ ;
}while(i<10)
- Questions
#For loop Questions:
1.WAP to print "hello world" ten times using for loop.
public class Question_answer{
public static void main(String[]args){
for(int i=1 ; i<=10 ; i++){
System.out.println("Hello World");
}
}
}
public class Question_answer{
public static void main(String[]args){
for(int i=10 ; i>=1 ; i--){
System.out.print(" "+ i);
}
}
}
public class Question_answer{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("------enter a number to print table------");
int n =sc.nextInt();
for(int i=1 ; i<=10 ; i++){
System.out.println(n + " * " + i + " = " +n*i);
}
}
}
4.WAP to find the factorial of a given number using for loop.
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("enter the number");
int number = sc.nextInt();
int fact = 1 ;
for(int i = 1 ; i<=number ; i++){
fact = fact * i;
}
System.out.print("Answer = " + fact);
}
5.Write a program in Java to display n terms of natural numbers and their sum.
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int sum = 0;
for(int i=1 ; i<=n ;i++){
System.out.print( " "+i);
sum = sum+i;
}
System.out.println("sum is " + sum);
}
6.Write a program in Java to read given numbers from the keyboard and find their sum and average.
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int count = sc.nextInt();//5
int sum = 0;
for(int i=1 ; i<=count ;i++){
System.out.println( " "+i);
sum = sum+i;
}
System.out.println("sum is " + sum);
int average = sum/count;// 15/5=3
System.out.println("average is " + average);
}
7.Write a program in Java to display the cube of the number up to an integer.
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
for(int i=1 ; i<=n ; i++){
int cube = i*i*i;
System.out.println(i + " cube is " + cube);
}
}
8.Write a program in Java to display the multiplier table vertically from 1 to n.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the number of tables");
int n = sc.nextInt();
for(int i=1 ; i<=n ; i++){
for(int j=1 ; j<=10 ; j++){
int a = i*j;
System.out.println(i +" * "+ j +" = "+ a );
}
System.out.println(" ");
}
}
#while loop Questions:
Same questions preformed my While loop.
1.WAP to print Multiplication table of given number using while loop.
public static void main(String[] args) {
int n = 2 ;
int i = 1;
while(i<=10){
int a = n * i;
i++;
System.out.println(a);
}
}
2.WAP to find the factorial of a given number using while loop.
public static void main(String[] args) {
int fact = 1;
int n = 5 ;
int i = 1;
while(i<=n){
fact = fact * i;
i++;
}
System.out.println(fact);
}
3.Write a program in Java to display the multiplier table vertically from 1 to n.
public static void main(String[] args) {
int n = 10;
int i = 1 ;
while(i<=n){//11<=10
int j = 1;
while(j<=10){//1<=10
int a = i * j ; //a = 2 * 1 = 2
j++;//
System.out.println(a);// 1
}
i++;//11
System.out.println(" ");
}
}
#do while loop Question :
1.WAP to print 10 to 1 numbers using do-while loop.
public static void main(String[] args) {
int i = 10;
do{
System.out.println(i);
i--;
}while(i>=1);
}
2.Write a program in Java to print fibonacci series of the give length by do - while loop.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = 0 , y = 1 , i = 0 ;
System.out.println(x +"\n" +y);
do {
int a = x + y;
System.out.println(a);
x = y ;
y = a ;
i++;
} while (i <= n-2);
}
Comments
Post a Comment