Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:23:10+00:00 2026-05-25T21:23:10+00:00

I am writing a C program which adds two nonzero integers which are given

  • 0

I am writing a C program which adds two nonzero integers which are given as fractions. The integers are input as n1 d1 n2 d2. There seems to be an error when certain negative numbers are input, such as in the following combination ( 1 2 -1 2). This should state that the numbers being added are 1/2 and -1/2, and then state that the sum is 0. Instead, the program just shows a blank line and does not accept any further input. The second problem I have is that when the sentinel value is input the program is supposed to display a termination message and then end, instead it displays “floating point exception”.

 int
    main( void )
    {
int n1;
int d1;
int n2;
int d2;
int g;
int p;
int q;
int again;
int sumn;
int sumd;

again = 1;

while ( again == 1 ) {

  printf( "Please enter 4 nonzero integers representing the fractions: " );
  scanf( "%d%d%d%d", &n1, &d1, &n2, &d2 );

  if ( n1 == 0 && n2 == 0 && d1 == 0 && d2 == 0 ) {
    again = 0;
  }
  else {
    again = 1;
  }
  if ( ( n1 == 0 || n2 == 0 || d1 == 0 || d2 == 0 ) && ( n1 != 0 || n2 != 0 || d1 != 0 || d2 != 0 ) ) {
    printf( "One or more of the integers is zero\n" );
    again = 1;
  }
    else {

    g = gcd( d1, d2 );
    p = ( ( n1 * ( d2 / g ) ) + ( n2 * ( d1 / g ) ) );
    q = ( d1 * ( d2 / g ) );

    sumn = ( p / ( gcd( p, q ) ) );
    sumd = ( q / ( gcd( p, q ) ) );

    printf( "The fractions are: %d/%d and %d/%d\n", n1, d1, n2, d2 );

    if ( sumn == sumd ) {
      printf( "Their sum is: 1\n" );
    }
    else {
      if ( sumd == 1 ) {
        printf( "Their sum is: %d\n", sumn );
      }
      else {
        printf( "Their sum is: %d/%d\n", sumn, sumd );
      }
    }
  }
}
printf( "***** Program Terminated *****\n" );

return (EXIT_SUCCESS);
}

int gcd( int a, int b )
{
while ( a != b ) {
  if ( a > b ) {
    a = ( a - b );
  }
  else {
    b = ( b - a );
  }
}
return a;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:23:10+00:00Added an answer on May 25, 2026 at 9:23 pm

    GCD should be taken care of by providing only positive values. As mystical pointed, you should make the numbers positive. Moreover you should also check if any number coming in is not zero. So, edit the GCD function as follows:

    int gcd( int a, int b )
    {
     if (a==0 || b==0)
           return 1;
     if (a < 0)
            a = -a;
     if (b < 0)
            b = -b;
    while ( a != b ) {
      if ( a > b ) {
        a = ( a - b );
      }
      else {
        b = ( b - a );
      }
    }
    return a;
    }
    

    You can now see you get correct result results for even the combination like (1 2 -1 2) or even (-1 -1 -2 -2).

    And to solve the sentinel problem, edit the while loop as:

    while ( again == 1 ) {
    
      printf( "Please enter 4 nonzero integers representing the fractions: " );
      scanf( "%d%d%d%d", &n1, &d1, &n2, &d2 );
    
      if ( n1 == 0 && n2 == 0 && d1 == 0 && d2 == 0 ) {
        again = 0;
      }
      else {    
      if ( ( n1 == 0 || n2 == 0 || d1 == 0 || d2 == 0 ) && ( n1 != 0 || n2 != 0 || d1 != 0 || d2 != 0 ) ) {
        printf( "One or more of the integers is zero\n" );
        again = 1;
      }
        else {
    
        g = gcd( d1, d2 );
        p = ( ( n1 * ( d2 / g ) ) + ( n2 * ( d1 / g ) ) );
        q = ( d1 * ( d2 / g ) );
    
    
        sumn = ( p / ( gcd( p, q ) ) );
        sumd = ( q / ( gcd( p, q ) ) );
    
    
        printf( "The fractions are: %d/%d and %d/%d\n", n1, d1, n2, d2 );
    
        if ( sumn == sumd ) {
          printf( "Their sum is: 1\n" );
        }
        else {
          if ( sumd == 1 ) {
            printf( "Their sum is: %d\n", sumn );
          }
          else {
            printf( "Their sum is: %d/%d\n", sumn, sumd );
          }
        }
      }
     }
    }
    

    -Sandip

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program which has two panes (via CSplitter ), however I
I'm writing a program which is supposed to read two strings that can contain
I am currently writing a program which takes user input and creates rows of
I am writing a program which will tokenize the input text depending upon some
I'm writing a program which has both an ASP.NET configuration system and a Silverlight
I am writing a program which if I compile on a Suse 10 32-bit
I'm writing a program which will use scan conversion on triangles to fill in
I'm writing a program which will allow to load a specific managed .DLL file
I am writing a C# program which captures signals from a external device, and
I'm writing a C++ program which needs to be able to read a complex

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.