#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();//clearing the screen
int n,x=2,count=0;//Here count is initialised to 0,if it is not prime it remains the same,else it will be equal to 1.You will understand this as you go down
//A number is a prime number if it is not divisible by any other number from 2 and the number before it.
printf("Enter a number : ");
scanf("%d",&n);
while(x<n)//As this checking process should continue till the number just preceding it
{
if(n%x==0)//checking if the number n is divisible by x or not
{
count++;//IF divisible,there is no meaning in continuing,So we are coming out of the loop by incrementing the variable "count"
break;
}
else
x++;
}
if(count==0)
{
printf("%d is a prime number",n);
return 0;//Here if number is prime,There is no need to go further and execute till end,To reduce time complexity ,We will write a return statement to stop executing the code.
}
printf("%d is not a prime number",n);
return 0;
}
用についてa linkです。 – SurvivalMachine
そこにループを記述することを忘れていませんか? 'n'と' x'も初期化されません。 'for 'ループと、一般的な基本Cについて読むことができます。あなたのコードは意味がありませんが、何らかのループが欲しいと思いますが、どうやって何がわからないのでしょうか。言い換えれば、**いくつかの基本的なCを最初に学ぶ**。 –