URI Online Judge Solution 1165 Prime Number Using C, Python Programming Language.
A Prime Number is a number that is divisible only by 1 (one) and by itself. For example the number 7 is Prime, because it can be divided only by 1 and by 7.
Input
The input contains several test cases. The first contains the number of test cases N (1 ≤ N ≤ 100). Each one of the following N lines contains an integer X (1 < X ≤ 107), that can be or not a prime number.
Output
For each test case print the message “X eh primo” (X is prime) or “X nao eh primo” (X isn't prime) according with to above specification.
| Input Sample | Output Sample |
3 | 8 nao eh primo |
Solution Using C:
#include <stdio.h>
int main() {
int T,n,i,p;
scanf("%d",&T);
while(T--)
{
p=1;
scanf("%d",&n);
for(i=2; i<n; i++)
{
if(n%i==0)
p=0;
}
if(p)
printf("%d eh primo\n",n);
else
printf("%d nao eh primo\n",n);
}
return 0;
}
int main() {
int T,n,i,p;
scanf("%d",&T);
while(T--)
{
p=1;
scanf("%d",&n);
for(i=2; i<n; i++)
{
if(n%i==0)
p=0;
}
if(p)
printf("%d eh primo\n",n);
else
printf("%d nao eh primo\n",n);
}
return 0;
}


Comments
Post a Comment