52.3k views
0 votes
You do not need to use any functions beyond the main function in this problem. Initialize an array of int with the values: 4, 6, 9, and 12. Write a for loop to add the values in the array and find their sum. Write a second loop to print the values from the array and their sum in the following format: 4 + 6 + 9 + 12 = 31

User Antonello
by
5.4k points

1 Answer

4 votes

Answer:

Following are the code in C language

#include <stdio.h> // header file

int main() // main function

{

int s1=0;// variable declaration

int arr[10]={4,6,9,12}; // array declaration

for(int k=0;k<4;++k) // iterating over the loop

{

s1=s1+arr[k]; // sum of the array of elements

}

for(int k=0;k<4;++k) // iterating over the loop

{

if(k<3)

{

printf("%d+",arr[k]); //for display values

}

else

{

printf("%d=",arr[k]);

}

}

printf("%d",s1); // display the total sum

return 0;

}

Output:

4+6+9+12=31

Step-by-step explanation:

  • Declared a variable sum of int type also declare an array "arr" of int type.
  • Iterating over the for loop to add the values of the array in the "sum" variable.
  • Again iterting the for loop to print the values from the array in the format as given in the question .

User Lee Benson
by
5.3k points