You are on page 1of 42

1/*program to find multiplication,division,difference of two

variables*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,mul,div,diff;
clrscr();
printf("enter the numbers a&b");
scanf("%d %d",&a,&b);
mul=a*b;
div=a/b;
diff=a-b;
printf("\n multiplication of a&b=%d",mul);
printf("\n division of a&b=%d",div);
printf("\n difference between a&b=%d",diff);
getch();
}
2/* input marks of student in six subject and print total
marks and percentage*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,tm,p;
clrscr();
printf("enter the values of a,b,c,d,e,f");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
tm=a+b+c+d+e+f;
p=(tm*100)/600;
printf("total marks is %d",tm);
printf("percentage is %d",p);
getch();
}
3/*program to interchange two variables without using third
variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter values of a and b");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nthe value of 'a' after interchange is %d",a);
printf("\nthe value of 'b' after interchange is %d",b);
getch();
}
4/*program to ifnd greater number of three numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enhter the values of a,b,c");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("%d is greater",a);
else
printf("%d is greater",c);
}
else
{
if(b>c)
printf("%d is greater",b);
else
printf("%d is greater",c);
}
getch();
}
5/*program to reverse a number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf(" value of n=%d \nthe reverse is %d ",n,sum);
getch();
}
6/*program to calculate average using while statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
int count=0,x;
float sum=0,avg,n;
clrscr();
printf("enter n");
scanf("%f",&n);
while(count<n)
{
printf("enter the value of x");
scanf("%d",&x);
sum=sum+x;
count=count+1;
}
avg=sum/n;
printf("average=%f",avg);
getch();
}
7/*program to find factorial using for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
long int fact=1;
int n,i;
clrscr();
printf("enter n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("factorial is %ld",fact);
getch();
}
8/*program for fibonacci series using whlile statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,a=0,b=1,c;
clrscr();
printf("enter n");
scanf("%d",&n);
printf("the fibonacci series is %d %d",a,b);
while(i<n)
{
c=a+b;
printf("%d",c);
a=b;
b=c;
i++;
}
getch();
}
10/*program to print name of particular months according to
user using switch*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf("enter value of i");
scanf("%d",&i);
switch(i)
{
case 1:
printf("january");
break;
case 2:
printf("february");
break;
case 3:
printf("march");
break;
case 4:
printf("april");
break;
case 5:
printf("may");
break;
case 6:
printf("june");
break;
case 7:
printf("july");
break;
case 8:
printf("august");
break;
case 9:
printf("september");
break;
case 10:
printf("october");
break;
case 11:
printf("november");
break;
case 12:
printf("december");
break;
default:
printf("you have to enter number between 1-12");
}
getch();
}
11/* Print prime number upto n */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf(" Enter the number upto which we have to find the prime number: ");
scanf("%d",&n);
printf("\n");
for(i=2;i<=n;i++)
{
for(j=2;j<=i-1;j++)
if(i%j==0)
break;
if(i==j)
printf("\t%d",i);
}
getch();
}
12/*Program to find armstrong number upto n*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a,b,c,x,y,d,n;
clrscr();
printf(“enter the value of n”);
scanf(“%d”,&n);
for (i=0;i<=n;i++)
{
c=i%10;
x=i/10;
b=x%10;
x=x/10;
a=x%10;
d=(a*a*a)+(b*b*b)+(c*c*c);
if (i==d)
printf("%d\n",i);
}
getch();
}
13/*program to print pattern: 1
1 2 1
1 2 3 2 1 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<4;i++)
{
for(j=4;j>=i;j--)
printf(" ");
for(j=1;j<=i;j++)
printf("%2d",j);
for(j=i-1;j>=1;j--)
printf("%2d",j);
printf("\n");
}
getch();
}
14 /* program to print pattern 1
23
456 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,g=0;
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
g=g+1;
printf("%d",g);
}
printf("\n");
}
getch();
}
15 /*Addition , Multiplication, Subtraction, division using
function */
#include<stdio.h>
#include<conio.h>
int sum(int,int);
int dif(int,int);
int mul(int,int);
int div(int,int);
main()
{
int a,b;
clrscr();
printf("enter the value of a & b=");
scanf("%d%d",&a,&b);
printf("\nthe sum is=%d",sum(a,b));
printf("\nthe difference is=%d",dif(a,b));
printf("\nthe multiplication is=%d",mul(a,b));
printf("\nthe division is=%d",div(a,b));
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}
int dif(int m,int n)
{
return(m-n);
}
int mul(int p,int q)
{
return(p*q);
}
int div(int i, int j)
{
return(i/j);
}
16/*WAP to enter 10 integers,10 characters,10 strings */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i;
char b[20];
char string[10][20];
clrscr();
printf("enter the 10 intger");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("enter the 10 character");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("enter the 10 string");
for(i=0;i<10;i++)
{
fflush(stdin);
scanf("%s",string[i]);
fflush(stdin);
}
getch();
}
17/*write a program to find sum and average of n array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,n,sum=0,avg;
clrscr();
printf(" enter n");
scanf("%d",&n);
printf(" enter the no \n");
for(i=0;i<n;i++)
{
scanf("%5d",&a[i]);
sum=sum+a[i];
}
printf(" the sum is %d",sum);
for(i=0;i<n;i++)
{
avg=sum/n;
}
printf(" the avg is %d",avg);
getch();
}
18/*program for transpose of matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[4][4],i,j,col,row;
clrscr();
printf("enter rows and column");
scanf("%d %d",&row,&col);
printf("enter the elements of matrix ");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("elements of matrix \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d \t",a[i][j]);
}
printf("\n");
}
printf("transpose of matrix \n");
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
printf("%d \t",a[j][i]);
}
printf("\n");
}
getch();
}
19/*program to find sum of diagonal elements of matrix*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[5][5],i,j,row,col,sum=0;
clrscr();
printf("\n enter the number of rows and columns of matrix:\n");
scanf("%d %d",&row,&col);
if(row!=col)
{
printf("the number of rows should be equal to columns");
exit(1);
}
else
{
printf("\n enter the elements of matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
}
printf("\nentered matrix is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
printf("\n the sum of diagonal is %d",sum);
getch();
}
20/* WAP to calculate the multiplication of two matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,m,n,p;
clrscr();
printf("enter the value of m,n & p=");
scanf("%d%d%d",&m,&n,&p);
if(m>5||n>5||p>5)
{
printf("Array is out of size");
exit(1);
}
{
printf("enter the elements of matrix 'A'=");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("enter the elements of matrix 'B'=");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
}
printf("\nelements of matrix 'A'=\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n elements of matrix 'B'=\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Multiplication is\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
21/*write a program to sort a list using bubble sort
technique*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter n");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("\t%d",&a[i]);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted list\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
}
22/*program to sort string containing name of person */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[10][20],dummy[10];
int n,i,j;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
printf("enter the string");
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%s",str[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(str[j],str[j+1])>0)
{
strcpy(dummy,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],dummy);
}
}
}
printf("list after sorting\n");
for(i=0;i<n;i++)
printf("\n%s",str[i]);
getch();
}
23/*program to find factorial using recursion*/
#include<stdio.h>
#include<conio.h>
long int fact(int);
void main()
{
int n;
long int factorial;
clrscr();
printf("enter value of n");
scanf("%d",&n);
factorial=fact(n);
printf("the factorial of number %d is %d ",n,factorial);
getch();
}
long int fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}
24/*program to calculate number of charaters,vowels,spaces in
a string*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[100],c,ch;
int i=0,j,ns=0,nv=0,nch;
clrscr();
printf("enter a string \n");
while((c=getchar())!='\n')
{
str1[i]=c;
i++;
}
str1[i]='\0';
for(j=0;str1[j]!='\0';j++)
{
ch=toupper(str1[j]);
if(ch==' '||ch=='\t')
ns++;
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
nv++;
}
nch=j-ns;
printf("\n no. of vowels are %d",nv);
printf("\n no. of space are %d",ns);
printf("\n no. of characters are %d",nch);
getch();
}
25/*program to check whether string is palindrome or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
char ch[80],c;
int i=0,t,c1,c0,flag=0;
clrscr();
printf("enter your string \n");
while((c=getchar())!='\n')
{
ch[i]=c;
i++;
}
ch[i]='\0';
t=i-1;
for((c1=0,c0=t);c1<=t/2;(c1++,c0--))
{
if(ch[c1]!=ch[c0])
{
flag++;
}
}
if(flag==0)
printf("string is palindrome");
else
printf("%s string is not palindrome",ch);
getch();
}
26/*program to find the reverse of a string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch[50],str1[50];
int c,j,i,count=0;
clrscr();
printf("enter a string");
scanf("%s",ch);
for(i=0;ch[i]!='\0';i++)
count++;
c=i-1;
for(i=c,j=0;i>=0;i--,j++)
str1[j]=ch[i];
printf("the reverse of string is %s",str1);
getch();
}
27/*program to concatenate string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[10],str2[10],str3[10],name[30];
int i,j,k;
clrscr();
printf("enter string 1\nstring2");
scanf("%s%s",&str1,&str2);
printf("enter string 3");
scanf("%s",&str3);
for(i=0;str1[i]!='\0';i++)
name[i]=str1[i];
name[i]=' ';
for(j=0;str2[j]!='\0';j++)
name[i+j+1]=str2[j];
name[i+j+1]=' ';
for(k=0;str3[k]!='\0';k++)
name[i+j+k+2]=str3[k];
name[i+j+k+2]='\0';
printf("\n\n");
printf("%s\n",name);
getch();
}
28/*WAP to input/output name,roll_no,marks, in six subject of a student
using structure*/
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
char name[26];
int roll_no;
char class[8];
int sub[7];
}student;
int i;
clrscr();
printf("enter students name,roll_no,class=");
scanf("%5s\t%5d\t%5s",student.name,&student.roll_no,student.class);
printf("enter the marks of ENG,MATH,DE,C,IT,CSA=");
for(i=0;i<6;i++)
{
scanf("%d",&student.sub[i]);
}
printf("\n name\troll_no\t\tclass\n\n");
printf("%5s\t%5d\t\t%5s",student.name,student.roll_no,student.class);
printf("\n\n\nENG\tMATH\tDE\tC\tIT\tCSA\n\n");
for(i=0;i<6;i++)
{
printf("%d\t",student.sub[i]);
}
getch();
}
29/*write a program to show the use of pointers*/
#include<stdio.h>
#include,conio.h>
void main()
{
int i=13;
int*a;
clrscr();
a=&i;
printf(" \n address of i=%u",&i);
printf(" \n address of i=%u",a);
printf(" \n address of a=%u",&a);
printf(" \n value of a=%u",a);
printf(" \n value of i=%d",i);
printf(" \n value of i=%d",*a);
printf(" \n value of i=%u",&i);
getch();
}
30/* WAP to perform the swapping using call by reference */
#include<stdio.h>
#include<conio.h>
int swap(int *x,int *y);
main()
{
int x=57,y=98;
clrscr();
printf(" before swapping=\n");
printf("\n the value of x=%d",x);
printf("\n the value of y=%d\n",y);
swap(&x,&y);
printf("\n after swapping=\n");
printf("\n the value of x=%d",x);
printf("\n the value of y=%d",y);
getch();
}
int swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
}
31 /*program
to sort a list using bubble sort technique and
dynamic memory allocation*/
#include<stdio.h>
#include<conio.h>
void main ()
{
int *a,i,j,n,temp;
clrscr();
printf("enter the number n");
scanf("%d",&n);
a=(int*)malloc(n);
printf("enter the elements");
for(i=0;i<n;i++)
scanf("%d",a+i);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(*(a+j)>*(a+j+1))
{
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;
}
}
}
printf("sorted list\n");
for(i=0;i<n;i++)
printf("%d",*(a+i));
getch();
}
32/* WAP to create file and display it to user */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
fp=fopen("try.txt","w");
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
fp=fopen("try.txt","r");
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}
33/* WAP to copy the content of one file to another file */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fs,*ft;
char ch;
fs=fopen("pr1.txt","r");
if(fs==NULL)
{
puts("cannot open the source file");
exit(1);
}
ft=fopen("pr2.txt","w");
if(ft==NULL)
{
puts("cannot open the source file");
exit(1);
}
while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
putc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}
34/* WAP to merge content of two files */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fs,*ft;
char ch;
fs=fopen("PR1.txt","r");
if(fs==NULL)
{
puts("cannot open the source file");
exit(1);
}
ft=fopen("pr2.txt","a");
if(ft==NULL)
{
puts("cannot open the source file");
exit(1);
}
while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
putc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}

You might also like