Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,522 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,949 people online right now. Registration is fast and FREE... Join Now!




C++ Question

2 Pages V  1 2 >  
Reply to this topicStart new topic

C++ Question, please help me..

gecheme
post 16 Apr, 2006 - 12:14 AM
Post #1


New D.I.C Head

*
Joined: 16 Apr, 2006
Posts: 22


My Contributions


Could you please help me in this program?



Write a C program that will;

•input a character and than a positive integer.

•check whether the inputted integer is a prime number or not if the
character input is ‘P’

•calculate the first n terms of the series 1+1/2+1/3+1/4+…+1/n where
n is the integer input if the character input is’S’.

•quit if ‘Q’ is inputted for the character.

Note: Your program should make the inputting and calculations until ‘Q’ is inputted.

Sample Run:

Enter (P)rime, (S)erie or (Q)uit:P
Enter a positive integer:4
4 is not a prime number
Enter (P)rime, (S)erie or (Q)uit:S
Enter a positive integer:2
Sum of first 2 elements:1.50
Enter (P)rime, (S)erie or (Q)uit:Q
User is offlineProfile CardPM

Go to the top of the page


Dark_Nexus
post 16 Apr, 2006 - 02:47 AM
Post #2


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


could you please show us what you've attempted thus far? we don't usually help members until they've attempted the problem at hand.
User is offlineProfile CardPM

Go to the top of the page

gecheme
post 16 Apr, 2006 - 02:57 AM
Post #3


New D.I.C Head

*
Joined: 16 Apr, 2006
Posts: 22


My Contributions


it is my homework.. i did these program. however it is working, there are still some problems too.. for example, i can't find prime number, i can't sum series, also i can't find a sample run as question asks.


here it is...

CODE

#include <stdio.h>

int main(void)
{
int n,j;
double sum;
char ch;

do
{

printf("Enter (P)rime, (S)erie or (Q)uit:\n");
scanf("%c",&ch);

switch(ch)
{

case 'P':

printf("Enter a positive integer:\n");
scanf("%d",&n);
{
if(n%2==1)

printf("%d is not a prime number\n",n);

else

printf("%d is a prime number\n",n);
}

break;

case 'S':

printf("Enter a positive integer:\n");

scanf("%d",&n);
sum=0;
for(j=1; j<=n; j++)
{
sum=sum+(1/j);
}

printf("Sum of first %d elements:%f\n",n,sum);

break;

}

}
while(ch!='Q');

getchar();
getchar();
return(0);
}


This post has been edited by Dark_Nexus: 16 Apr, 2006 - 11:45 PM
User is offlineProfile CardPM

Go to the top of the page

frog
post 16 Apr, 2006 - 08:04 AM
Post #4


unleashed

Group Icon
Joined: 26 Mar, 2006
Posts: 682



Dream Kudos: 500
My Contributions


u cant find a prime number because your loop finds if the number is even or odd.to find a prime number divide the inputed by
1 and so on . check if by dividing the number by them u get the remainder 0. keep a track of the no. of times u get 0 as a remainder.u run the loop till the number itself and break from the loop as soon as the count gets to 3. if the loop breaks at 3 then the no is composite else prime.

the code for the series is correct

try to use cin and cout from iostream instead of scanf and printf. u are using c++ not c. try to take some advantage of the classes gifted to u
User is offlineProfile CardPM

Go to the top of the page

gecheme
post 16 Apr, 2006 - 08:16 AM
Post #5


New D.I.C Head

*
Joined: 16 Apr, 2006
Posts: 22


My Contributions


i knew that i can't find a prime number.. so i wrote these to find even or odd.. actually, i wanted to know if teh programme is working or not.. but, i really don't now how to find prime number??

could you please help me? could you pls write down the part which includes prime number..

note: in the university we are not allowed to use cin and cout from iostream instead of scanf and printf. since, they didn't teach us..
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Apr, 2006 - 10:00 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


It would not help you very much if we were to simply provide you with code tha finds a prime number...but I will provide some pseudocode that should allow you to implement it:
CODE

FOR every value from zero to the number
  IF (number divided by the value has no remainder) AND is value is not 1 AND value is not the number THEN
  number is not prime
  END IF
END FOR

If the condition is met at ay point, the number is not a prime. Think modulus operator.
User is offlineProfile CardPM

Go to the top of the page

gecheme
post 17 Apr, 2006 - 12:03 AM
Post #7


New D.I.C Head

*
Joined: 16 Apr, 2006
Posts: 22


My Contributions


i wrote thsi one, however it is not working... Also series part does not give the result..

CODE

for(i=2; i<=n; i++)
{

if(n%i==0)

m=1;

else if(n%i!=0)

m=0;


}


if(m==1)

printf("\n%d is not a prime number",n);

else if(m==0)
printf("%\nd is a prime number\n",n);


please someone help me... sad3.gif
User is offlineProfile CardPM

Go to the top of the page

asrith143
post 17 Apr, 2006 - 04:13 AM
Post #8


New D.I.C Head

*
Joined: 17 Apr, 2006
Posts: 4


My Contributions


Ur code i.e calculating Prime no is wrong. Caz..

QUOTE

FOR every value from zero to the number
  IF (number divided by the value has no remainder) AND is value is not 1 AND value is not the number THEN
  number is not prime
  END IF
END FOR


the above must be coded like...

CODE

m=0;
for(i=2;i<n;i++)
{
        if(n%i==0)
        {
           m=1;
           break;
         }
}

if(m==1)

printf("\n%d is not a prime number",n);

else if(m==0)
printf("%\nd is a prime number\n",n);


I think U got it....

This post has been edited by Dark_Nexus: 18 Apr, 2006 - 11:43 PM
User is offlineProfile CardPM

Go to the top of the page

asrith143
post 17 Apr, 2006 - 04:22 AM
Post #9


New D.I.C Head

*
Joined: 17 Apr, 2006
Posts: 4


My Contributions


calculating series ...

sum=sum+(1/j); // this Ur calculation..

But j is an int type...
1/j returns or evaluated as integer devision.. i.e. 1/j always 0. if J!=1;
1/j= 1 if j=1;

So in Ur calculation J runs 4m 1. ie. always sum=1;

So. change it as... sum=sum+(float)1/j;

Thnx...
User is offlineProfile CardPM

Go to the top of the page

gecheme
post 17 Apr, 2006 - 01:01 PM
Post #10


New D.I.C Head

*
Joined: 16 Apr, 2006
Posts: 22


My Contributions


thank you all very much.. i made this one..

CODE

#include <stdio.h>

int main(void)
{
int m,n,i,j;
double sum;
char ch;

do
{

printf("Enter (P)rime, (S)erie or (Q)uit:");
scanf("%c",&ch);
getchar();
switch(ch)
{

case 'P':
case 'p':

printf("Enter a positive integer:");
scanf("%d",&n);getchar();

m=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
m=1;

}
}

if(m==1)

printf("%d is not a prime number\n",n);

else if(m==0)
printf("%d is a prime number\n",n);
break;

case 'S':
case 's':

printf("Enter a positive integer:");

scanf("%d",&n);

sum=0;

for(j=1; j<=n; j++)
{
sum=sum+(1.0/j);
}

printf("Sum of first %d elements:%f\n",n,sum);

break;

}

}
while(ch!='Q' && ch!='q');

getchar();
getchar();
return(0);
}
User is offlineProfile CardPM

Go to the top of the page

frog
post 18 Apr, 2006 - 12:20 AM
Post #11


unleashed

Group Icon
Joined: 26 Mar, 2006
Posts: 682



Dream Kudos: 500
My Contributions


nice coding .i m impressed much better than last time biggrin.gif

u have taken care of the possibility that the user may enter choice in upper case or lower case.good programming habit.


in the loop u where check if the no. is prime or not u can add a break statement after m=1.that would reduce the no. of comparisions

rest ur code is good
User is offlineProfile CardPM

Go to the top of the page

burketo
post 16 Mar, 2007 - 08:02 AM
Post #12


New D.I.C Head

*
Joined: 16 Mar, 2007
Posts: 1


My Contributions


does anybody know a more efficient way of doing this?

i wrote a similar program to that shown but the question makes a point of asking for an efficient method and i haven't a clue of any other way to find them.

what i mean is that it asks why i think it is efficient and to compare it to other methods

This post has been edited by burketo: 16 Mar, 2007 - 08:46 AM
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/20/08 01:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month