C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 18th, 2012, 11:06 AM
rajatahirqaiser rajatahirqaiser is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 rajatahirqaiser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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();
}

Reply With Quote
  #2  
Old November 18th, 2012, 11:15 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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.
__________________
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

Reply With Quote
  #3  
Old November 18th, 2012, 11:57 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > No error but giving wrong solution HELP PLEASE

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap