473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Empty Output on a program that prints the maximum number out of 3

elpidahope
I don't understand what's wrong. My program prints only half the output. It doesn't print the maximum number. Here's the code if you wanna check it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5.     int a, b, c;
  6.     int max;
  7.  
  8.     printf("\n1st number: ");
  9.     scanf("%d", &a);
  10.     printf("\n2nd number: ");
  11.     scanf("%d", &b);
  12.     printf("\n3rd number: ");
  13.     scanf("%d", &c);
  14.  
  15.     if (b > a && b > c){
  16.         max = b;
  17.         printf("\nMax number is ", max);
  18.     }
  19.     if (c > a && c > b){
  20.         max = c;
  21.         printf("\nMax number is ", max);
  22.     }
  23.     if (a > b && a > c){
  24.         max = a;
  25.         printf("\nMax number is ", max);
  26.     }
  27.  
  28.     return 0;
  29.  
  30. }
  31.  
Jan 24 '22 #1
2 7313
dev7060
633 Expert 512MB
I don't understand what's wrong. My program prints only half the output. It doesn't print the maximum number. Here's the code if you wanna check it.
Add a format specifier for max.
Expand|Select|Wrap|Line Numbers
  1. printf("\nMax number is %d", max);

The statement may also be written just once after all the ifs.
Jan 28 '22 #2
donbock
2,426 Expert 2GB
Suppose all three of your numbers are equal to each other ... all of the if tests will fail, max will be uninitialized, and nothing will print.

People don’t always do what you expect. What would you like to happen if the user types xyz instead of a number?

Your code doesn’t scale well. Suppose tomorrow you need to change your program to find the maximum of four numbers instead of three? Every if condition has to change and the logic gets more complicated. Another approach is to tentatively set the maximum value to the first number and then compare the tentative maximum to each number in turn, updating the tentative maximum whenever you find a larger number.
Jan 28 '22 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kirby Angell | last post by:
We are porting our applications from Python 2.2 on RH9 to Python 2.3 on RH9. One of our apps used to be able to create threads with wild abandon, 800 or more if necessary. With Python 2.3 we are...
4
by: chirs | last post by:
Hi, What is the maximum number in JavaScript? I tried a large number like 0xff...ff with 30 fs, it still gives me a number, not an infinity. I use IE6. Thanks.
6
by: Sal | last post by:
I want to sale my Access 2002 program and charge per number of computers using it on the network.This is front/back end database. How can I limit maximum number of users and change it base on some...
2
by: mayur | last post by:
Hi, Can anyone help me with the following error I am getting in my asp.net application. The number name 'System.Web.UI.WebControls' contains more than the maximum number of prefixes. The maximum...
11
by: Leroy | last post by:
Hello, I have a question regarding the maximum number of parameters that can be passed to a procedure. In VB 6 the max was 60. What is the max for Dot Net? please and thanks.
2
by: Michael.Suarez | last post by:
In MS Sql Server 2000, if you run a stored procedure that query's more than 8 databases, you get this error gets raised: Maximum number of databases used for each query has been exceeded. The...
5
by: M | last post by:
There are a maximum number of controls can be put in a form for VB3 to VB6. The BOL of VBNET do not say VBNET also has this limitation. Because the clients want add more functions from time to...
2
by: Wayne | last post by:
Is there a "best practice" maximum number of controls that can be loaded on to a tab control before things start getting messy and sluggish? I'm asking for a "best practice" maximum number across...
6
by: ashiela | last post by:
i need to find the max position of my program. how can i do that? int main() { int j,max=0,k; for(k=0;k<=9;k++) { printf("Enter the number:"); scanf("%d", &j); }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.