Lucky Pradana
Lucky Pradana

Reputation: 23

How can I sum up the row of numbers in C?

So I got an assignment to make a program. The program to create is using a variable starting from number 1. The value of this variable increases by 1 each iteration. Make a sum of the iteration values ​​before and then. Show the sum results in the terminal. If the sum is more than one hundred, the program will exit the iteration by issuing the words "the program is complete"

I'm not understanding this assignment, but I managed to make this code :

#include<stdio.h>
int main() {
    int i,j,rows;
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    for (i=1; i<=rows; ++i) {
        for (j=1; j<=i; ++j)

        { printf("%d ",j); }
        printf("\n");
    }
    return 0;
}

The problem I get :

  1. I need to know the number of rows that if I sum them up will be 100
  2. I need to show the previous equation in the output, something like this:

    1 = 1
    1 + 2 = 3
    1 + 2 + 3 = 6
    1 + 2 + ... + etc = no more than 100
    

Upvotes: 1

Views: 789

Answers (1)

PatrickOfThings
PatrickOfThings

Reputation: 239

Here is a rough draft of some code to do what you want.

#include <stdio.h>

int main()
{

int i,j,rows,sum;

printf("Enter number of rows: ");
scanf("%d", &rows);

for (i=1; i<=rows; i++) 
{
    sum = 0;

    for (j=1; j<=i; j++)
    { 
        sum += j;
        printf("%d ",j); 
    }


    printf(" : Sum = %i\n",sum);
    if(sum > 100)
    {
        printf("Program Complete\n");
        break;
    }
}

return 0;
}

Sample Output

Upvotes: 1

Related Questions