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

Status
Not open for further replies.

Welcome Guest

Established
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

  • prime.webp
    prime.webp
    14.4 KB · Views: 28
#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.

About this Thread

  • 6
    Replies
  • 509
    Views
  • 4
    Participants
Last reply from:
Arcturus

Trending Topics

Online now

Members online
808
Guests online
2,521
Total visitors
3,329

Forum statistics

Threads
2,292,843
Posts
29,079,761
Members
1,209,539
Latest member
Gintokiii
Back
Top