Create a C++ program that utilizes nested for loop that asks the user for a numb
ID: 3681188 • Letter: C
Question
Create a C++ program that utilizes nested for loop that asks the user for a number then the program should compile a list of all natural numbers from 2 to the number the user keyed in marking with a star the prime numbers only.
Steps how the program should go:
1. Asks the user for number.
2. List all numbers from 2 to the user typed number.
3. Mark the Prime number with a stars leaving the composite numbers not labeled.
4. Stars should increment keeping track of the prime numbers found previously.
5. Make sure to check your program for values over 180.
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int i,j,n,flag,cnt=0;
cout<<"Please enter a number:";
cin>>n;
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
if(flag==0)
{
cnt++;
cout<<i<<" ";
for(int k=0;k<cnt;k++)
cout<<"*";
}
else
cout<<i;
cout<<endl;
}
return 0;
}