Tuesday 7 October 2014

WAP to determine whether the Number is Prime or Not by using while and if Statement

C break Statement
Break Statement: 
There are many situations where we want to jump out of a loop instantly. 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. So Let us understand the break statement with the help of Program :-

Statement of C Program: This Program accepts a number and determined whether the number is Prime or not.

Prime Number:
A Prime number is one, which is divisible by 1 or itself.  Ex: 3 , 5 , 7 , 11 , 13 , 17 , 23 , 19 etc.

#include<stdio.h>
#include<conio.h>
void main()
{
int a , b ;
clrscr();

printf(" Enter the value of a\n");
scanf("%d" , &a);

b = 2;
while( b <= a-1 )
{
if(a % b == 0)
{
printf(" Entered Number is not Prime\n");
break;
}
b = b + 1;                                                   /*   b++     or    b += 1   */
}
if(b = a)
{
printf(" Prime Number\n");
}
getch();
}

Output1:
Enter the value of a
7
Prime Number

Output2:
Enter the value of a
9
Entered Number is not Prime

Explanation:
  • In the above Program, if the condition { i.e a % b } turns out to be zero, then the message " Entered number is not Prime " is printed and after that the break statement encountered which causes control out of while loop.
  • If the condition { i.e b == a } is true, then the message " Prime Number " is printed.

No comments:

Post a Comment