Practice Program
Solution:
void main()
{
int a[10],n,i;
int sum_of_odd = 0;
printf("Enter the number of elements: ");
scanf("%d",&n);
//taking array elements as input.
for(i=0;i<i++)
{
printf("\nEnter the Element: ");
scanf("%d",&a[i]);
}
//printing the array elements
printf("Array Elements are: \n");
for(i=0;i<i++)
{
printf("%d\t",a[i]);
}
//CALCULATING THE SUM OF ODD AND EVEN ELEMENTS.
for(i=0;i<i++)
{
if(a[i]%2 != 0)
{
sum_of_odd = sum_of_odd + a[i];
}
}
printf("\nSum of all Even Numbers is %d",sum_of_odd);
}
void main() { int a[10],n,i; int sum_of_odd = 0; printf("Enter the number of elements: "); scanf("%d",&n); //taking array elements as input. for(i=0;i<i++) { printf("\nEnter the Element: "); scanf("%d",&a[i]); } //printing the array elements printf("Array Elements are: \n"); for(i=0;i<i++) { printf("%d\t",a[i]); } //CALCULATING THE SUM OF ODD AND EVEN ELEMENTS. for(i=0;i<i++) { if(a[i]%2 != 0) { sum_of_odd = sum_of_odd + a[i]; } } printf("\nSum of all Even Numbers is %d",sum_of_odd); }
Output1
Enter the number of elements: 5
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Array Elements are:
1 2 3 4 5
Sum of all Odd Numbers is 9
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Array Elements are:
1 2 3 4 5
Sum of all Odd Numbers is 9
Output2
Enter the number of elements: 10
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Array Elements are:
1 2 3 4 5 6 7 8 9 10
Sum of all Even Numbers is 25.
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Array Elements are:
1 2 3 4 5 6 7 8 9 10
Sum of all Even Numbers is 25.
Output3
Enter the number of elements: 15
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Enter the Element:11
Enter the Element:12
Enter the Element: 13
Enter the Element: 14
Enter the Element:15
Array Elements are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sum of all Even Numbers is 64.
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Enter the Element:11
Enter the Element:12
Enter the Element: 13
Enter the Element: 14
Enter the Element:15
Array Elements are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sum of all Even Numbers is 64.
defining a function 'sum_of_odd' which will return sum of all Even numbers.
int sum_of_odd(int a[],int n)
{
int sum_of_odd = 0;
int i;
for(i=0;i<i++)
{
if(a[i]%2 != 0)
{
sum_of_odd = sum_of_odd + a[i];
}
}
return sum_of_odd;
}
int sum_of_odd(int a[],int n) { int sum_of_odd = 0; int i; for(i=0;i<i++) { if(a[i]%2 != 0) { sum_of_odd = sum_of_odd + a[i]; } } return sum_of_odd; }
Using above define function 'sum_of_odd()':
void main()
{
int a[10] = {1,2,3,4,5};
result = sum_of_odd(a,5);
printf("Sum of all odd numbers are %d",result);
}
void main() { int a[10] = {1,2,3,4,5}; result = sum_of_odd(a,5); printf("Sum of all odd numbers are %d",result); }
Output1
Sum of all odd numbers are 9.
Using above define function 'sum_of_odd()':
void main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int result;
result = sum_of_odd(a,10);
printf("Sum of all odd numbers are %d",result);
}
void main() { int a[10] = {1,2,3,4,5,6,7,8,9,10}; int result; result = sum_of_odd(a,10); printf("Sum of all odd numbers are %d",result); }
Output2
Sum of all odd numbers are 25.
Using above define function 'sum_of_odd()':
void main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int result;
result = sum_of_odd(a,15);
printf("Sum of all odd numbers are %d",result);
}
void main() { int a[10] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int result; result = sum_of_odd(a,15); printf("Sum of all odd numbers are %d",result); }
Output3
Sum of all odd numbers are 64.
0 Comments
Post a Comment