while()


while():-

'while' is a another conditional statement where we can make a condition with the while loop, basically while is a looping statement like for loop. 'while' is also a looping statement where we need to do some repeated job. We can use the while loop where while loop have just one part which is conditional pert.

 SYNTAX:

while(condition)
{
statement 1
statement 2
statement n
}


EXAMPLE:-

1. QUESTION:  Display "Harvard University" 20 times.

CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Count = 20;
while(Count)
{
printf("\nHarvard University");
Count--;
}
getch();
return 0;
}

OUTPUT:-



DISCUSSION:- 'while' is also a looping statement where we define a loop with a condition and until the condition is false the statement is true and execute the job which is defined in the while loop. The looping statement is terminates when the condition gets false. In the program we initialize 20 in 'Count' variable, while(Count) means the condition is true until the value of count is zero(0)  and every positive value of Count the condition is true.

2. QUESTION: Write a program to display the result of summation of all the numbers between two given numbers.

CODE:-
# include <stdio.h>
# include <conio.h>
int main()
{
int Num1,Num2,Sum;
printf("Enter your Minimum number");
scanf("%d",&Num1);
printf("Enter your Maximum number");
scanf("%d",&Num2);
Sum = Num1;
while(Num1<=Num2)
{
Sum +=Num1;
Num1++;
}
printf("\nThe result is %d",Sum);
getch();
return 0;


}


OUTPUT:-



DISCUSSION: In this program at-first we enter two numbers one is minimum and another is maximum, and get the result of summation  all the numbers between them. In this program our condition is while(Num1<=Num2) which means 'Num1' is a variable consider as minimum value in this program and 'Num2' is also a integer type of variable which consider as maximum value in this program and we increase (1) the value of Num1 by every iteration and when the condition get false and exit from the loop, then we get our required result.



3. QUESTION: Write a program to find the given number is prime or not.

CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Num,i=2,Flag=0;
printf("Enter your number: ");
scanf("%d",& Num);
while(i<Num)
{
if(Num%i==0)
{
Flag = 1;
break;
}
i++;
}
if(Flag == 0)
printf("PRIME NUMBER");
else
printf("NOT PRIME NUMBER");
getch();
return 0;
}

OUTPUT:-




DISCUSSION: Prime number means a number which is only divisible by 1 or itself. In the program we find the enter number is prime or not. At first we have to check that the entered number is divisible by 2 or the entered number less than 1.



4.  QUESTION: Write a program to check the numbers is prime or not between the two given number.

CODE:-
# include <stdio.h>
# include <conio.h>
int main()
{
int Num1,Num2,i=2,Flag=0;
printf("Enter your number: ");
scanf("%d",& Num1);
printf("Enter your number: ");
scanf("%d",& Num2);
while(Num1<Num2)
{
while(i<Num1)
{
if(Num1%i==0)
{
Flag = 1;
break;
}
i++;
}
if(Flag == 0)
printf("\n%d = PRIME NUMBER",Num1);
else
printf("\n%d = NOT PRIME NUMBER",Num1);
Num1++;
Flag = 0;i=2;
}
getch();
return 0;
}


OUTPUT:-


DISCUSSION: In this program we are checking all the numbers between the two given numbers is prime or not. In this program we just enter a minimum value and a maximum value and check all the numbers between them is prime or not.


Translate