Solved

Fraction inputs

Posted on 2001-06-17
3
458 Views
Last Modified: 2010-04-15
I have only just started a course in 'C', as you can see below I havent got very far.
I need to know how to enter a fraction as 1/4 rarther than 14 as i have set it below.
Any help would be nice.
Thanks Guys..

#include <stdio.h>

void main()
{
int fraction;
                 
printf("\nEnter a Fraction 1/4 as 14 same with other Fractions\n");

printf("for the decimal equivalent ");
scanf("%d", &fraction);

if( fraction == 1/4 )
printf(" The Decimal equivalent for 1/4 is 0.25 ");
else
if( fraction == 1/3 )
printf(" The Decimal equivalent for 1/3 is 0.33 ");

}

0
Comment
Question by:pnorris98
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
hi dude,

use float or double to input fractions:



#include <stdio.h>

void main()
{
     int f1,f2;
               
     printf("\nEnter a Fraction [1/4 as 14]: \n");
     scanf("%d/%d", &f1,&f2);

     if(f2 == 0)
          printf("Can't have a fraction with 0\n");
     else
     {
          float fraction = f1/(float)f2;
          printf(" The Decimal equivalent for %d / %d is: %f\n\n", fraction);
}
}

good luck
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 100 total points
Comment Utility
sorry the last printf line should be:
          printf(" The Decimal equivalent for %d / %d is: %f\n\n", f1,f2,fraction);
0
 

Author Comment

by:pnorris98
Comment Utility
Cheers Mate,
I wasnt using this to get my work done for me, I just learn alot quicker this way.
Again thanx alot

Phil...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now