Practice Program
Solution: through for loop
void main()
{
int i,j,n;
printf("Enter the number:");
scanf("%d",&n);
printf("Prime numbes from 1 to %d are:\n",n));
for(j=1;j<=n;j++)
{
for(i=2;i<i++) //for every value of j we are checking
{ //the divisibility of that number with 2 to (j-1)
if(j%i == 0)
{ //if j value is divisible by i value then
break; //we are terminating the loop with the break keyword
} //termination is the indication
//that given number is not an prime number
}//As above loop will complete normally,means it will not get terminated by break keyword
if(j == i) //then value of j will become equal to the value of i
printf("%d\t",j); //being both the value equal is the indication that the number is a Prime number,
//so we are checking that values are equal or not
// if they are equal then we are printing it
}
}
void main() { int i,j,n; printf("Enter the number:"); scanf("%d",&n); printf("Prime numbes from 1 to %d are:\n",n)); for(j=1;j<=n;j++) { for(i=2;i<i++) //for every value of j we are checking { //the divisibility of that number with 2 to (j-1) if(j%i == 0) { //if j value is divisible by i value then break; //we are terminating the loop with the break keyword } //termination is the indication //that given number is not an prime number }//As above loop will complete normally,means it will not get terminated by break keyword if(j == i) //then value of j will become equal to the value of i printf("%d\t",j); //being both the value equal is the indication that the number is a Prime number, //so we are checking that values are equal or not // if they are equal then we are printing it } }
Output1
Enter the number:20
Prime numbers from 1 to 20 are: 2 3 5 7 11 13 17 19
Prime numbers from 1 to 20 are: 2 3 5 7 11 13 17 19
Output2
Enter the number:10
Prime numbers from 1 to 10 are:
2 3 5 7
Prime numbers from 1 to 10 are:
2 3 5 7
Output3
Enter the number:30
Prime numbers from 1 to 10 are:
2 3 5 7 11 13 17 19 21 23 27 29
Prime numbers from 1 to 10 are:
2 3 5 7 11 13 17 19 21 23 27 29
Solution: from while loops
void main()
{
int i,j,n;
printf("Enter the number:");
scanf("%d",&n);
j = 1;
printf("Prime numbers from 1 to %d are:",n);
while(j<=n)
{
i=2;
while(i<j) //for every value of j we are checking
{ //the divisibility of that number with 2 to (j-1)
if(j%i == 0)
{ //if j value is divisible by i value then
break; //we are terminating the loop with the break keyword
} //termination is the indication
//that given number is not an prime number
i++;
}//As above loop will complete normally,means it will not get terminated by break keyword
if(j == i) //then value of j will become equal to the value of i
printf("%d\t",j); //being both the value equal is the indication that the number is a Prime number,
//so we are checking that values are equal or not
// if they are equal then we are printing it
j++;
}
}
void main() { int i,j,n; printf("Enter the number:"); scanf("%d",&n); j = 1; printf("Prime numbers from 1 to %d are:",n); while(j<=n) { i=2; while(i<j) //for every value of j we are checking { //the divisibility of that number with 2 to (j-1) if(j%i == 0) { //if j value is divisible by i value then break; //we are terminating the loop with the break keyword } //termination is the indication //that given number is not an prime number i++; }//As above loop will complete normally,means it will not get terminated by break keyword if(j == i) //then value of j will become equal to the value of i printf("%d\t",j); //being both the value equal is the indication that the number is a Prime number, //so we are checking that values are equal or not // if they are equal then we are printing it j++; } }
Solution: from do-while loops
void main()
{
int i,j,n;
printf("Enter the number:");
scanf("%d",&n);
j = 1;
printf("Prime numbers from 1 to %d are:",n);
do
{
i=2;
do//for every value of j we are checking
{ //the divisibility of that number with 2 to (j-1)
if(j%i == 0)
{ //if j value is divisible by i value then
break; //we are terminating the loop with the break keyword
} //termination is the indication
//that given number is not an prime number
i++;
} while(i<j);//As above loop will complete normally,means it will not get terminated by break keyword
if(j == i) //then value of j will become equal to the value of i
printf("%d\t",j); //being both the value equal is the indication that the number is a Prime number,
//so we are checking that values are equal or not
// if they are equal then we are printing it
j++;
}while(j<=n);
}
void main() { int i,j,n; printf("Enter the number:"); scanf("%d",&n); j = 1; printf("Prime numbers from 1 to %d are:",n); do { i=2; do//for every value of j we are checking { //the divisibility of that number with 2 to (j-1) if(j%i == 0) { //if j value is divisible by i value then break; //we are terminating the loop with the break keyword } //termination is the indication //that given number is not an prime number i++; } while(i<j);//As above loop will complete normally,means it will not get terminated by break keyword if(j == i) //then value of j will become equal to the value of i printf("%d\t",j); //being both the value equal is the indication that the number is a Prime number, //so we are checking that values are equal or not // if they are equal then we are printing it j++; }while(j<=n); }
defining a function 'is_prime' which will check whether a number is prime number or not.
int is_prime(int n)
{
int i,raise;
for(i=2;i<i++)
{
if(n%i == 0)
{
raise = 1;
break;
}
}
if(n == i)
{
return 1;
}
else
return 0;
}
int is_prime(int n) { int i,raise; for(i=2;i<i++) { if(n%i == 0) { raise = 1; break; } } if(n == i) { return 1; } else return 0; }
Using above define function 'is_prime()':
void main()
{
int n,result;
printf("Enter the Number");
scanf("%d",&n);
//calling function.
result = is_prime(n);//actual parameter.
if(result == 1)//if 'is_prime()' will return 1,
{ // then number is prime,otherwise not
printf(Prime Number);
}
else
printf("Not an Prime Number");
}
void main() { int n,result; printf("Enter the Number"); scanf("%d",&n); //calling function. result = is_prime(n);//actual parameter. if(result == 1)//if 'is_prime()' will return 1, { // then number is prime,otherwise not printf(Prime Number); } else printf("Not an Prime Number"); }
Output1
Enter the Number:407
Prime Number
Prime Number
Using above define function 'is_prime()':
void main()
{
int n,result;
printf("Enter the Number");
scanf("%d",&n);
//calling function.
result = is_prime(n);//actual parameter.
if(result == 1)//if 'is_prime()' will return 1,
{ // then number is prime,otherwise not
printf(Prime Number);
}
else
printf("Not an Prime Number");
}
void main() { int n,result; printf("Enter the Number"); scanf("%d",&n); //calling function. result = is_prime(n);//actual parameter. if(result == 1)//if 'is_prime()' will return 1, { // then number is prime,otherwise not printf(Prime Number); } else printf("Not an Prime Number"); }
Output2
Enter the Number:1
Not an Prime Number
Not an Prime Number
Using above define function 'is_prime()':
void main()
{
int n,result;
printf("Enter the Number");
scanf("%d",&n);
//calling function.
result = is_prime(n);//actual parameter.
if(result == 1)//if 'is_prime()' will return 1,
{ // then number is prime,otherwise not
printf(Prime Number);
}
else
printf("Not an Prime Number");
}
void main() { int n,result; printf("Enter the Number"); scanf("%d",&n); //calling function. result = is_prime(n);//actual parameter. if(result == 1)//if 'is_prime()' will return 1, { // then number is prime,otherwise not printf(Prime Number); } else printf("Not an Prime Number"); }
Output1
Enter the Number:7
Prime Number
Prime Number
0 Comments
Post a Comment