Write a C program that will accept 8 input from a user then find and print the sum of the inputted values.



To solve this question I will first items the steps we need to follow then write the program that solves the problem. Note that array index begin with 0. That was why our array iterator was initialize to 0

  1. Define a length variable to hold the size of the array using macro.
  2. Declare an empty array.
  3. Declare a variable to hold the sum of the array and initialize it to zero.
  4. Collect the user input and store the values into the empty array using a for loop.
  5. Iterate through the element of the array using a second loop and sum all the array value.
  6. Finally print out the sum of the values.

Answer

// Online C compiler to run C program online
#include <stdio.h>
#define LENGTH 8
int main() {
    // Write C code here
    int array[LENGTH];
    int sumArray=0;
    for (int a = 0; a < LENGTH; a++){
        printf("Enter value at array[%d]\n", a);
        scanf("%d", &array[a]);
    }
    for (int b = 0; b < LENGTH; b++){
        //printf("Enter value at array[%d]\n", a);
        sumArray+=array[b];
    }
    printf("The sum of all values in the array is %d ", sumArray);

    return 0;
}
Ameh Joseph Onyeke

Hello and welcome to my blog! My name is Ameh Joseph Onyeke a passionate educator and tech enthusiast with a BSc in Computer Science from Benue State University, a PGDE from the University of Maiduguri, and a Master’s in Software Engineering from Bayero University Kano. My journey blends a strong foundation in computing with a love for teaching, aiming to simplify complex tech concepts for learners at all levels. Through this blog, I provide answers and insights across 10+ programming languages to support students and tech enthusiasts in building their skills and confidence. Whether you're solving coding problems or sharpening your programming abilities, this blog is here to guide you. Let’s learn and grow together!

Post a Comment

Previous Post Next Post