Solved

Fraction inputs

Posted on 2001-06-17
3
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 6200036
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
ID: 6200038
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
ID: 6200048
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing a array as parameter - C 2 111
C language IDE – Compilers installation 14 96
Grammars for C C++ and java 1 147
Super Scope, DHCP 5 102
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

710 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