Help - Search - Members - Calendar
Full Version: Turbo C Code Problem
BleepingComputer.com > Software > Programming
   
rebellion534
hello im a begginer in Turbo C compiler

please help me to code this,
example my int is "a" and "b"
then if a is negative or either of the two it will display the product
but if there is no negative it will display the sum.
example if "a" is 2 and "b" is -3 it will multiply
but if both of them is positive it wiill add.

thanks!
groovicus
So in other words:
If a>0 and b>0, then display a+b,
else display a*b

Let's see your code, and we will help you fix it. This is a pretty simple exercise. smile.gif
rebellion534
#include <stdio.h>
main()
{
int a,b,c,d;
clrscr();
printf("input 1st number:");
scanf("%d",&a);
printf("input 2nd number:");
scanf("%d",&b);
printf("input 3rd number:");
scanf("%d",&c);
printf("input 4th number:");
scanf("%d",&d);
if ("a<0&&b||a&&b<0")
{
printf("the product is:%d\n",a*cool.gif;
}
else
printf("the sum is:%d\n",a+cool.gif;
getch();
}



here, im making 4 int. all negative will be multiplied and positive will be added

thanks for the help..
groovicus
Is that code working for you then?
rebellion534
that is the problem, it's not working.. :-) im sorry for the "smiley" in the code it is supposedly b )
groovicus
lmfao.gif

Oh.. I don't use C very often, so I didn't look very hard. We can probably figure this out together though... First, you are only comparing the condition on two of the digits. The second two digits are never compared. That's a good place to start though. Instead of using four digits, see if you can make it work for just two. What happens then?
rebellion534
ok.. so let's just imagine that the

printf("input 3rd number:");
scanf("%d",&c);
printf("input 4th number:");
scanf("%d",&d);

is not included

so if you input either "a" or "b" is negative

then it will be multiplied

but if you input both positve numbers

it will be added..

the program will be like this..

input 1st number: 2
input 2nd number: 3

the sum is: 5

or.......

input 1st number: -2
input 2nd number: 3

the product is: 6

so what im trying to do is allow the user to input 4 numbers like this one...

input 1st number: -2
input 2nd number: 3
input 3rd number: -3
input 4th number: 5

the product is: 6
the sum is: 8

so if you notice the negative number are multiplied and positive are added.

so if three of them is negative it will all be multiplied except the positive one which will remain because there are no any other positive number that can be added to it.
groovicus
I'm sorry. I misunderstood your original goal. I thought you were examining numbers in pairs. What you want to do is a little more fun. What you want to do is:

Start out with two variables called prod and sum. Set prod equal to one, and sum equal to zero. Get the first number. If it is a negative, multiply it with prod. If it is positive, add to sum. When all the input has been processed, print out the results.

Something like this (except using c syntax):
sum=0; prod=1;

If(user_variable = 0)
print out sum and prod
else if (user_variable <0)
sum = sum + user_variable
else prod = prod * user_variable
rebellion534
ok thanks.. maybe i need to include sum and prod as an integers.. i'll try to work on it!.. thanks for the time and help mr.groovicus :-)
rebellion534
im almost complete mr.groovicus! i didn't use the integer sum and prod anymore..
it is not yet finished, but it works!

this program is for multiplying all negative numbers that you input
and add all the positive numbers that you input.

here is the code:

#include <stdio.h>
main()
{

int a,b,c,d;
clrscr();

printf("enter 1st number:");
scanf("%d",&a);
printf("enter 2nd number:");
scanf("%d",&b);
printf("enter 3rd number:");
scanf("%d",&c);
printf("enter 4th number:");
scanf("%d",&d);

if(a<0&&b>0&&c>0&&d>0)
{
printf("the product is:%d\n",a);
printf("the sum is:%d\n",b+c+d);
}
else if(a<0&&b<0&&c>0&&d>0)
{
printf("the product is:%d\n",a*cool.gif;
printf("the sum is:%d\n",c+d);
}
else if(a<0&&b<0&&c<0&&d>0)
{
printf("the product is:%d\n",a*b*c);
printf("the sum is:%d\n",d);
}
else if(a<0&&b<0&&c<0&&d<0)
{
printf("the product is:%d\n",a*b*c*d);
printf("there is no sum\n");
}
else
printf("the program is incomplete and cannot calculate that");
getch();
}
groovicus
That looks reasonable, but the problem is that you are trying to do a check for every single condition. What about this: (no guarantees that the syntax is correct):
CODE
int i;
int sum = 0;
int prod = 1;
int cont = -1;

while (cont==-1){
   printf("Enter a number, or enter 0 to quit:");
   scanf("%d",&i);

   if(i<0){
        prod = prod * i;
   }else if(i>0){
        sum = sum + 1;
   }else cont=0;
   }
}

printf("the product is:%d\n",prod);
printf("the sum is:%d\n",d);


As long as the number entered is not zero, the program will continue to execute. Once a zero is entered, then the program prints out the results. This is better because with no modifications at all, I can take two numbers, 20 numbers, or 200 numbers.

Good Luck. smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.