Sei sulla pagina 1di 12

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();
}

2) /* program to perform simple arithmetic operations using character choice*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char ch;
clrscr();
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
printf("Enter + -Addition, - - subtraction, * -multiplication, / -division\n");
//scanf("%c",&ch);
ch=getch();
putch(ch);
switch(ch)
{
case '+': c=a+b;
printf("sum of %d and %d is %d",a,b,c);
break;
case '-': c=a-b;
printf("difference of %d and %d is %d",a,b,c);
break;
case '*': c=a*b;
printf("product of %d and %d is %d",a,b,c);
break;
case '/': 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();
}

3) /* program to find alphabet is vowel or consonant*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char ch;
clrscr();
printf("Enter the alphabet\n");
ch=getch();
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("alphatbet is vowel");
break;
default: printf("alphabet is consonant");
}
getch();
}

4) /* program to print grade of a student*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,total,per=0,ch;
clrscr();
printf("Enter the marks in 6 subjects\n");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
if((a<35)||(b<35)||(c<35)||(d<35)||(e<35)||(f<35))
printf("Grade is F");
else
{
total=a+b+c+d+e+f;
per=(total*100)/600;
printf("total = %d",total);
printf("per=%d",per);
ch=per/10;
printf("ch=%d",ch);
switch(ch)
{
case 10:
case 9:
case 8:printf("Grade is H");
break;
case 7:printf("Grade is A");
break;
case 6:printf("Grade is B");
break;
case 5:printf("Grade is C");
break;
case 4:
case 3:printf("Grade is D");
break;
default: printf("Invalid marks");
}
}
getch();
}
5) /* program to find whether entered character is alphabet or a digit*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
char c,d;
clrscr();
printf("Enter the character\n");
c=getch();
if(isalpha(c))
printf("Entered character %c is an alphabet \n",c);
else
if(isdigit(c))
printf("Entered character %c is a digit \n",c);
getch();
}

6) /* program to find whether the entered character is in lowercase or uppercase */


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
char c,d;
clrscr();
printf("Enter the character\n");
c=getch();
if(islower(c))
printf("Entered character %c is in lower case \n",c);
else
if(isupper(c))
printf("Entered character %c is in uppercase \n",c);
getch();
}

7) /* program to convert lowercase to uppercase and viceversa*/

#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 ");

if (islower(c)) //check if alphabet is lower


putchar(toupper(c)); // print in upper case
else
putchar(tolower(c)); // print in lower case
}
else
printf("Entered character is not an alphabet");
getch();
}
8) /* program to print sum of first 10 natural numbers using while */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,sum=0;
clrscr();
printf("The sum of first 10 natural numbers is \n");
while(i<=10)
{
sum=sum+i;
i++;
}
printf("%d",sum);
getch();
}

9) /* program to print sum of first 10 natural numbers using do-while*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,sum=0;
clrscr();
printf("The sum of first 10 natural numbers is \n");
do
{
sum=sum+i;
i++;
}
while(i<=10);
printf("%d",sum);
getch();
}

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();
}

12) /* program to show difference between while and do while*/


#include<stdio.h>
#include<conio.h>

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

13) /* program to show difference between while and do while*/


#include<stdio.h>
#include<conio.h>

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();
}

15) /* program to find sum of even and odd numbers*/


#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)
{
if(i%2==0)
evensum=evensum+i;
else
oddsum=oddsum+i;
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();
}
16) /* program to find sum of even and odd numbers*/
#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);
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();
}

19) /* program to print sum of first n natural numbers using for */


#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\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();
}

22) /* program to find sum of even and odd numbers*/


#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);
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();
}

Potrebbero piacerti anche