Tuesday, 29 July 2014

9. WRITE A MENU DRIVEN C PROGRAM TO A. ACCEPT TWO NUMBERS N AND M B. SUM OF ALL INTEGERS RANGING FROM N TO M C. SUM OF ALL ODD INTEGERS RANGING FROM N TO M D. SUM OF ALL EVEN INTEGERS RANGING FROM N TO M DISPLAY AN ERROR MESSAGE IF N > M. CREATE FUNCTIONS FOR EACH OF THE OPTIONS



9. WRITE A MENU DRIVEN C PROGRAM TO
A. ACCEPT TWO NUMBERS N AND M
B. SUM OF ALL INTEGERS RANGING FROM N TO M
C. SUM OF ALL ODD INTEGERS RANGING FROM N TO M
D. SUM OF ALL EVEN INTEGERS RANGING FROM N TO M
DISPLAY AN ERROR MESSAGE IF N > M. CREATE FUNCTIONS FOR EACH OF THE OPTIONS
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i,m,n,ch;
while(1){
printf("1.Sum of All numbers \n2.Sum of all even numbers\n3.Sum of
all odd numbers \n4.exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch){
case 1:
sum=0;
printf("\nEnter n and m value\n");
scanf("%d %d",&n,&m);
if(m<n)
printf("\n m value should be greater than n\n");
for(i=n;i<=m;i++)
sum=sum+i;
printf("\nSum of %d to %d is %d\n",n,m,sum);
break;
case 2:
sum=0;
printf("\nEnter n and m value \n");
scanf("%d %d",&n,&m);
if(m<n)
printf("\n m value should be greater than n\n");
for(i=n;i<=m;i++)
if(i%2==0)
sum=sum+i;
printf("\nSum of even numbers from %d to %d is %d\n",n,m,sum);
break;
case 3:
sum=0;
printf("\nEnter n and m value \n");
scanf("%d %d",&n,&m);
if(m<n)
printf("\n m value should be greater than n\n");
for(i=n;i<=m;i++)
if(i%2!=0)
sum=sum+i;
printf("\nSum of odd numbers from %d to %d is %d\n",n,m,sum);
break;
case 4:
exit(0);
default:
printf("Invalid option\n");
break;
}
}
}

0 comments:

Post a Comment

 
- |