Main

http://open.popnc.com/pnc/16280

Sunday, 22 December 2019

C Programs with Answer part 2


34]     Program to find multiplication of two matrices.

#include<stdio.h> #include<conio.h>
void main()
{
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
printf(“enter value for 1 matrix: ”); for(i=0;i<3;i++)
{
for(j=0;j<2;j++) scanf(“%d”,&a[i][j]);
}
printf(“enter value for 2 matrix: ”); for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
 c[i][j]=a[i][j]*b[i][j];
}
printf(“matrix is\n”); for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d ”,c[i][j]);
}
printf(“\n”);
}
getch();
}

Output:

enter value for 1 matrix: 7
8
9
4
5
6
enter value for 2 matrix: 3
2
1
2
5
6
matrix is

21
16
9
8
25
36



35]     Program to find transpose of a matrix.

#include<stdio.h> #include<conio.h>
void main()
{
int a[3][2],b[2][3],i,j; clrscr();
printf(“Enter value for matrix: ”); for(i=0;i<3;i++)
{
for(j=0;j<2;j++) scanf(‘%d”,&a[i][j]);
}
printf(“Matrix:\n”); for(i=0;i<3;i++)
{
for(j=0;j<2;j++) 
printf(“ %d ”,a[i][j]); printf(“\n”);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
 b[j][i]=a[i][j];
}
printf(“Transpose matrix:\n”); for(i=0;i<2;i++)
{
for(j=0;j<3;j++) 
printf(“ %d ”,b[i][j]);
 printf(“\n”);
}
getch();
}



Output:

Enter value for matrix: 4 5
6
1
2
3
Matrix:
4 5
6 1
2 3
Transpose matrix:
4 6 2
5 1 3

36]  Program to find the maximum number in array using pointer.

#include<stdio.h> #include<conio.h> 
void main()
{
int max,i,*a[5]; clrscr();
printf(“enter element for the array: ”); for(i=0;i<5;i++)
scanf(“%d”,&*a[i]); max=*a[0];
for(i=1;i<5;i++)
{
if(max<*a[i]) max=*a[i];
}
printf(“maximum no= %d”,max); getch();
}



Output:

enter elements for array: 5 4
7
1
2
maximum no= 7

37]  Program to show input and output of a string.

#include<stdio.h> #include<conio.h>
void main()
{
char a[50];
clrscr();
printf(“enter any string: ”); gets(a);
puts(a);
getch();
}

Output:

enter any string: hi everyone hi     everyone

38]  Program to find square of a number using functions.

#include<stdio.h> #include<conio.h> 
void main()
{
int rev(int); int r,a; clrscr();
printf(“enter any no: ”); scanf(“%d”,&a); r=rev(a);
printf(“square is : %d”,r);
getch();
}
int rev(int x)
{
return(x*x);
}

Output:

enter any no: 5 
square is : 25

39]   Program to swap two numbers using functions.

#include<stdio.h> #include<conio.h> 
void main()
{
void swap(int,int); 
int a,b,r;
clrscr();
printf(“enter value for a&b: ”); scanf(“%d%d”,&a,&b); swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp; temp=a; a=b; b=temp;
printf(“after swapping the value for a & b is : %d %d”,a,b);
}

Output:

enter value for a&b: 4 5
after swapping the value for a & b : 5 4



40]  Program to find factorial of a number using functions.

#include<stdio.h> #include<conio.h>
void main()
{
int a,f;
int fact(int); clrscr();
printf(“enter a no: ”); scanf(“%d”,&a); f=fact(a); printf(“factorial= %d”,f); getch();
}
int fact(int x)
{
int fac=1,i; for(i=x;i>=1;i--) fac=fac*i; return(fac);
}

Output:

enter a no: 5 factorial=120

41]   Program to show table of a number using functions.

#include<stdio.h> #include<conio.h>
void main()
{
void table();
 clrscr();
table();
getch();
}
              void table()
{
int n,i,r;
printf(“enter a no to know table: ”); scanf(“%d”,&n); 
for(i=1;i<=10;i++)
{
r=n*i; 
printf(“%d*%d=%d\n”,n,i,r);
}
}

Output:

enter a no to know table: 2 2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

42]   Program to show call by value.

#include<stdio.h> #include<conio.h> 
void main()
{
int a,b,swap(); 
clrscr();
a=5; b=10;
printf(”value of a=%d & value of b=%d before swap ”,a,b);
 swap(a,b);
printf(“\nvalue of a =%d & b=%d after swap”,a,b); 
getch();
}
int swap(int x,int y)
{
int temp; temp=x; x=y; y=temp;
}

Output:

value of a=5 & value of b=10 before swap value of a=5 & b=10 after swap

43]   Program to show call by reference.

#include<stdio.h> #include<conio.h>
void main()
{
int a,b,*aa,*bb,swap(); clrscr();
a=5; b=10;
aa=&a; bb=&b;
printf(“value of a= %d & value of b=%d before swap”,a,b); swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after swap”,a,b); getch();
}
int swap(int *x,int *y)
{
int temp; temp=*x;
*x=*y;
*y=temp;
}

Output:

value of a= 5 & value of b=10 before swap value of a=10 & b=5 after swap

44]Program to find largest of two numbers using functions.

#include<stdio.h> #include<conio.h>
void main()
{
void max(); 
clrscr();
max();
getch();
}
void max()
{
int a[5],max,n,i;
printf(“How many no’s you want to enter: ”); scanf(“%d”,&n);
printf(“Enter element for the array: ”); for(i=0;i<n;i++)
scanf(“%d”,&a[i]); max=a[0]; for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf(“maximum no= %d”,max);
}



Output:

How many no’s you want to enter: 4 Enter element for array: 4
5
6
1
maximum no: 6

45]  Program to find factorial of a number using recursion.

#include<stdio.h> #include<conio.h>
void main()
{
int n; 
clrscr();
printf(“enter number: ”); scanf(“%d”,&n); 
if(n<0)
printf(“invalid number”);
else
 printf(“%d!=%d”,n,fact(n)); getch();
}
int fact(int x)
{
if(x==0) 
return 1; 
else
return(x*fact(x-1));
}

Output:

enter number: 5
5!=120

46]  Program to find whether a string is palindrome or not.

#include<stdio.h> #include<conio.h>
void main()
{
char s1[20],s2[20];
 clrscr();
printf(“enter a string: ”); scanf(“%s”,s1); strcpy(s2,s1); strrev(s2); if(strcmp(s1,s2)==0)
printf(“string is a palindrome”);
 else
printf(“not a palindrome string”); getch();
}

Output:

enter a string: abc not a palindrome string
47.              File Operations: #include<stdio.h> #include<conio.h>
void main()
{
file *fp,*fp1; 
char c; 
clrscr();
fp=fopen(“test.c”,”w”);
printf(“\nenter the contents for file1(#.end)\n”);
 c=getchar();
while(c!=’#’)
{
fputc(c,fp); c=getchar();
}
rewind(fp); fp=fopen(“test.c”,”r”);
fp1=fopen(“tes.c”,”w”); c=fgetc(fp); while(c!=eof)
{
fputc(c,fp); c=fgetc(fp);
}
fclose(fp); fclose(fp1);
fp1=fopen(“tes.c”,”r”); c=fgetc(fp1);
printf(“\nthe contents in file 2\n”); while(c!=eof)
{
putchar(c); c=fgetc(fp1);
}
fclose(fp1); getch();
}

Output:

enter the contents of file1(#-end) good morning#
the contents of file2 good morning

48.             Merging One Dimensional Array Excluding The Repeating Element

#include<stdio.h> #include<conio.h>
void main()
{
int a[50],b[50],n1,n2,i,x; clrscr();
printf(“enter the number of elements in the first array”); scanf(“%d”,&n1);
printf(“enter the elements\n”);

for(i=0;i<n1;i++)
{
printf(“enter a[%d]”,i+1);
scanf(“%d”,&a[i]);
}
printf(“enter the number of elements in the second array”); scanf(“%d”,&n2);
printf(“enter the elements\n”); for(i=0;i<n2;i++)
{
printf(“enter b[%d]”,i+1);
scanf(“%d”,&b[i]);
}
for(x=0;x<n1;x++)
{
for(i=0;i<n2;i++)
{
if(b[i]==a[x])
{
b[i]=’ ‘;
}
}
}
for(i=o;i<n1;i++)
{
printf(“%d”,a[i]);
}
for(i=0;i<n2;i++)
{
if(b[i]==’ ‘;) continue; else
printf(“%d”,b[i]);
}
getch();
}



Output:

Enter the number of elements in the first array 3
Enter the elements 3
5
7
Enter the number of elements in the first array 3
Enter the elements 2
5
9
3 5 7 2 9

49.          Number Of Occurrences Of Vowels, Consonants, Words, Spaces And Special Characters In The Given Statement.

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> 
void main()
{
char s[100];
int vow=0,cons=0,spc=0,punc=0,l,i; clrscr();
printf(“enter the statement\n”); gets(s);
l=strlen(s); for(i=0;i<l;i++)
{
if(isalpha(s[i]))
{
if(s[i]==’a’||s[i]==’e’||s[i]==’i’||s[i]==’o’||s[i]==’u’)
{



vow++;
}
else
{
cons++;
}
}
if(isspace(s[i])
{
spc++;
}
if(ispunct(s[i]))
{
punc++;
}
}
printf(“\nno.of words=%d”,spc+1);
 printf(“\nno.of vowels=%d”,vow);
 printf(“\nno.of consonants=%d”,cons); printf(“\nno.of space=%d”,spc);
 printf(“\nno.on special characters=%d”,punc); getch();
}


Output:

Enter the statement
*Nothing is impossible in the world. No.of words=6
No.of vowels=10 No.of consonants=19 No.of space=5
No.of special characters=1



50.          Write a program to create enumerated data type for 12 months. Display their values in integer constants.

#include<stdio.h> #include<conio.h> 
void main()
{
Enum month(Jan, Feb, Mar, Apr, May, June, July, Aug,Sep, Oct, Nov, Dec) clrscr();
printf(“Jan=%d”, Jan); printf(“Feb=%d”, Feb); printf(“June=%d”, June); printf(“Dec=%d”, Dec);
}

Output:

Jan =0 Feb=1 June=5 Dec=11

No comments:

Post a Comment

C Language Overview

C Language Overview T he C programming language is a general-purpose, high-level language that was originally developed by Dennis M. Ri...