1) / Program To Perform Simple Arithmetic Operations Using Integer Choice
1) / Program To Perform Simple Arithmetic Operations Using Integer Choice
#include<stdio.h>
#include<conio.h>
void main()
{
int ch,a,b,c;
clrscr();
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
printf("Enter 1-Addition, 2- subtraction, 3-multiplication, 4-division\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("sum of %d and %d is %d",a,b,c);
break;
case 2: c=a-b;
printf("difference of %d and %d is %d",a,b,c);
break;
case 3: c=a*b;
printf("product of %d and %d is %d",a,b,c);
break;
case 4: if(b>0)
{
c=a/b;
printf("sum of %d and %d is %d",a,b,c);
}
else
printf("divide by zero -error\n");
break;
default: printf("invalid choice\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char c,d;
clrscr();
printf("Enter a character\n");
c=getchar( );
if(isalpha(c))
{
printf("Entered alphabet is %c\n",c);
printf("Changed case of alphabet is ");
10) /* program to print sum of first n natural numbers using while and decrement */
#include<conio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum\n");
scanf("%d",&n);
printf("The sum of first %d natural numbers is \n",n);
i=n;
while(i>=0)
{
sum=sum+i;
i--;
}
printf("%d",sum);
getch();
}
11) /* program to print sum of first n natural numbers using do while and
decrement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum\n");
scanf("%d",&n);
printf("The sum of first %d natural numbers is \n",n);
i=n;
do
{
sum=sum+i;
i--;
}
while(i>=0);
printf("%d",sum);
getch();
}
void main()
{
int i=11,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum\n");
scanf("%d",&n);
printf("The sum of first %d natural numbers is \n",n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("%d",sum);
getch();
}
Output:- 0
void main()
{
int i=11,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum\n");
scanf("%d",&n);
printf("The sum of first %d natural numbers is \n",n);
do
{
sum=sum+i;
i++;
}
while(i<=n);
printf("%d",sum);
getch();
}
Output:- 10
14) /* program to find sum of even and odd numbers using only while*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,sum=0,n,evensum=0,oddsum=0;
clrscr();
printf("Enter the value till which you want to find the sum of even and odd\n");
scanf("%d",&n);
while(i<=n)
{
evensum=evensum+i;
i+=2;
}
i=1;
while(i<=n)
{
oddsum=oddsum+i;
i+=2;
}
printf("sum of even numbers till %d is %d\n",n,evensum);
printf("sum of odd numbers till %d is %d",n,oddsum);
getch();
}
void main()
{
int i=0,sum=0,n,evensum=0,oddsum=0;
clrscr();
printf("Enter the value till which you want to find the sum of even and odd\n");
scanf("%d",&n);
do
{
if(i%2==0)
evensum=evensum+i;
else
oddsum=oddsum+i;
i++;
}
while(i<=n);
printf("sum of even numbers till %d is %d\n",n,evensum);
printf("sum of odd numbers till %d is %d",n,oddsum);
getch();
}
17) /* program to find sum of squares till a given number using while*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum of squares\n");
scanf("%d",&n);
printf("The sum of square of numbers till %d is \n",n);
while(i<=n)
{
sum=sum+i*i;
i++;
}
printf("%d",sum);
getch();
}
18) /* program to find sum of squares till a given number using do-while*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum of squares\n");
scanf("%d",&n);
printf("The sum of square of numbers till %d is \n",n);
do
{
sum=sum+i*i;
i++;
}
while(i<=n);
printf("%d",sum);
getch();
}
void main()
{
int i=0,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum\n");
scanf("%d",&n);
printf("The sum of first %d natural numbers is \n",n);
for(i=0;i<=n;i++)
{
sum=sum+i;
}
printf("%d",sum);
getch();
}
20) /* program to print sum of first n natural numbers using for and decrement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum\n");
scanf("%d",&n);
printf("The sum of first %d natural numbers is \n",n);
for(i=n;i>=0;i--)
{
sum=sum+i;
}
printf("%d",sum);
getch();
}
21) /* program to find sum of squares till a given number using for*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf("Enter the value till which you want to find the sum of squares\n");
scanf("%d",&n);
printf("The sum of square of numbers till %d is \n",n);
for(i=0;i<=n;i++)
{
sum=sum+i*i;
}
printf("%d",sum);
getch();
}
void main()
{
int i=0,sum=0,n,evensum=0,oddsum=0;
clrscr();
printf("Enter the value till which you want to find the sum of even and odd\n”);
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i%2==0)
evensum=evensum+i;
else
oddsum=oddsum+i;
}
printf("sum of even numbers till %d is %d\n",n,evensum);
printf("sum of odd numbers till %d is %d",n,oddsum);
getch();
}