#include <iostream>
using namespace std;
int sumsq(int);
int sumcb(int);
int prime(int);
int main()
{
cout<<" Number SumSq SumCb PRIME "<<endl;
cout<<endl;
int numb;
int sum;
int sum1;
for(numb=5;numb<=49;numb+=2)
if(prime(numb)==1)
cout<<numb<<" "<<sumsq(numb)<<" "<<sumcb(numb)<<endl;
sum=sumsq(numb);
sum1=sumcb(numb);
system ("pause");
return 0;
}
int sumsq(int numb){
int yoyo;
int sum =0;
for(yoyo=1;yoyo<=numb;yoyo++)
sum+=yoyo*yoyo;
return sum;
}
int sumcb(int numb){
int val;
int sum1 = 0;
for(val=1;val<=numb;val++)
sum1+=val*val*val;
return sum1;
}
int prime(int numb){
int p;
for(p=2;p<=numb;p++)
if(numb%1==0){
cout<<" "<<"*"<<endl;
return 1;
}
else{
return 0;
}
}
Printing "*" for prime numbers between 5 and 49
Page 1 of 14 Replies - 2043 Views - Last Post: 25 October 2007 - 11:36 AM
#1
Printing "*" for prime numbers between 5 and 49
Posted 23 October 2007 - 08:52 PM
I'm a freshmen in college and I'm taking an intoductory c++ course. My assignment asked me to print out all odd numbers from 5 to 49 and to then write a couple of functions. Anyway it asks for a function that will print out a " * " if the number is prime. I did that and a " * " prints our for all the numbers which is false because some of the numbers aren't prime. What did I do wrong and I will appreciate any hints. Also how do u calculate an avg in c++? Thank You
Replies To: Printing "*" for prime numbers between 5 and 49
#2
Re: Printing "*" for prime numbers between 5 and 49
Posted 23 October 2007 - 09:41 PM
One of the problems with your prime() function is that any value modulus one is equal to zero. Perhaps you meant to take the number modulo p instead?
Also, since you have a return statement in both of the alternatives of your if-else structure in that function, the loop will not work - it will return a value and exit the function on the first round through the loop. THe following may work a little better:
Once the loop is entered, the if statement tests whether the number is evenly divisible by p. If it is, the number is not prime, and the function returns a zero and exits. If it is not, the loop continues. If the loop makes it all the way through to the end without finding a factor, the number is prime, so it outputs a * and returns one.
Notice that I have also enclosed the entire contents of the for loop in { }, and only run the loop up to numb/2 - you don't need to go all the way to numb, because any number greater than numb/2 cannot be a factor (since the corresponding factor would be less than 2).
I haven't looked over the rest of it in great detail, but that should at least give you a start.
Hope that helps,
-jjh
Also, since you have a return statement in both of the alternatives of your if-else structure in that function, the loop will not work - it will return a value and exit the function on the first round through the loop. THe following may work a little better:
int prime(int numb) {
int p;
for (p=2; p<=numb/2; p++) {
if (numb%p==0) {
return 0;
}
}
cout << "*" << endl;
return 1;
}
}
Once the loop is entered, the if statement tests whether the number is evenly divisible by p. If it is, the number is not prime, and the function returns a zero and exits. If it is not, the loop continues. If the loop makes it all the way through to the end without finding a factor, the number is prime, so it outputs a * and returns one.
Notice that I have also enclosed the entire contents of the for loop in { }, and only run the loop up to numb/2 - you don't need to go all the way to numb, because any number greater than numb/2 cannot be a factor (since the corresponding factor would be less than 2).
I haven't looked over the rest of it in great detail, but that should at least give you a start.
Hope that helps,
-jjh
#3
Re: Printing "*" for prime numbers between 5 and 49
Posted 24 October 2007 - 05:14 PM
This code works I tried it, but I need to print out all the odd numbers from 5 to 49 and then if the number is prime a star is suppose to come up. In your code only the prime numbers come out. Thanks alot I really appreciate the help. Ill try to see if I can fix it. 
EDIT: I dont know how to make the prime function print out all the numbers and only put a star where there is a prime number. Can some one please help me?
EDIT: I dont know how to make the prime function print out all the numbers and only put a star where there is a prime number. Can some one please help me?
This post has been edited by garc2541: 24 October 2007 - 08:41 PM
#4
Re: Printing "*" for prime numbers between 5 and 49
Posted 25 October 2007 - 05:39 AM
The prime function is not meant to do all the testing and printing...only to decide is a number is prime or not. You need to integrate it into the rest of your code, like so (pseudocode):
if prime(mynum) AND mynum is ODD and mynum is greater than 4 AND mynum is less than 50 then print * end if
#5
Re: Printing "*" for prime numbers between 5 and 49
Posted 25 October 2007 - 11:36 AM
garc2541, on 24 Oct, 2007 - 05:14 PM, said:
This code works I tried it, but I need to print out all the odd numbers from 5 to 49 and then if the number is prime a star is suppose to come up. In your code only the prime numbers come out. Thanks alot I really appreciate the help. Ill try to see if I can fix it. 
EDIT: I dont know how to make the prime function print out all the numbers and only put a star where there is a prime number. Can some one please help me?
EDIT: I dont know how to make the prime function print out all the numbers and only put a star where there is a prime number. Can some one please help me?
- You need a loop to run through all the numbers 5 - 49
- You need to run the prime function above on each number
- Inside your loop have an if/else statement that basically says
If (numIsPrime)
{
cout << " * ";
}
Else
{
cout << number;
}
Page 1 of 1

New Topic/Question
Reply



MultiQuote




|