🔒 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: 25
#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
  • 486
    Views
  • 4
    Participants
Last reply from:
Arcturus

Trending Topics

Online now

Members online
1,153
Guests online
1,026
Total visitors
2,179

Forum statistics

Threads
2,274,706
Posts
28,957,751
Members
1,233,405
Latest member
VEdge2025
Back
Top