Table of Contents
generally, in programming languages, the looping statement is used to perform the block of code until the condition is satisfied.
C program has 3 looping statement
first the initialization statements are executed only once
Then the test expression is evaluated. When the test expression becomes false. The control comes out from the loop and wile loop is terminated.
However, when the test expression evaluated to true, statements inside the body of while loop are executed.
finally, the updating statement(increment/decrement) is updated
then again the test expression is evaluated
This process happening on until the test expression becomes false.
This program displays natural numbers from 1 to 9
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,i=1,sum=0;
printf("Enter a number for find sum\n");
scanf("%d",&num);
while(i<=num){
sum+=i; //sum=sum+i
i++;
}
printf("Sum of natural numbers 1 to %d is: %d",num,sum);
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter a number for find sum 100 sum of natural numbers 1 to 100 is: 5050
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
int j=0;
while(i<=10)
{
printf(“*”);
printf(“n”);
j=0;
while(j<=10)
{
printf(“*”);
j++;
}
i++;
}
}
we can print a rectangular pattren using two while loop at the above program
When the above code is executed, it produces the following result
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.