You are on page 1of 26

# Laboratory File : Programming Laboratory in C

## 1 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C

• INDEX
S.N. PROGRAM Page No.
1. Write a micro to find largest among number. 14

## 10. Write a program to print n terms of Fibonacci series using 12

recursion.
11. Write a program to alginate various bit operator. 16

## 12. Write a program to SWP two numbers using pointer and 13

function.
13. Write a program to find out the addition of n numbers using 20
command line argument.
14 Write a program to create a file copy in to another file and 17
display the 2nd file.
15. Write a program to find the given string is palindrome or not. 11

## 17. Write a program to find out the addition of two complex 21

numbers using structure function.
18. Write a program to find the √ (root) of quadratic equation. 15

## 19. Write a program to create a display link list using dynamic 22

memory a location.

## 2 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<conio.h>
void main()
{
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:
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 (/) *
* * * * * * * * * * **

## 3 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
Enter 2nd value : 21

## 4 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C

5th Program:

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

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

## Enter the value of n : 4

Enter the value of r : 2

## 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!(4­2)!
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<conio.h>
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<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);

## printf("\nEnter the size of second matrix \n");

scanf("%d%d",&k,&l);
printf("\nEnter the elements of second matrix \n");

for(i=0;i<k;i++)
for(j=0;j<l;j++)
scanf("%d",&a1[i][j]);

## printf(" First Matrix you entered:\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("\t %d ",a[i][j]);
printf("\n");
}

## printf(" Second Matrix you entered:\n");

for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
printf("\t %d ",a1[i][j]);
printf("\n");
}

## //Multiplication of these two matrix

for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
{
a2[i][j]=0;
for(e=0;e<k;e++)
{
a2[i][j]=a2[i][j] + (a[i][e] * a1[e][j]);
}
}
}
printf(" Multiplication of matrixes is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<l;j++)
printf("\t %d ",a2[i][j]);

## 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<n;i++)
{
if(n%i==0)
{
printf("# Entered number is not a prime number.");
break;
}
}
if(i==n)
{
printf("# Entered number is a prime number.");
}
getch();
}

First Example:

Second Example:

## 10 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
7th Program:

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

## Enter any 5 values :

10
40
50
30
20
=========================================
Entered values after arrangement:
=========================================
Serial Number is 1 value is : 10

## 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:

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

Enter number : 5

## 13 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
15th Program:

## /* A program to find out the entered string is palindrome or not */

#include<stdio.h>
#include<conio.h>
#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();
}

First Example:

Second Example:

## 14 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
10th Program:

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

First Example:

1
1
2
3
5
8

Second Example:

1
1
2
3
5
8
13

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

First Example:

A= 8
B= 6

A= 6
B= 8

Second Example:

A= 47
B= 59

A= 59
B= 47

## 16 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C

## 17 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
1st Program:

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

First Example:

## Enter any 3 values :

8
4
6

Largest value is : 8

Second Example:

## Enter any 3 values :

56
81
69

Largest value is : 81

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

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:

## /*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<AND Condition> 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<OR Condition> a|b = %d",a|b);
printf("\n<Negation> ~a = %d",~a);
printf("\n<Negation> ~b = %d",~b);
printf("\n<NOT Condition> !a = %d",!a);
printf("\n<NOT Condition> !b = %d",!b);
printf("\n<Exclusive NOT> a^b = %d",a^b);
printf("\n---------------------------------------");
printf("\n\a Press any key for exit . . .");
getch();
}

## Enter two numbers :

4
6
---------------------------------------
<AND Condition> a&b = 4
<Shift Right> a>>2 = 1
<Shift Right> b>>2 = 1
<Shift Left> a<<2 = 16
<Shift Left> b>>2 = 24
<OR Condition> a|b = 6
<Nagetion> ~a = -5
<Nagetion> ~b = -7
<NOT Condition> !a = 0
<NOT Condition> !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<conio.h>
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();
}

## 22 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
16th Program:

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

## My name is Hirdesh Singh Rajput

I am an MCA student

## 23 Hirdesh Singh Rajput

Laboratory File : Programming Laboratory in C
9th Program:

## /*A program for count lines, word and characters */

#include<stdio.h>
#include<conio.h>
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();
}

## 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<conio.h>
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