I'm Confusing with this C program
Discussion in "Welcome Forum" started by maruf Oct 3, 2012.
Wed Oct 03 2012, 08:36 am
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
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
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......
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
@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;
}
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:57 pm
there must be if else statement....
try changing next statement to else if and last if to else
try changing next statement to else if and last if to else
Wed Oct 03 2012, 10:00 pm
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 ]
maruf, Amit Kumar Das like this.
Thu Oct 04 2012, 02:12 am
@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.
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.
Powered by e107 Forum System