//prime numbers in given range 0 and 100
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count;
for(i=1;i<=100;i++)
{
if(i<=3)
{
printf("%d\n",i);
}
else
{
count=0;
for(j=2;j<=i-1;j++)
{
if(i%j==0)
{
count=1;
}
}
}
if(count==0)
{
printf("%d\n",i);
}
}
getch();
}
----------------------------------------------------------------------------------------------------------------------------------
logics in mind:-
---------------
->we have to display prime numbers in given range 0 and 100
->number s are 1,2,3,,5,7,....
we can see that first three numbers are 1,2,3 prime so for them we have used loop with 'if'
-> as the number(i) exceeds 3 it transfers the control to next loop where that number is divided from 2 to i-1. We are not taking 1 because we want to know , is there any number which can divide with remainder '0' or not.
->We want to know here , how many numbers are there which can divide completely with remainder '0'.
->To know that, we have used one variable 'count'. If it is divisible by by some other numbers then 'count' becomes '1'. otherwise it remains '0'.
->If the variable 'count' has same value '0' then we display that value using variable 'i'.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count;
for(i=1;i<=100;i++)
{
if(i<=3)
{
printf("%d\n",i);
}
else
{
count=0;
for(j=2;j<=i-1;j++)
{
if(i%j==0)
{
count=1;
}
}
}
if(count==0)
{
printf("%d\n",i);
}
}
getch();
}
----------------------------------------------------------------------------------------------------------------------------------
logics in mind:-
---------------
->we have to display prime numbers in given range 0 and 100
->number s are 1,2,3,,5,7,....
we can see that first three numbers are 1,2,3 prime so for them we have used loop with 'if'
-> as the number(i) exceeds 3 it transfers the control to next loop where that number is divided from 2 to i-1. We are not taking 1 because we want to know , is there any number which can divide with remainder '0' or not.
->We want to know here , how many numbers are there which can divide completely with remainder '0'.
->To know that, we have used one variable 'count'. If it is divisible by by some other numbers then 'count' becomes '1'. otherwise it remains '0'.
->If the variable 'count' has same value '0' then we display that value using variable 'i'.
No comments:
Post a Comment