Thread: I'm a beginner and I need help

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Unhappy I'm a beginner and I need help

    I started 'c' programming about 2 weeks ago, and our instructer is going way too fast.
    I've been given so many asingments, but i got stuck on this one.
    I have to write a program, without using loop.
    the program has to take a fractional number and round it to two decimal places. ex. 32.4851, to 32.49.
    I know this looks pathetic to all of you, but I'm a begginer, and would really appretiate your help. thank you.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    You can specify the number of decimal places when you output the number
    Code:
    #include <stdio.h>
    
    int main()
    {
    	double num;
    	
    	num = 32.4851;
    
    	printf("%.2lf", num); /* .2 specify's 2 decimals */
    
    	return 0;
    }

  3. #3
    Unregistered
    Guest
    I think your instructor wants you to round the numbers using the truncation property of integer division. A possible solution may be to multiply the number by 100 or so, to get rid of the decimal, then change or cast the number to an integer, add 5 or so to make up for what you are going to truncate, and then divide the number by 100 or so. You'll have to play with the numbers in order to get it to work. Look up rounding in your textbook.

Popular pages Recent additions subscribe to a feed