November 18th, 2012, 12:06 PM

No error but giving wrong solution HELP PLEASE
CHECK
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
float c;
c=a*(9/5)+32;
printf("===============================================================================");
printf("\n \n \t This Program will Convert Centigrade Temperature to Fahrenheit ");
printf("\n \n================================================================================");
printf("\n \n Enter the Temperature In Centigrade : ");
scanf("%d",&a);
printf("\n %d Centigrade = %f Fahrenheit",a,c);
printf("\n \n *******************************************************************************");
printf("\n \t \t This Program is Developed By Raja Muhammad Tahir Qaiser \n \n \t \t \t Roll Number 151 BS IT 1st Semester ");
printf("\n \n *******************************************************************************");
getch();
}
November 18th, 2012, 12:15 PM

1. Use tags when posting code, so it looks like this.
Code:
#include <stdio.h>
/* yes, really!, main returns an int (not void) */
int main ( ) {
printf("Hello world\n");
return 0;
}
2. Check your statement order.
c=a*(9/5)+32;
....
scanf("%d",&a);
printf("\n %d Centigrade = %f Fahrenheit",a,c);
3. Be mindful that 9/5 is done in integer arithmetic.
November 18th, 2012, 12:57 PM

Originally Posted by salem
3. Be mindful that 9/5 is done in integer arithmetic.
Raja, that is a deceptively simple statement, but if you choose to ignore it then you will have to endure many hours of frustration.
You want 9/5 to give you a float value of 1.8, just as your electronic calculator would do. That's what's called floatingpoint division (that "floatingpoint" is where the float datatype got its name from).
But C also does integer division, which is the kind that we were first taught in school, where you get an integer (AKA "whole number") quotient and a remainder. So the integer division result of 9/5 would be 1 R 4. By the way and for your information, you use the modulo operator, %, to get the remainder of an integer division.
Multiplying by the floatingpoint value 1.8, your result would be 1.8 times greater, a little less than twice the original, but multiplying by the integer division result of 1, the result would be equal to the original value. Quite obviously, you want the floatingpoint division result and not the integer division one.
C uses the same symbol, / , for both integer and floatingpoint division, so then how does C know which kind of division you want it to use? Quite simply through the datatypes of the operands you give it. If both operands (in this case, 9 and 5) are integer, then it's integer division, but if either if them are floatingpoint then it's floatingpoint division. At this point, you need to review the section of your textbook on literals, specifically regarding the difference between int and float and double literals. For example, 42 would be int, whereas 42.0 would be double.
BTW, the same thing applies to multiplication ( * ), but in most situations it's difficult to detect the difference that it makes.