3
votes

I'm a newbie!

I'm supposed to get 2 integers from the user, and print the result(sum of all numbers between those two integers).

I also need to make sure that the user typed the right number. The second number should be bigger than the first one.

And if the condition isn't fulfilled, I have to print "The second number should be bigger than the first one." and get the numbers from the user again until the user types right numbers that meet the condition.

So if I programmed it right, an example of the program would be like this.

Type the first number(integer) : 10

Type the second number(integer) : 1

The second number should be bigger than the first one.

Type the first number(integer) : 1

Type the second number(integer) : 10

Result : 55

End

I think that I have to make two loops, but I can't seem to figure out how. My English is limited, to help your understanding of this quiz, I'll add my flowchart below.

Flowchart

I tried many different ways I can think of, but nothing seems to work. This is the code that I ended up with now. But this doesn't work either.

#include <stdio.h>
void main(void)
{
    int a = 0;
    int b = 0;
    int total_sum = 0;
    printf("Type the first number : \n");
    scanf("%d", &a);
    printf("Type the second number : \n");
    scanf("%d", &b);
    while (a > b) {
        printf("The second number should be bigger than the first one.\n");
        printf("Type the first number : \n");
        scanf("%d", &a);
        printf("Type the second number : \n");
        scanf("%d", &b);
    }
    while (a <= b) {
        total_sum += a;
        a++;
    }
    printf("Result : \n", total_sum);
}
3
Welcome to StackOverflow. If you are looking for help with debugging code see ericlippert.com/2014/03/05/how-to-debug-small-programs - Yunnosch
In which way does it not work? Crash? Hang? Wrong output? - Yunnosch
You forgot your format specifier on the last printf call. - George
You should always compile your code with -Wall and read the warnings, this would have saved you this question ;) - Ctx
I'd use google to check if there is a formula for the sum of n numbers (there is), you can make your program loads faster by getting rid of the loop. Also you may consider checking for overflow. - James Snook

3 Answers

1
votes

Instead of using loop to sum the numbers, we can use mathematical formula.

Sum of first N integers= N*(N+1)/2

 #include <stdio.h>
 int main(void)
 {
     int a = 0;
     int b = 0;
     int sum;

     //Run infinite loop untill a>b
     while(1)
     {
         printf("Type the first number : ");
         scanf("%d", &a);
         printf("Type the second number : ");
         scanf("%d", &b);
         if(a>b)
         {
             printf("The second number should be bigger than the first one.\n");
         }
         else
         {
             break;
         }
     }

     //Reduce comlexity of looping
     sum=((b*(b+1))-(a*(a-1)))/2;

     printf("Result : %d " , sum);
     return 0;
 }
0
votes

After corrections your code should run. The community has pointed out many mistakes in your code. Here's an amalgamated solution:

#include <stdio.h>
int main(void)
{
    int a = 0;
    int b = 0;
    int correctInput=0;
    int total_sum = 0;
    do 
    {
        printf("Type the first number : \n");
        scanf("%d", &a);
        printf("Type the second number : \n");
        scanf("%d", &b);
        if(a<b)
            correctInput=1;
        else 
            printf("The second number should be bigger than the first one.\n");
    }
    while (correctInput ==0) ;

    while (a <= b) {
        total_sum += a;
        a++;
    }
    printf("Result : %d \n" , total_sum);
    return 0;
}
0
votes

Factorials are used frequently in probability problems. The factorial of a positive integer n (written n! and pronounced "n factorial") is equal to the product of the positive integers from 1 to n: n! = 1 x 2 x 3 x x n Write a program that takes as input an integer n and computes n!.