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

  • SEARCH
  • Home
  • 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 6644257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:08:47+00:00 2026-05-26T00:08:47+00:00

This is a homework project I was assigned some time ago… I’ve been successful

  • 0

This is a homework project I was assigned some time ago… I’ve been successful in getting this far on my own, and the only hiccup I have left is (I believe) an issue with data types and overflow.

I’ve tried changing over to unsigned and double, and the code complies and still accepts input in the terminal, but it seems to hang up after that… nothing is printed and it looks like it’s caught in a loop.

Here is the code…

    /* pascaltri.c
     * A program that takes a single integer as input and returns the nth line of
     * Pascal's Triangle. Uses factorial() function to help find items of
     * individual entries on a given row.
     */
    #include <stdio.h>
    #include <stdlib.h>
    long factorial(long i) 
    {
        long fact = 1;

        while(i > 1)
        {
            fact = fact * i;
            i = i - 1;
        }
        return fact;
    }
    main(void)
    {   
        long n;
        long *nPtr;
        nPtr = &n;
        scanf(" %i", nPtr); 
        if (n >= 0)
        {
            long k;
            long *kPtr;
            kPtr = &k;                
            for(k = 0; k <= n; k++)
            {
                long ans;
                long *ansPtr;
                ansPtr = &ans;

                ans = factorial(n) / (factorial(k) * factorial(n - k));
                printf("\n %i", ans);
            }
            return 0;
        }
        return 0;
    }

It’s not perfect or pretty, but it works up to an input of 13 (that is, row 14) of the triangle. Beyond that I start getting gibberish and even negative values sprinkled throughout the returns… much larger values break the code and return nothing but an exit error message.

Any ideas on how I can correct this problem? I’ve been staring at the screen for much to long to really see anything myself. Also, it’s not essential, but I would like to print my return values on one line, rather than having them separated by a newline character.

1 5 10 10 5 1

Would the easiest way be to load the values into an array as they are computed, and then print the array? Or is there a built-in way I can tell the print statement to occur on only one line?

  • 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-26T00:08:48+00:00Added an answer on May 26, 2026 at 12:08 am

    You are suffering from integer overflow. You may need to find a different approach to the algorithm to avoid having to calculate the large numbers.

    In answer to your other point about the newline, you are explicitly printing the newline with the \n in your print statement. Remove it, and you will get answers printed on one line. You probably want to inlucde a final printf("\n"); at the end so the whole line is terminated in a newline.

    Some other observations:

    • You don’t need the first return 0; – the control will drop out of
      the bottom of the if block and on to the second (should be only)
      return 0; and not cause any problems.
    • You’re declaring kPtr but not using it anywhere
    • You don’t need to declare a separate variable nPtr to pass to scanf; you can pass &n directly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been assigned some sed homework in my class and am one step away
I'm doing a homework project that requires this: Below you will find the code
This is currently a homework project that me and my teammate are stuck on.
This IS NOT a Homework question! While building my current student database project I
This is an attempt to rewrite some old homework using STL algorithms instead of
First yes, this is a homework project for my Perl class. I am not
This is homework, just so that's mentioned. I have project called AdventCalendar, which is
I'm working on this homework that's kind of confusing me... I am provided with
This is homework, and this questions Extends this one So there is a button
This is homework. Beginning Java class. Still wrapping my head around this stuff. The

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.