C Exercises: Accepts a positive integer n less than 100 from the user and prints out the sum
Sum the series 1^4+2^4+4^4+...+m^4
Write a C program that accepts a positive integer n less than 100 from the user. It prints out the sum of 14 + 24 + 44 + 74 + 114 + • • • + m4. In this case, m is less than or equal to n. Print an appropriate message.
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;
// Prompt user for input
printf("Input a positive number less than 100: \n");
// Read the input value
scanf("%d", &n);
// Check if the input is valid
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;
}
// Display the result
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:
For more Practice: Solve these Related Problems:
- Write a C program to calculate the sum of n^4 for all n from 1 to m using a loop.
- Write a C program to compute the sum of fourth powers of even numbers up to a given limit.
- Write a C program to sum the series of fourth powers for a series of numbers provided by the user.
- Write a C program to compute the sum of 1^4+3^4+5^4+… for odd numbers up to m.
Go to:
PREV : Compute the sum of the digits of an integer less than 500.
NEXT : Count positive integers and display their min, max, and average.
C programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.