What's new

Closed C++ For loop prime numbers from 1-50

Status
Not open for further replies.

Welcome Guest

Eternal Poster
Joined
Nov 29, 2015
Posts
1,000
Reaction
146
Points
509
Age
27
Mga kuys, pa code naman po ng problem na to please? Hindi ko siya masyado ma gets... Using for loop lng po ang gagamiting codes... E susubmit ko pa naman to mamaya, sana may maka tulong. Thanks in advance po.
 

Attachments

#include<iostream>
using namespace std;
int main()
{
int c;
int g;
cout<<"Enter Number: ";
cin>>c;

g=c%2;
if(g!=0)
cout<<c<<" is a prime number";
else
cout<<c<<" is not a prime number";

}





Ts try mo to hindi nga lang forloop pero simplify codes lang hahha
 
#include<iostream>
using namespace std;
int main()
{
int c;
int g;
cout<<"Enter Number: ";
cin>>c;

g=c%2;
if(g!=0)
cout<<c<<" is a prime number";
else
cout<<c<<" is not a prime number";

}





Ts try mo to hindi nga lang forloop pero simplify codes lang hahha
This is wrong. Your code tests a number if it is even or odd but not if it's a prime number.
 
pa try nito paps
C++:
#include<iostream>
using namespace std;
bool isPrime(int number){
    for(int i = 2 ; i < number ; i++){
        if(number % i == 0)
            return false;
    }
    return true;
}
int main(void){
    cout<<"Prime Numbers from 1 to 50"<<endl;
    for(int i = 1 ; i <= 50 ; i++)
        if(isPrime(i))
            cout<<i<<" is a prime number"<<endl;
        else
            cout<<i<<" is not a prime number"<<endl;
}
 
Status
Not open for further replies.
Back
Top