Thread: Need help woth fractions..

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Need help woth fractions..

    Hi! i am new to C, so i want your help about a program. Could someone post any code on how to add two fractions, and checking the case of the zero denominator...thanks!

    my code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
    int a, b, c, d;
    char dummychar;
    int divisor, divident;
    
    printf( "Enter First divisor:");
    scanf("%d",&a); 
    printf("\n");
    
    printf("Enter the First divident:");
    scanf("%d",&b);
    printf("\n");
    
    printf( "Enter second divisor:");
    scanf("%d",&c); 
    printf("\n");
    
    printf( "Enter second divident:");
    scanf("%d",&d); 
    printf("\n");
    
    divisor = a*d + b*c;
    divident = b+d;
    how can i print the result..and how can i use an if statement to checking for zero divident?

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    comparisons in c work like this
    Code:
    if(conditional) {
         code to be executed if conditional is true
    }
    You can work that into your program, then to print use printf.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    yes, i know that, could you send me the code plz..

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    First of all, stick to C code if you're going to post on this forum, not C++. Second of all, we don't just do homework for people. Telling you how to use programming as a tool is one thing, but handing out finished code is quite another.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    snapshooter you have a little error there
    Code:
    a   c   a*d   c*b   a*d+c*b
    - + - = --- + --- = -------
    b   d   b*d   d*b     b*d  
    
    divisor = a*d + b*c;
    divident = b*d;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 05:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM