+ 2
Can someone please write a code for prime numbers using flag?
6 odpowiedzi
+ 6
#include <iostream>
using namespace std;
int main()
{
int n, i, flag;
flag = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{ if(n % i == 0)
{ flag = 0;
break; }
}
if (flag==1)
cout << "This is a prime number";
else cout << "This is not a prime number";
return 0; }
+ 4
#include<iostream>
using namespace std;
int main()
{
int number,count=0;
cout<<"Enter number to check its Prime or Not : ";
cin>>number;
for(int a=1;a<=number;a++)
if(number%a==0)
count++;
if(count==2)
cout<<"Its Prime Number \n";
else
cout<<"Its not a Prime Number\n";
return 0;
}
just go through the course here you will learn more and just code and practice 😊
+ 1
Thank you Ankit
Thank you Gawen
+ 1
.
0
1000