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 8709097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:17:07+00:00 2026-06-13T04:17:07+00:00

how to write program that takes a number as its argument and return the

  • 0

how to write program that takes a number as its argument and return the sum of 1+2+.. up to argument?

I can’t get the codes right. Can somebody help me?

#include<stdio.h>
#include<stdlib.h>                                                               
int main(int argc, char*argv[])
{
int i;
int sum =0;

if(argc !=2){
 printf("usage: %s <count> \n", argv[0]);
 exit(n);
 }

for(i=1; i<=atoi(argv[1]); i++){
 sum+=i;
}
  • 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-06-13T04:17:10+00:00Added an answer on June 13, 2026 at 4:17 am

    First, to answer your question.

    You need to actually output the result. Something like:

    printf("%d\n", sum);
    

    Or return it to whoever called the program, although that is a little unusual:

    int main( int argc, char **argv ) {
        ...
        return sum;
    }
    

    But I am providing my own answer here because there is a good reason to consider doing this in a loop… At least until you’ve thought about it a bit more.

    Namely, the formula (n * (n+1)) / 2 will overflow 32-bit integers and produce the wrong answer when n becomes 65536 or greater. But the 32-bit integer can itself store a sum up to n <= 92681. That means the formula by itself produces the wrong answer for roughly 30% of the solution space.

    So you might think you need to loop, but there’s a little trick here. Because the formula uses both n and n+1, you can guarantee that one of those numbers is evenly divisible by 2. And therefore you can do it like this:

    unsigned long n;
    unsigned long sum;
    
    n = atoi(argv[1]);
    
    if( n == 0 || n > 92681 ) {
        printf( "The supplied value (%u) is out of range\n", n );
    } else {
        if( (n % 2) == 0 ) {
            sum = (n / 2) * (n+1);
        } else {
            sum = n * ((n+1) / 2);
        }
        printf( "Sum from 1 to %u is %u\n", n, sum );
    }
    

    Now you have a simple formula that produces the same answer as the loop, at least for all values of n that don’t lead to overflowing the sum.

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

Sidebar

Related Questions

I'm solving the following kata: Write a program that takes as its first argument
I am trying to write a program that takes a number with a single
I need to write a program that takes input line at a time and
I'm trying to write a program that takes a large file (of any type)
I'm going to need to write a program that takes a list of persons
I have been assigned wit the task to write a program that takes a
I'm trying to write a simple program that takes in the users input, then
I want to write a simple program that takes a moderately sized wav file
I need to write a C++ code coverage program that takes in another C++
I'm trying to write a program that takes a Sudoku puzzle and solves it.

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.