Assignment 1 :
Practice Program:
Q. 1 : Write a menu driven C program to perform the following operation on an integer array:
a) Display the sum of elements at even subscript position of array
b) Display the sum of elements at odd subscript position of array
Answer :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int arr[20], i, n, even_sum = 0, odd_sum = 0;
char choice;
printf("***** Menu ***** \n");
printf("Which operation to be perform \n");
printf("a) Display the sum of elements at even subscript position of array. !! press small a\n");
printf("b) Display the sum of elements at odd subscript position of array. !! press small b\n");
printf("Enter your choice a or b for that task : \n");
scanf("%c", &choice);
printf("Enter the size of Array : ");
scanf("%d", &n);
printf("Enter the array element : \n");
for (i = 0; i < n; i++)
{
printf("Enter the value of index[%d] : ", i);
scanf("%d", &arr[i]);
}
switch (choice)
{
case 'a':
for (i = 1; i < n; i++)
{
if (i % 2 == 0)
{
even_sum += arr[i];
}
}
printf("The sum of even subscript position of array = %d \n", even_sum);
break;
case 'b':
for (i = 1; i < n; i++)
{
if (i % 2 != 0)
{
odd_sum += arr[i];
}
}
printf("The sum of odd subscript position of array = %d \n", odd_sum);
break;
default:
printf("Wrong Input !!\n");
}
getch();
return 0;
}
*****************************************************************************
0 Comments