break statement
When we writing programming code, we often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
The keyword break, breaks the control only from the while in which it is placed.
Let's understand break with c program example:
#include<conio.h>
void main()
{
int i=5;
clrscr();
while(i>=1)
{
if(i==2)
{
printf("\nIt is equal to two!!");
break;
}
printf("%d\n",i);
i--;
}
getch();
}
Output:
5
4
3
It is equal to two!!
Thus, it is clear that a break statement takes the execution control out of the loop.
In above program, if we omitted break statement then what will be output? The answer is
5
4
3
It is equal to two!!
2
1
because when i's value is 2, condition will be satisfy and executed printf statement, after that compiler goes to next statement i.e. printf("%d",i); because loop do not terminate and it is run till the i value is not less than 1.
The keyword break, breaks the control only from the while in which it is placed.
Let's understand break with c program example:
/*demonstration of break statement through c program*/
#include<stdio.h>#include<conio.h>
void main()
{
int i=5;
clrscr();
while(i>=1)
{
if(i==2)
{
printf("\nIt is equal to two!!");
break;
}
printf("%d\n",i);
i--;
}
getch();
}
Output:
5
4
3
It is equal to two!!
Thus, it is clear that a break statement takes the execution control out of the loop.
In above program, if we omitted break statement then what will be output? The answer is
5
4
3
It is equal to two!!
2
1
because when i's value is 2, condition will be satisfy and executed printf statement, after that compiler goes to next statement i.e. printf("%d",i); because loop do not terminate and it is run till the i value is not less than 1.
search prime number
Q. Write a C program to find whether a number is prime or not.
Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.
Ans.
#include<stdio.h>
#include<stdio.h>
int main()
{
int x,num;
printf("Enter number : ");
scanf("%d",&num);
x=2;
while(x<=num-1)
{
if(num%x==0)
{
printf("Number is not prime!!");
break;
}
x++;
}
if(x==num)
printf("Number is prime!!");
getch();
getch();
return 0;
}
output of above program :
Enter number 7
Number is prime
Generate first n Prime number
Q. Write a C program to generate first n prime number C program.
Ans.
/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,num,t,div,count;
printf("How many prime number you want to print: ");
scanf("%d", &n);
printf("\n%d\t",2); /*2 is first prime number*/
count=1;
num=3;
while(count<n)
{
t=sqrt(num);
div=2;
while(div<=t)
{
if(num%div==0)
break;
div++;
}
if(div>t)
{
printf("%d\t",num);
count++;
}
num=num+2;
}
getch();
return 0;
}
/************Output************/
Ans.
/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,num,t,div,count;
printf("How many prime number you want to print: ");
scanf("%d", &n);
printf("\n%d\t",2); /*2 is first prime number*/
count=1;
num=3;
while(count<n)
{
t=sqrt(num);
div=2;
while(div<=t)
{
if(num%div==0)
break;
div++;
}
if(div>t)
{
printf("%d\t",num);
count++;
}
num=num+2;
}
getch();
return 0;
}
/************Output************/







