Discussion in "Welcome Forum" started by    maruf    Oct 3, 2012.
Wed Oct 03 2012, 08:36 am
#1
Hello Dear,
I'm confusing with one program, as long as i know "if" is one statement and it perform it's operation in an order way, whatever any number of "if" is exist without "else". I'm write a program to find largest from three number and someone told me that the program won't run, but i compile and run this program correctly with Trubo C++ 4.5, so where is my error? My program is below:


#include<stdio.h>
#include<conio.h>

int main(void)
{
int a,b,c;

printf("Enter first num:");
scanf("%d",&a);

printf("Enter 2nd num: ");
scanf("%d",&b);

printf("Enter 3rd num: ");
scanf("%d", &c);

if(a>b && a>c)
printf("%d is heighest", a);

if(b>a && b>c)
printf("%d is heightest", b);

if(c>a && c>b)
printf("%d is heightest",c);

getch();

return 0;
}

I'm So confuse!!!! :-(

Thank You
Maruf


[ Edited Wed Oct 03 2012, 12:07 pm ]
Wed Oct 03 2012, 11:16 am
#2
if(c>a && c>b)
printf("%d is heightest",c);
here is the fault, u forget to write 'c' on dis line so it always puts a garbage value, that the only problem......
Wed Oct 03 2012, 12:12 pm
#3
@sonuverma
i miss 'c' to type in this site
but my program was still this:
#include<stdio.h>
#include<conio.h>

int main(void)
{
int a,b,c;

printf("Enter first num:");
scanf("%d",&a);

printf("Enter 2nd num: ");
scanf("%d",&b);

printf("Enter 3rd num: ");
scanf("%d", &c);

if(a>b && a>c)
printf("%d is heighest", a);

if(b>a && b>c)
printf("%d is heightest", b);

if(c>a && c>b)
printf("%d is heightest",c);

getch();

return 0;
}
Wed Oct 03 2012, 12:34 pm
#4
then i guess there is nothing any error, try using any other compiler
 maruf like this.
Wed Oct 03 2012, 12:57 pm
#5
there must be if else statement....

try changing next statement to else if and last if to else
Wed Oct 03 2012, 10:00 pm
#6


someone told me that the program won't run,
but i compile and run this program correctly with Trubo C++ 4.5, so where is my error?

maruf


What error ?
Ask whoever said it doesn't work to tell you what they did,and what happened.

Your code does not handle the case where the two highest numbers are the same
eg 7, 7, 5.. is that the problem ?




[ Edited Wed Oct 03 2012, 10:19 pm ]
 marufAmit Kumar Das like this.
Thu Oct 04 2012, 02:12 am
#7
@ExperimenterUK
It was the simple program, and i wrote it for find largest from three (e.g, 10, 5, 15), and i know it will not handle the same largest value. My point is that, the person told me that, there must use, "else if", where i used a series of "if" statement.
Thu Oct 04 2012, 05:46 am
#8


@ExperimenterUK
My point is that, the person told me that, there must use, "else if",
where i used a series of "if" statement.

maruf


Well as you have proved..
you don't need an "else" or "else if" with "if"


[ Edited Thu Oct 04 2012, 05:50 am ]
 maruf like this.
Thu Oct 04 2012, 07:44 am
#9
Thanks to all, for helping me.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

SamuelSmise
Thu Apr 25 2024, 09:56 am
DustinErele
Thu Apr 25 2024, 08:44 am
ztaletpzca
Wed Apr 24 2024, 11:19 pm
IrardlPex
Wed Apr 24 2024, 08:42 pm
Charlestehed
Wed Apr 24 2024, 05:20 pm
Robertgurse
Wed Apr 24 2024, 02:43 pm
Richardedils
Wed Apr 24 2024, 04:07 am
Malcolmaccek
Wed Apr 24 2024, 01:21 am