Anda di halaman 1dari 19

Assignment Programming in C

Q. WAP to check entered string is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch[20],ch1[20];
clrscr();
printf("Enter the String\n");
gets(ch);
strcpy(ch1,ch);
strrev(ch1);
if((strcmp(ch,ch1))==0)
{
printf("Palindrome\n");
}
else
{
printf("Not Palindrome\n");
}
getch();
}

Output:

Assignment Programming in C

Q. WAP to check the entered character is vowel or not


#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("Enter a character\n");
scanf("%c",&c);
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
{
printf("%c is a Vowel\n",c);
}
else
{
printf("%c is not a Vowel\n",c);
}
getch();
}

Output:

Assignment Programming in C

Q. WAP to check the entered number is Armstrong or not.


#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0,temp,rem;
clrscr();
printf("Enter the number you want to check:\n");
scanf("%d",&num);
temp=num;
while(temp!=0)
{
rem=temp%10;
sum=sum+rem*rem*rem;
temp = temp/10;
}
if(num==sum)
printf("Number is an armstrong number\n");
else
printf("Number is not an armstrong number\n");
getch();
}

Output:

Assignment Programming in C

Q. WAP to find Fibonacci series.


#include<stdio.h>
#include<conio.h>
#include<conio.h>
long int fibbo(int x);
void main()
{
int n,i;
clrscr();
printf("Enter the number of terms in series\n");
scanf("%d",&n);
printf("Fibonacci series:\n");
for(i=1;i<=n;i++)
{
printf("%ld\t",fibbo(i));
}
getch();
}
long int fibbo(int x)
{
if(x==1 || x==0)
return 1;
else
{
return(fibbo(x-1)+fibbo(x-2));
}
}
Output :

Assignment Programming in C

Q. WAP to print the following pattern :


*********
*******
*****
***
*
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,row;
clrscr();
printf("Enter the number of rows you want:\n");
scanf("%d",&row);
for(i=row;i>=1;i--)
{
for(j=row;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
getch();
}

Output:

Assignment Programming in C

Q. WAP to print the following pattern (Floyd's Triangle):


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
#include<stdio.h>
#include<conio.h>
void main()
{
int row,i,j,c=1;
clrscr();
printf("Enter the number of rows you want: \n");
scanf("%d",&row);
for(i=1;i<=row;i++)
{
for(j=1;j<=i;j++)
{
printf("%3d ",c);
c++;
}
printf("\n");
}
getch();
}
Output:

Assignment Programming in C

Q. WAP to solve Tower of Hanoi problem.


#include<stdio.h>
#include<conio.h>
void toh(int,char,char,char);
void main()
{
int n=3;
clrscr();
toh(n,'A','B','C');
getch();
}
void toh(int n,char a,char b,char c)
{
if(n==1)
{
printf("\nMoved from %c to %c",a,c);
}
else
{
toh(n-1,a,c,b);
toh(1,a,' ',c);
toh(n-1,b,a,c);
}
}
Output:

Assignment Programming in C

Q. WAP to calculate sum of A.P. series


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,d,n,i,nth;
int sum=0;
clrscr();
printf("Enter the first number of the series: ");
scanf("%d",&a);
printf("Enter the number of terms in series: ");
scanf("%d",&n);
printf("Enter the common difference of series: ");
scanf("%d",&d);
sum=(n*(2*a+(n-1)*d))/2;
nth=a+(n-1)*d;
printf("Sum of the A.P. is:\n");
for(i=a;i<=nth;i=i+d)
{
if(i!=nth)
{
printf("%d + ",i);
}
else
{
printf("%d = %d ",i,sum);
}
}
getch();
}
Output:

Assignment Programming in C

Q. WAP to convert decimal number to binary


#include<stdio.h>
#include<conio.h>
void binary(int x);
void main()
{
int a,b;
clrscr();
printf("Enter the number\n");
scanf("%d",&a);
printf("Number in Binary is:\t");
binary(a);
getch();
}
void binary(int x)
{
int r;
r=x%2;
x=x/2;
if(x==0)
{
printf("%d",r);
}
else
{
binary(x);
printf("%d",r);
}
}
Output:

Assignment Programming in C

Q. WAP to multiply two matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,i,j,k,sum=0;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter the no. of rows and columns of matrix 1\n");
scanf("%d\n%d",&m,&n);
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the no. of rows and columns of matrix 2\n");
scanf("%d\n%d",&p,&q);
if(n!=p )
{
printf("Matrices can not be multiplied.\n");
}
else
{
printf("Enter the elements of second matrix\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
clrscr();
printf("Matrix 1 is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
10

Assignment Programming in C

}
printf("\n");
}
printf("Matrix 2 is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
}
printf("Product of matrices:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
}
Output :

11

Assignment Programming in C

Q. WAP to find largest element in array


#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int a[MAX];
int i,n,max=0;
clrscr();
printf("Enter number of elements (max. 20)\n");
scanf("%d",&n);
printf("Enter the values:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("Maximum value in Array = %d",max);
getch();
}
Output :

12

Assignment Programming in C

Q. WAP to reverse a string using pointers


#include<stdio.h>
#include<conio.h>
char rev(char s[50],int ,int );
void main()
{
int i,l;
char a[50],b[50];
clrscr();
printf("Enter the string\n");
gets(a);
l=strlen(a);
rev(a,0,l-1);
printf("Reverse string is:");
puts(a);
getch();
}
char rev(char *s,int beg,int end)
{
char p;
if(beg>=end)
{
return 0;
}
else
{
p=*(s+beg);
*(s+beg)=*(s+end);
*(s+end)=p;
return rev(s,++beg,--end);
}
}
Output:

13

Assignment Programming in C

Q. WAP to calculate area & volume of a cylinder


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float r,h;
float sa,volume;
clrscr();
printf("Enter radius and height of cylinder:\n");
scanf("%f\n%f",&r,&h);
sa=2*(3.141)*r*(r+h);
volume=(3.141)*r*r*h;
printf("Surface area of cylinder is: %4f",sa);
printf("\nVolume of cylinder is : %.4f",volume);
getch();
}
Output :

14

Assignment Programming in C

Q. WAP to count characters, lines, spaces & tabs in a file


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char a[20];
int nol=0,not=0,nob=0,noc=0;
char c;
clrscr();
printf("Enter the name of File:\n");
gets(a);
if((fp=fopen(a,"r"))==NULL)
{
printf("File dosen't exist.");
}
else
{
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
noc++;
if(c==' ')
nob++;
if(c=='\n')
nol++;
if(c=='\t')
not++;
}
}
fclose(fp);
printf("Number of characters = %d\n",noc);
printf("Number of blanks = %d\n",nob);
printf("Number of tabs = %d\n",not);
15

Assignment Programming in C

printf("Number of lines = %d\n",nol);


getch();
}
Output :

Q. WAP to merge 2 files in C


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fs1,*fs2,*ft;
char ch,file1[20],file2[20],file3[20];
clrscr();
printf("Enter name of first file\n");
gets(file1);
printf("Enter name of second file\n");
gets(file2);
printf("Enter name of file which'll store contents:\n");
gets(file3);
fs1=fopen(file1,"r");
fs2 = fopen(file2,"r");
16

Assignment Programming in C

if(fs1==NULL || fs2==NULL )
{
printf("Error opening file\n");
getch();
exit(1);
}
ft=fopen(file3,"w");
if(ft==NULL )
{
printf("Error opening file\n");
exit(1);
}
while((ch=fgetc(fs1))!=EOF)
fputc(ch,ft);
while((ch=fgetc(fs2))!=EOF)
fputc(ch,ft);
printf("Two files were merged into %s file \n",file3);
fclose(fs1);
fclose(fs2);
fclose(ft);
getch();
}
Output :

17

Assignment Programming in C

Q. C program to use structure within union & display the


contents of structure elements
#include<stdio.h>
#include<conio.h>
void main() {
struct student {
char name[30];
char sex;
int rollno;
float percentage;
};
union details {
struct student st;
};
union details set;
printf("Enter details:");
printf("\nEnter name : ");
scanf("%s", set.st.name);
printf("\nEnter roll no : ");
scanf("%d", &set.st.rollno);
flushall();
printf("\nEnter sex : ");
scanf("%c", &set.st.sex);
printf("\nEnter percentage :");
scanf("%f", &set.st.percentage);
printf("\nThe student details are : \n");
printf("\name : %s", set.st.name);
printf("\nRollno : %d", set.st.rollno);
printf("\nSex : %c", set.st.sex);
printf("\nPercentage : %f", set.st.percentage);
getch();
}

18

Assignment Programming in C

Output :
Enter details:
Enter name : Pritesh
Enter rollno: 10
Enter sex: M
Enter percentage: 89
The student details are:
Name : Pritesh
Rollno : 10
Sex : M
Percentage : 89.000000

Q.

19

Anda mungkin juga menyukai