Solution using functions without passing arguments
Solution using functions and by passing arguments
Solution : Without using function
Program/Source Code
Program to take input for 10 elements using an array, display all the elements also calculate and print sum of all the elements.
For better understanding of the above program one should have knowledge of
* for loop
* while loop
* do while loop
* Arrays
Problem In Hand
Problem In Hand
To take input for 10 elements using an array further display all the elements amd also calculate and print sum of all the elements
Process and Solution Of Problem
* Using a for loop(any loop can be used) we take input for 10 elements
* Further we use for loop to display all the elements and calculate sum of all the elements as the elements are traversed.
Program/Source Code
#include <stdio.h> int main() { int a[10],i,s=0; /* input */ for(i=0;i<10;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } /* display */ for(i=0;i<10;i++) { printf("%d\n",a[i]); s=s+a[i]; } printf("Sum = %d",s); return 0; }
Program Explanation
- As we have to take input for 10 elements we declare an array of size 10.
- memory locations are created from 0 to 9 i.e. a[0] to a[9].
- Now using a for loop we take input for elements
- further we use for loop again to dislay the elements and as elements are traversed their values are added to a variable s. (s=s+a[i];)
- And after the full traversal the sum of all the elements is calculated in variable “s”.
- Now we display the value of variable “s” .
Output:
Enter 0 element 1
Enter 1 element 2
Enter 2 element 3
Enter 3 element 4
Enter 4 element 5
Enter 5 element 6
Enter 6 element 7
Enter 7 element 8
Enter 8 element 9
Enter 9 element 10
1
2
3
4
5
6
7
8
9
10
Sum = 55
Solution : Using function Without Passing Arguments
Program/Source Code
For better understanding of the above program one should have knowledge of
* for loop
* while loop
* do while loop
* Function
* Arrays
#include <stdio.h>
void check()
{
int a[10],i,s=0;
/* input */
for(i=0;i<10;i++)
{
printf("Enter %d element ",i);
scanf("%d",&a[i]);
}
/* display */
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
s=s+a[i];
}
printf("Sum = %d\n",s);
}
int main()
{
check();
return 0;
}
#include <stdio.h> void check() { int a[10],i,s=0; /* input */ for(i=0;i<10;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } /* display */ for(i=0;i<10;i++) { printf("%d\n",a[i]); s=s+a[i]; } printf("Sum = %d\n",s); } int main() { check(); return 0; }
Output:
Enter 0 element 10
Enter 1 element 20
Enter 2 element 30
Enter 3 element 40
Enter 4 element 50
Enter 5 element 60
Enter 6 element 70
Enter 7 element 80
Enter 8 element 90
Enter 9 element 100
10
20
30
40
50
60
70
80
90
100
Sum = 550
Solution : Using function Passing Arguments
Program/Source Code
For better understanding of the above program one should have knowledge of
* for loop
* while loop
* do while loop
* Function
* Arrays
#include <stdio.h>
void input(int a[],int n)
{
int i;
for(i=0;i<10;i++)
{
printf("Enter %d element ",i);
scanf("%d",&a[i]);
}
}
void show(int a[],int n)
{
int i,s=0;
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
s=s+a[i];
}
printf("Sum = %d\n",s);
}
int main()
{
int a[10];
input(a,110);
show(a,10);
return 0;
}
Output:
#include <stdio.h> void input(int a[],int n) { int i; for(i=0;i<10;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } } void show(int a[],int n) { int i,s=0; for(i=0;i<10;i++) { printf("%d\n",a[i]); s=s+a[i]; } printf("Sum = %d\n",s); } int main() { int a[10]; input(a,110); show(a,10); return 0; }
Output:
Enter 0 element 2
Enter 1 element 1
Enter 2 element 5
Enter 3 element 6
Enter 4 element 9
Enter 5 element 2
Enter 6 element 10
Enter 7 element 2
Enter 8 element 5
Enter 9 element 45
2
1
5
6
9
2
10
2
5
45
Sum = 87
You May Also Like:
C Program to Check Number is multiple of 7 or not
C Program to Find Factorial of Number Using Loop
C Program to Print Fibonacci Series
C Program to check character is an alphabet or not
C Language Program for Prime Number
Previous : Program to take input for 10 elements using an array. Further display all the elements.
Next : Program to take input for 5 elements using an array, display all the elements and also calculate and print product of all the elements.
Programming Solutions @ rajeshshukla
Programming Solutions @ rajeshshukla
Tutorials | Technical Questions | Interview Questions |
---|---|---|
C Programming C++ Programming Class 11 (Python) Class 12 (Python) |
C Language C++ Programming Python |
C Interview Questions C++ Interview Questions C Programs C++ Programs |