Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Accepts a positive integer n less than 100 from the user and prints out the sum

C Basic Declarations and Expressions: Exercise-63 with Solution

Write a C program that accepts a positive integer n less than 100 from the user and prints out the sum 14 + 24 + 44 + 74 + 114 + • • • + m4 , where m is less than or equal to n. Print appropriate message.

If the input is outside the range.
Test data and expected output:
Input a positive number less than 100:
Sum of the series is 1024388

Sample Solution:

C Code:

#include <stdio.h>
  int main() {
    int i, j, n, sum_int = 0;
    printf("Input a positive number less than 100: \n");
    scanf("%d", & n);
    if (n < 1 || n >= 100) {
      printf("Wrong input\n");
      return 0;
    }
    j = 1;
    for (i = 1; j <= n; i++) {
      sum_int += j * j * j * j;
      j += i;
    }
    printf("Sum of the series is %d\n", sum_int);
    return 0;
  }

Sample Output:

Input a positive number less than 100: 68
Sum of the series is 37361622

Flowchart:

C Programming Flowchart: Accepts a positive integer n less than 100 from the user and prints out the sum

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a C program that accepts a positive integer less than 500 and prints out the sum of the digits of this number.
Next: Write a C program that accepts integers from the user until a zero or a negative number, display the number of positive values, the minimum value, the maximum value and the average of all numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP