John Smith
John Smith

Reputation: 129

Looping the Sum of numbers (Inputs) in C

Give the c code for a loop that computes and displays a sum of numbers that the user enters. The loop should prompt the user for input until the user enters -335 and then exit and display the final sum.

This is what I have. Why does it not print (exit the loop) when I enter -335? It just keeps asking me to input a number.

#include <stdio.h>


int main(void)
{

int userNum;
printf("Please enter an integer:\n");
scanf("%d", &userNum);

while (userNum != -335){
    printf("Please enter an integer:\n");
    scanf("%d", &userNum);
    userNum += userNum;

}

printf("%d", userNum);


return 0;
}

Upvotes: 0

Views: 8950

Answers (4)

Raindrop7
Raindrop7

Reputation: 3911

your program is hanging in a while loop forever because there is even no luck to break the loop because there's no integer * 2 = -335, there's only float (-167.5).

  • there's one in million chance to break the loop IF you declare userNum as float so inside the loop enter -167.5:

    #include <stdio.h>
    
    int main(void)
    {
    
        float userNum;  
        printf("Please enter an integer:\n");
        scanf("%G", &userNum);
    
        while (userNum != -335){
            printf("Please enter an integer:\n"); // enter -167.5
            scanf("%G", &userNum);
            userNum += userNum; // -167.5 + (-167.5) = -335 which will cause while to exit
    
        }
    
        printf("%G", userNum); // -335
    
        return 0;
    }
    

to solve your problem declare a new variable let's say sum then sum userNum and store the result in it. so userNum is still unchanged while evaluating so if it was -335 while will exit.

#include <stdio.h>


int main(void)
{

    int userNum;
    int sum = 0; // (very important) initialize in order not to sum gabage value 

    printf("Please enter an integer:\n");
    scanf("%d", &userNum);

    while (userNum != -335){
        printf("Please enter an integer:\n");
        scanf("%d", &userNum);
        sum += userNum;

    }

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


    return 0;
}

Upvotes: 0

Rivasa
Rivasa

Reputation: 6750

You need an additional variable to keep track of the sum instead. Your current method keeps overwriting it:

So add a line:

int userNum;
int sum = 0; // sum holds total variable

and you add and change here:

while (userNum != 335){
    printf("Please enter an integer:\n");
    scanf("%d", &userNum);
    sum += userNum;
}

 printf("%d", sum);

Upvotes: 1

Gaurav Sehgal
Gaurav Sehgal

Reputation: 7542

  while (userNum != 335){
printf("Please enter an integer:\n");
scanf("%d", &userNum);
userNum += userNum;

You are losing the input provided by user in the last statement.Use a separate variable to store result.

 int ans=0;
 while (userNum != -335){
printf("Please enter an integer:\n");
scanf("%d", &userNum);
ans += userNum;

Upvotes: 1

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27672

userNum changes after you have input it, in the statement userNum += userNum;. -335 plus -335 is -670, not 335.

Upvotes: 2

Related Questions