Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Turbo C Code Problem


  • Please log in to reply
10 replies to this topic

#1 rebellion534

rebellion534

  • Members
  • 40 posts
  • OFFLINE
  •  
  • Local time:11:40 AM

Posted 17 April 2007 - 02:22 AM

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!

BC AdBot (Login to Remove)

 


m

#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:40 PM

Posted 17 April 2007 - 07:24 AM

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. :thumbsup:

#3 rebellion534

rebellion534
  • Topic Starter

  • Members
  • 40 posts
  • OFFLINE
  •  
  • Local time:11:40 AM

Posted 17 April 2007 - 10:40 AM

#include <stdio.h>
main()
{
int a,b,c,d;
clrscr();
printf("input 1st number:");
scanf("%d",&a);
printf("input 2nd number:");
scanf("%d",&:thumbsup:;
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*:flowers:;
}
else
printf("the sum is:%d\n",a+:trumpet:;
getch();
}



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

thanks for the help..

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:40 PM

Posted 17 April 2007 - 11:26 AM

Is that code working for you then?

#5 rebellion534

rebellion534
  • Topic Starter

  • Members
  • 40 posts
  • OFFLINE
  •  
  • Local time:11:40 AM

Posted 17 April 2007 - 11:29 AM

that is the problem, it's not working.. :-) im sorry for the "smiley" in the code it is supposedly b )

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:40 PM

Posted 17 April 2007 - 12:08 PM

:thumbsup:

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?

#7 rebellion534

rebellion534
  • Topic Starter

  • Members
  • 40 posts
  • OFFLINE
  •  
  • Local time:11:40 AM

Posted 17 April 2007 - 12:35 PM

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.

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:40 PM

Posted 17 April 2007 - 04:18 PM

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

#9 rebellion534

rebellion534
  • Topic Starter

  • Members
  • 40 posts
  • OFFLINE
  •  
  • Local time:11:40 AM

Posted 18 April 2007 - 05:29 AM

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 :-)

#10 rebellion534

rebellion534
  • Topic Starter

  • Members
  • 40 posts
  • OFFLINE
  •  
  • Local time:11:40 AM

Posted 18 April 2007 - 06:05 AM

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",&:thumbsup:;
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*:flowers:;
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();
}

#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:40 PM

Posted 18 April 2007 - 07:33 AM

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):
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. :thumbsup:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users