Upload
hirdesh
View
223
Download
1
Embed Size (px)
Citation preview
8/14/2019 Programming Laboratory in C
1/26
Laboratory File : Programming Laboratory in C
Hirdesh Singh Rajput1
8/14/2019 Programming Laboratory in C
2/26
Laboratory File : Programming Laboratory in C
I N D E X
S.N. PROGRAM Page No.
1. Write a micro to find largest among number. 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 usingrecursion.
12
11. Write a program to alginate various bit operator. 16
12. Write a program to SWP two numbers using pointer andfunction.
13
13. Write a program to find out the addition of n numbers usingcommand line argument.
20
14 Write a program to create a file copy in to another file anddisplay the 2nd file.
17
15. Write a program to find the given string is palindrome or not. 11
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.
21
18. Write a program to find the (root)of quadratic equation. 1519. Write a program to create a display link list using dynamic
memory a location.22
Hirdesh Singh Rajput2
8/14/2019 Programming Laboratory in C
3/26
Laboratory File : Programming Laboratory in C
6th
Program:
Write a program to build a calculator using switch case.
/* A program of calculator */
#include
#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
Hirdesh Singh Rajput3
8/14/2019 Programming Laboratory in C
4/26
Laboratory File : Programming Laboratory in C
Enter 2nd value : 21
Addition is : 43
Hirdesh Singh Rajput4
8/14/2019 Programming Laboratory in C
5/26
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
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
8/14/2019 Programming Laboratory in C
6/26
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
int fact(int x){
int f=1, i;
for(i=1;i
8/14/2019 Programming Laboratory in C
7/26
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
#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
8/14/2019 Programming Laboratory in C
8/26
Laboratory File : Programming Laboratory in C
printf("\n");
}
getch();
}
Hirdesh Singh Rajput8
8/14/2019 Programming Laboratory in C
9/26
Laboratory File : Programming Laboratory in C
Screen Print of program:
Example 1:
Enter the size of first matrix33Enter the elements of first matrix12
3456789Enter the size of second matrix33Enter the elements of second matrix987
654321First matrix you entered:
1 2 34 5 67 8 9
Second matrix you entered:9 8 76 5 43 2 1
Multiplication of matrixes is:30 24 1984 69 54138 114 90
Example 2:
Enter the size of first matrix22Enter the elements of first matrix2222Enter the size of second matrix22Enter the elements of second matrix2222First matrix you entered:
2 22 2
Second matrix you entered:2 22 2
Multiplication of matrixes is:8 88 8
Hirdesh Singh Rajput9
8/14/2019 Programming Laboratory in C
10/26
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. */
#includevoid main(){int n,i;clrscr();printf("Please enter any number : ");scanf("%d",&n);for(i=2;i
8/14/2019 Programming Laboratory in C
11/26
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
void printline(void)
{
int i;
for(i=0;i
8/14/2019 Programming Laboratory in C
12/26
Laboratory File : Programming Laboratory in C
Hirdesh Singh Rajput12
8/14/2019 Programming Laboratory in C
13/26
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
int power(int a, int b)
{
int i, c=1;
for(i=1;i
8/14/2019 Programming Laboratory in C
14/26
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
#include
#include
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
8/14/2019 Programming Laboratory in C
15/26
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*/
#includevoid 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 : 4112358
Press any key for exit . . .
Second Example:
Enter the length : 611235813
Press any key for exit . . .
Hirdesh Singh Rajput15
8/14/2019 Programming Laboratory in C
16/26
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
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= 8B= 6
Values of A and B after swapping :
A= 6B= 8
Press any key for exit . . .
Second Example:
Enter the value of A and B :
A= 47B= 59
Values of A and B after swapping :
A= 59B= 47
Press any key for exit . . .
Hirdesh Singh Rajput16
8/14/2019 Programming Laboratory in C
17/26
Laboratory File : Programming Laboratory in C
Hirdesh Singh Rajput17
8/14/2019 Programming Laboratory in C
18/26
Laboratory File : Programming Laboratory in C
1st
Program:
Write a program to find largest among number.
/*A program for find the largest value*/
#include
#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 :846
Largest value is : 8
Enter any key for exit . . .
Second Example:
Enter any 3 values :568169
Largest value is : 81
Enter any key for exit . . .
Hirdesh Singh Rajput18
8/14/2019 Programming Laboratory in C
19/26
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*/
#includevoid 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
8/14/2019 Programming Laboratory in C
20/26
Laboratory File : Programming Laboratory in C
Press any key for exit . . .
Hirdesh Singh Rajput20
8/14/2019 Programming Laboratory in C
21/26
Laboratory File : Programming Laboratory in C
11th
Program:
Write a program to alginate various bit operator.
/*A program for bit operator*/
#include
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 a>>2 = %d",a>>2);
printf("\n b>>2 = %d",b>>2);
printf("\n a>2 = %d",b>2 = 1
b>>2 = 1
a2 = 24
a|b = 6
~a = -5
~b = -7
!a = 0
!b = 0
a^b = 2
---------------------------------------
Press any key for exit . . .
Hirdesh Singh Rajput21
8/14/2019 Programming Laboratory in C
22/26
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
#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 Im alive without honesty.
Hirdesh Singh Rajput22
8/14/2019 Programming Laboratory in C
23/26
Laboratory File : Programming Laboratory in C
16th
Program:
Write a program to merge two file in to 3 rd file..
/*A program for merge two files in to 3rd file */
#include
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.
Hirdesh Singh Rajput23
8/14/2019 Programming Laboratory in C
24/26
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
#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
Hirdesh Singh Rajput24
8/14/2019 Programming Laboratory in C
25/26
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 ...
Hirdesh Singh Rajput25
8/14/2019 Programming Laboratory in C
26/26
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#include
void main(int argc,char*argv[])
{
int i,s=0;
printf("argc=%d\n",argc);
for(i=0;i