Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 18th, 2012, 12:06 PM
 rajatahirqaiser
Registered User

Join Date: Nov 2012
Posts: 5
Time spent in forums: 1 h 11 m 4 sec
Reputation Power: 0
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();
}

#2
November 18th, 2012, 12:15 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 35 m 56 sec
Reputation Power: 1827
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;
}```

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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
November 18th, 2012, 12:57 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,890
Time spent in forums: 3 Months 1 Day 19 h 4 m 17 sec
Reputation Power: 2199
Quote:
 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 floating-point division (that "floating-point" 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 floating-point 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 floating-point division result and not the integer division one.

C uses the same symbol, / , for both integer and floating-point 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 floating-point then it's floating-point 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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > No error but giving wrong solution HELP PLEASE