Laboratory File : Programming Laboratory in C
1
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C • S.N. 1.
INDEX PROGRAM Write a micro to find largest among number.
Page No. 14
2.
Write a program to find out nCr using function.
5
3.
Write a program to check the number it is prime or not.
8
4.
Write a program to simulate power function.
10
5.
Write a program to Armstrong number between given range.
4
6.
Write a program to build a calculator using switch case.
3
7.
Write a program to sort n numbers using bubble sort.
9
8.
Write a program to find out of multiplication of two matrixes. 6
9.
Write a program to count lines, word and characters.
19
10.
Write a program to print n terms of Fibonacci series using recursion. Write a program to alginate various bit operator.
12
13
15.
Write a program to SWP two numbers using pointer and function. Write a program to find out the addition of n numbers using command line argument. Write a program to create a file copy in to another file and display the 2nd file. Write a program to find the given string is palindrome or not.
16.
Write a program to merge two file in to 3rd file.
18
17.
Write a program to find out the addition of two complex numbers using structure function. Write a program to find the √ (root) of quadratic equation.
21
Write a program to create a display link list using dynamic memory a location.
22
11. 12. 13. 14
18. 19.
2
16
20 17 11
15
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 6th Program: Write a program to build a calculator using switch case. /* A program of calculator */ #include<stdio.h> #include
void main() { float a,b,add,sub,mult,div; int choice; clrscr();. printf("\n * * * * * * * * * *"); printf("\n * Press 1 for (+) *"); printf("\n * Press 2 for (-) *"); printf("\n * Press 3 for (*) *"); printf("\n * Press 4 for (/) *"); printf("\n * * * * * * * * * *"); printf("\n Enter your choice : "); scanf("%d",&choice); printf("\n Enter 1st value : "); scanf("%f",&a); printf("\n Enter 2nd value : "); scanf("%f",&b); switch(choice) { case 1: add=a+b; printf("\n Addition is : %f",add); break; case 2: sub=a-b; printf("\n Subtraction is : %f",sub); break; case 3: mult=a*b; printf("\n Multiplication is : %f",mult); break; case 4: div=a/b; printf("\n Division is : %f",div); break; default: printf("\n WARNING : You entered wrong choice key. Please try again !"); } getch(); } Screen Print of program: * * * * * * * * * * ** * Press 1 for (+) * * Press 2 for (-) * * Press 3 for (*) * * Press 4 for (/) * * * * * * * * * * * ** Enter your choice : 1 Enter 1st value : 22
3
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C Enter 2nd value : 21 Addition is : 43
4
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 5th Program: Write a program to Armstrong number between given range. /* A program for find out Armstrong numbers between given range */ #include<stdio.h> void main() { int a,b,x,y,z,i; clrscr(); printf("\n Enter starting range :"); scanf("\n %d",&a); printf("\n Enter ending range :"); scanf("\n %d",&b); printf("\n Armstrong numbers between %d to %d",a,b); for(i=a;i<=b;i++) { x=i%10; y=i%100; y=(y-x)/10; z=i/100; if((x*x*x)+(y*y*y)+(z*z*z)==i) printf("\n %d",i); } getch(); }
Screen Print of program: Enter starting range : 100 Enter ending range : 999 Armstrong Numbers between 100 to 999 153 370 371 407
5
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 2th Program:
Write a program to find out nCr using function. /*A program to find out the NCR using function*/ #include<stdio.h> int fact(int x) { int f=1, i; for(i=1;i<=x;i++) f=f*i; } void main() { int a,b,c,n,r,ncr; clrscr(); printf("Enter the value of n : "); scanf("%d",&n); printf("Enter the value of r : "); scanf("%d",&r); a=fact(n); b=fact(r); c=fact(n-r); ncr=a/(b*c); printf("\nThe value of NCR = %d",ncr); printf("\a\n\nPress any key for exit . . ."); getch(); }
Screen Print of program: Enter the value of n : 4 Enter the value of r : 2 The value of NCR = 6 Press any key for exit . . .
Explaining: 4! = 4*3*2*1 = 24 2! = 2*1 = 2 (4-2)! = 2! 2! = 2*1 = 2 n
Cr
= n!/r!(n-r)!
C2 = 4!/2!(42)!
4
4
C2 = 24/2*2 = 6
6
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 8th Program:
Write a program to find out of multiplication of two matrixes. /* A program to find out the multiplication of two matrix */ #include<stdio.h> #include void main() { int a[25][25]; int a1[25][25]; int a2[25][25]; int i,j,n,m,k,l,e; clrscr(); printf("Enter the size of first matrix \n"); scanf("%d%d",&n,&m); printf("\nEnter the elements of first matrix \n"); for(i=0;i
7
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C printf("\n"); } getch();
}
8
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C Screen Print of program: Example 1: Enter the size of first matrix 3 3 Enter the elements of first matrix 1 2 3 4 5 6 7 8 9 Enter the size of second matrix 3 3 Enter the elements of second matrix 9 8 7 6 5 4 3 2 1 First matrix you entered: 1 2 3 4 5 6 7 8 9 Second matrix you entered: 9 8 7 6 5 4 3 2 1 Multiplication of matrixes is: 30 24 19 84 69 54 138 114 90 Example 2: Enter the size of first matrix 2 2 Enter the elements of first matrix 2 2 2 2 Enter the size of second matrix 2 2 Enter the elements of second matrix 2 2 2 2 First matrix you entered: 2 2 2 2 Second matrix you entered: 2 2 2 2 Multiplication of matrixes is: 8 8 8 8
9
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 3rd Program:
Write a program to check the number it is prime or not. /* A program to find out the entered number is prime or not. */ #include<stdio.h> void main() { int n,i; clrscr(); printf("Please enter any number : "); scanf("%d",&n); for(i=2;i
Screen Print of program: First Example: Please enter any number: 5 # Entered number is a prime number.
Second Example: Please enter any number: 10 # Entered number is not a prime number.
10
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 7th Program: Write a program to sort n numbers using bubble sort. /* A program for sort n numbers using bubble sort */ #include<stdio.h> void printline(void) { int i; for(i=0;i<=40;i++) printf("="); }void main() { int i,j,arr[5],tem=0; clrscr(); printf("Enter any 5 values : \n"); for(i=1;i<=5;i++) { scanf("%d",&arr[i]); } for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { if(arr[j]>arr[j+1]) { tem=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tem; } } } printline(); printf("\n Entered values after arrangement: \n"); printline(); for(i=1;i<=5;i++) { printf("\n Serial Number is %d value is : %d \n",i,arr[i]); } printline(); getch(); } Screen Print of program: Enter any 5 values : 10 40 50 30 20 ========================================= Entered values after arrangement: ========================================= Serial Number is 1 value is : 10 Serial Number is 2 value is
: 20
Serial Number is 3 value is
: 30
Serial Number is 4 value is
: 40
Serial Number is 5 value is : 50 =========================================
11
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C
12
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 4th Program: Write a program to simulate power function. /* A program for get the value of any number's power */ #include<stdio.h> int power(int a, int b) { int i, c=1; for(i=1;i<=b;i++) c=c*a; return c; } void main() { int n,m,x; clrscr(); printf("\n Enter number : "); scanf("%d",&n); printf("\n Enter the power : "); scanf("%d",&m); x=power(n,m); printf("\n The value of %d^%d = %d",n,m,x); getch(); }
Screen Print of program: Enter number : 5 Enter the power : 2 The value of 5^2 = 25
13
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 15th Program: Write a program to find the given string is palindrome or not. /* A program to find out the entered string is palindrome or not */ #include<stdio.h> #include #include<string.h> void main() { int i,j,k; char name[20]; clrscr(); printf("\n Enter any String : "); scanf("%s",&name); j=strlen(name)/2; k=strlen(name)-1; for(i=0;i<j;i++) { if(name[i]!=name[k]) { printf("\n The string is not palindrome"); break; } k--; } if(j==k) printf("\n The string is palindrome"); getch(); }
Screen Print of program: First Example: Enter any String : madam The string is palindrome Second Example: Enter any String : hirdesh The string is not palindrome
14
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 10th Program: Write a program to print n terms of Fibonacci series using recursion. /*A program for print the Fibonacci series help of recursion*/ #include<stdio.h> void main() { int a=1,b=1,c; clrscr(); printf("Enter the length : "); scanf("%d",&c); printf("%d\n%d\n",a,b); feb(a,b,c); printf("\n"); printf("\aPress any key for exit . . ."); getch(); } feb(int f1, int f2, int no) { int f; f=f1+f2; printf("%d\n",f); if(f2>=no) return 0; feb(f2,f, no); }
Screen Print of program: First Example: Enter the length : 4 1 1 2 3 5 8 Press any key for exit . . .
Second Example: Enter the length : 6 1 1 2 3 5 8 13 Press any key for exit . . .
15
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 12th Program: Write a program to SWP two numbers using pointer and function. /*A program for swap two numbers using pointer and function*/ #include<stdio.h> swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; printf("\nValues of A and B after swapping : \n"); printf("\nA= %d",*a); printf("\nB= %d",*b); } void main() { int a,b,c; clrscr(); printf("\nEnter the value of A and B :\n"); printf("\nA= "); scanf("%d",&a); printf("B= "); scanf("%d",&b); swap(&a,&b); printf("\a\n\nPress any key for exit . . ."); getch(); }
Screen Print of program: First Example: Enter the value of A and B : A= 8 B= 6 Values of A and B after swapping : A= 6 B= 8 Press any key for exit . . .
Second Example: Enter the value of A and B : A= 47 B= 59 Values of A and B after swapping : A= 59 B= 47 Press any key for exit . . .
16
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C
17
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 1st Program: Write a program to find largest among number. /*A program for find the largest value*/ #include<stdio.h> #define max(x,y)(x>y?x:y) void main() { int a,b,c,p; clrscr(); printf("Enter any 3 values :\n"); scanf("%d%d%d",&a,&b,&c); p=max(a,max(b,c)); printf("\nLargest value is : %d",p); printf("\a\n\nEnter any key for exit . . . "); getch(); }
Screen Print of program: First Example: Enter any 3 values : 8 4 6 Largest value is : 8 Enter any key for exit . . .
Second Example: Enter any 3 values : 56 81 69 Largest value is : 81 Enter any key for exit . . .
18
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 18th Program: Write a program to find the root of quadratic equation is real or not. /*A program for find the root of quadratic equation is real or not*/ #include<stdio.h> void main() { int a,b,c,d; clrscr(); printf("Enter the value of a,b and c :\n"); scanf("%d %d %d",&a,&b,&c); d=(b*b)-(4*a*c); printf(" The value of d is : %d",d); if(d>0) { printf("\n Root is real"); } if(d<0) { printf("\n Root is imaginary"); } if(d==0) { printf("\n Root is equal"); } printf("\n\a Press any key for exit . . ."); getch(); }
Screen Print of program: 1st Example: Enter the value of a,b and c : 2 4 2 The value of d is : 0 Root is equal Press any key for exit . . . 2nd Example: Enter the value of a,b and c : 4 6 2 The value of d is : 4 Root is real Press any key for exit . . . 3rd Example: Enter the value of a,b and c : 4 5 8 The value of d is : -103 Root is imaginary
19
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C Press any key for exit . . .
20
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C th
11 Program: Write a program to alginate various bit operator. /*A program for bit operator*/ #include<stdio.h> void main() { int a,b; clrscr(); printf("Enter two numbers :\n"); scanf("%d %d",&a,&b); printf("---------------------------------------"); printf("\n a&b = %d",a&b); printf("\n<Shift Right> a>>2 = %d",a>>2); printf("\n<Shift Right> b>>2 = %d",b>>2); printf("\n<Shift Left> a<<2 = %d",a<<2); printf("\n<Shift Left> b>>2 = %d",b<<2); printf("\n a|b = %d",a|b); printf("\n ~a = %d",~a); printf("\n ~b = %d",~b); printf("\n !a = %d",!a); printf("\n !b = %d",!b); printf("\n<Exclusive NOT> a^b = %d",a^b); printf("\n---------------------------------------"); printf("\n\a Press any key for exit . . ."); getch(); }
Screen Print of program: Enter two numbers : 4 6 -------------------------------------- a&b = 4 <Shift Right> a>>2 = 1 <Shift Right> b>>2 = 1 <Shift Left> a<<2 = 16 <Shift Left> b>>2 = 24 a|b = 6 ~a = -5 ~b = -7 !a = 0 !b = 0 <Exilusive NOT> a^b = 2 --------------------------------------Press any key for exit . . .
21
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 14th Program:
Write a program to create a file copy in to another file and display the 2nd file.. /*A program for copy a file */ #include<stdio.h> #include void main() { FILE *fp1,*fp2; char ch; printf(“Enter the data : \n”); fp1=fopen("sourcef.txt","w"); while((ch=getchar())!=EOF) putc(ch,fp1); fclose(fp1); fp1=fopen("source.txt","r"); fp2=fopen("destination.txt","w"); while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp2); fclose(fp1); fp2=fopen("destination.txt","r"); while((ch=getc(fp2))!=EOF) printf("%c",ch); fclose(fp2); getch(); } Screen Print of program: Enter the data: Never thought I’m alive without honesty.
22
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 16th Program:
Write a program to merge two file in to 3rd file.. /*A program for merge two files in to 3rd file */ #include<stdio.h> void main() { FILE *f1,*f2,*f3; char a,b,c; f1=fopen("file1","w"); f2=fopen("file2","w"); f3=fopen("file3","w"); while((a=getchar())!=EOF) putc(a,f1); fclose(f1); while((b=getchar())!=EOF) putc(b,f2); fclose(f2); f1=fopen("file2","w"); while((a=getc(f1))!=EOF) putc(a,f3); fclose(f1); fclose(f3); f2=fopen("file2","r"); f3=fopen("file3","r"); while((c=getc(f2))!=EOF) putc(b,f3); fclose(f2); fclose(f3); f3=fopen("file3","r"); while((c=getc(f3))!=EOF) printf("%c",c); fclose(f3); getch(); }
Screen Print of program: My name is Hirdesh Singh Rajput I am an MCA student My name is Hirdesh Singh Rajput. I am an MCA student.
23
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 9th Program:
Write a program to count lines, word and characters. /*A program for count lines, word and characters */ #include<stdio.h> #include void main() { int ln=1,ch=0,wd=1,sp=0; char c,c1; clrscr(); printf("Enter the string : \n"); while(1) { c=getchar(); if(c==' ') { sp++; wd++; ch-- ; } if(c=='\0') ch--; if(c=='\n') { printf("\n"); c1=getchar(); if(c1=='\n') break; else { ln++; wd++; continue; }} ch++; } printf("\n*******File confrigration****** "); printf("\n Total no.of character=%d",ch); printf("\n Total no.of words=%d",wd); printf("\n Total no.of spaces=%d",sp); printf("\n Total no.of lines=%d",ln); printf("\n Please enter any key for exit ...\a"); getch(); } Screen Print of program: Enter the string : Education can change the view of the world But Knowledge can give the new view to the world (Devid Pol) *******File confrigration****** Total no.of character=81
24
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C Total no.of words=20 Total no.of spaces=16 Total no.of lines=4 Please enter any key for exit ...
25
Hirdesh Singh Rajput
Laboratory File : Programming Laboratory in C 13th Program:
Write a program to find out the addition of n numbers using command line argument.. /*A program for find out the addition of n numbers using commend line argument */ #include<stdio.h> #include void main(int argc,char*argv[]) { int i,s=0; printf("argc=%d\n",argc); for(i=0;i<argc;i++) { printf("argv[%d]=%s\n",i,argv[i]); s=s+atoi(argv[i]); } printf("\n the sum is=%d",s); getch(); }
Screen Print of program:
C:\TMP\soa 15 12 13 argc=4 argv[0]=C:\TMP\SOA.EXE argv[1]=15 argv[2]=12 argv[3]=13 the sum is=40
Note : Program will run on CMD
26
Hirdesh Singh Rajput