Você está na página 1de 56

CHANDIGARH COLLEGE OF ENGG.

&
TECH ,
SEC-26,CHD.

::PROGRAMMING FUNDAMENTAL PROGRAMS


FILE::
SUBMITTED BY :- TOSHAR BANSAL [ C014257 ]CIVIL
1ST SEM.
SUBMITTED TO :- JASPREET MAAM

INDEX
1. To Print hello.
2. To add,subtract,multiply,divide two numbers and find remainder
using 2 numbers.
3. To find simple interest.
4. To find roots of quadratic equation.
5. To find square root of a given number.
6. To swap two numbers using three variables.
7. To swap two numbers without using three variables.
8. To find the area of a rectangle.
9.
To find the sum of first and last digit of a four digit
number.
10.
To find the reverse of a 5 digit number.
11.
To find total marks and percentage.
12.
To find the greatest out of three numbers.
13.
To check whether the given year is leap year or not.
14.
To check whether a number is even or odd.
15. To check whether a character entered is in lowercase or uppercase
16.
To check whether a character entered is a special
symbol.
17.
To check whether a given number is prime or not.
18.
To find the division obtained by a student.
19.
To find the sum of all the digits of a 5 digit number.
20.
To find factorial of a given number.
21. To check whether the number is armstrong or not.
22.
To print all numbers from 1-20.
23.
To find one number power of the other.
24.
To print the fibonacci series.
25. To print the multiplication table of a given number.
26.
To print the following pattern.
27.
To print the following pattern.
28.
To swap two numbers using call by reference.
29.
To find factorial of a number using recursion.
30.
To print fibonacci series using recursion.
31.
To read and write a matrix.
32.
To add and subtract two matrices.
33.
To find transpose of a matrix.
34.
To find the multiplication of two matrices.
35. To store roll no. and marks of a student side by side in a matrix.
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

36. To find length of a string using string function.


37. To compare two strings using string functions.
38. To convert uppercase string into lowercase using string functions.
39.
To convert lowercase string into uppercase using
string functions.
40. To enter the age,tenure,salary.
41. To read structure and tenure shit
42. To read a program to reverse a string using pointers.
43. To read and display information about a student.
44. To delete all vowels from a string.
45. To write data on text file and read it.

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. - 1
Write a program to print hello.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(Hello);
getch();
}

Output
Hello

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. - 2
Write a program to find the addition, subtraction , multiplication, division
and remainder by using two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,add,sub,mul,div,rem;
printf(Enter any two numbers:);
scanf(%d%d,&a,&b);
add=a+b;

sub=a-b;

mul=a*b;

div=a/b;

rem=a%b;

printf(Addition = %d\nSubtraction = %d\nMultiplicatoin = %d\nDivision =


%d\nRemainder = %d,add,sub,mul,div,rem);
getch();
}

Output
Enter any two numbers:9 3
Addition = 12
Subtraction = 6
Multiplication = 27
Division = 3
Remainder = 0

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 3
Write a program to find the Simple Interest.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
float p,r,t,s;
printf(Enter the values of p, r and t:);
scanf(%f%f%f,&p,&r,&t);
s=(p*r*t)/100;
printf(Simple Interest = %f,s);
getch();
}

Output
Enter the values of p, r and t:70 30 15
Simple Interest = 315.000000

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 4
Write a program to find the roots of a quadratic equation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int a,b,c,d;
float x1,x2;
printf("enter the value of a,b & c\n");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf("Both roots are equal\n");
x1=-b/(2*a);
x2=x1;
printf("First Root x1= %f\n",x1);
printf("Second Root x2= %f\n",x2);
}
else if(d>0)
{
printf("Both roots are real and different\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("First Root x1= %f\n",x1);
printf("Second Root x2= %f\n",x2);
}
else
printf("Root are imeginary\n No Solution \n");
getch();
}
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Output
Enter the value of a,b & c
121
Both roots are equal
First Root x1= -1.000000
Second Root x2= -1.000000

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 5
Write a program to find the square root of a given number.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
int a;
float s;
printf(Enter any number:);
scanf(%d,&a);
s=sqrt(a);
printf(Square root of %d = %f,a,s);
getch();
}

Output
Enter any number:98
Square root of 98 = 9.899494

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 6
Write a program to swap two numbers using three variables.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c;
printf(Enter any two numbers:);
scanf(%d%d,&a,&b);
c=a;
a=b;
b=c;
printf(The numbers after swapping are a = %d & b = %d,a,b);
getch();
}

Output
Enter any two numbers:7 2
The numbers after swapping are a = 2 & b = 7

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 7
Write a program to swap two numbers without using three variables.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b;
printf(Enter any two numbers:);
scanf(%d%d,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(The numbers after swapping are a = %d & b = %d,a,b);
getch();
}

Output
Enter any two numbers:7 2
The numbers after swapping are a = 2 & b = 7
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

PRACTICAL NO. 8
Write a program to find the area of a rectangle.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int l,b,a;
printf(Enter the length and the breadth of rectangle:);
scanf(%d%d,&l,&b);
a=l*b;
printf(Area of rectangle = %d,a);
getch();
}

Ouput
Enter the length and breadth of rectangle:8 5
Area of rectangle = 40
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

PRACTICAL NO. - 9
Write a program to find the sum of first and last digit of a four digit number.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,s,c,d;
printf(Enter the four digit number:);
scanf(%d,&n);
c=a%10;
d=a/1000;
s=c+d;
printf(The sum of first and last digits of %d = %d,n,s);
getch();
}

Output
Enter the four digit number:2013
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

The sum of firsty and last digits of 2013 = 5

PRACTICAL NO. - 10
Write a program to find the reverse of a 5 digit number.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
float n,r,rev;
printf(Enter the 5 digit number:);
scanf(%f,&n);
rev=0;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf(Reverse of the number = %.0f,rev);
getch();
}

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

Output
Enter the 5 digit number:20131
Reverse of the number = 13102

PRACTICAL NO. - 11
Write a program to find total marks and percentage.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int m1,m2,m3,m4,total;
float per;
printf(Enter the marks of all the four subjects:);
scanf(%d%d%d%d,&m1,&m2,&m3,&m4);
total = m1+m2+m3+m4;
per= total/4;
printf(Total marks = %d \nPercentage = %.2f,total,per);
getch();
}

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

Output
Enter the marks of all the four subjects:90 89 87 94
Total marks = 360
Percentage = 90.00

PRACTICAL NO. 12
Write a program to find the greatest out of three numbers.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c,l;
printf(Enter all the three numbers:);
scanf(%d%d%d,&a,&b,&c);
if(a>b && a>c)
printf(%d is the greatest,a);
else if(b>a && b>c)
printf(%d is the greatest,b);
else
printf(%d is the greatest,c);
getch();
}

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

Output
Enter all the three numbers:40 33 20
40 is the greatest

PRACTICAL NO. 13
Write a program to check whether the given year is leap year or not.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int year,flag=1;
printf(Enter the required year till 2013:);
scanf(%d,&year);
if(year>2013)
flag=0;
if(flag)
{
if(year%4==0)
printf(%d is a leap year,year);
else
printf(%d is not a leap year,year);
}
else
printf(The year you have entered is wrong);
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

getch();
}

Output
Enter the required year till 2013:2000
2000 is a leap year

PRACTICAL NO. - 14
Write a program to check whether a number is even or odd.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a;
printf(Enter the number:);
scanf(%d,&a);
if(a%2==0)
printf(%d is even,a);
else
printf(%d is odd,a);
getch();
}

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

Ouput
Enter the number:168
168 is even

PRACTICAL NO. 15
Write a program to check whether a character entered is in lowercase or uppercase
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
clrscr();
char ch;
if(isupper(ch))
{
printf(The character entered is in uppercase);
}
else if(islower(ch))
{
printf(The character entered is in lowercase);
}
getch();
}
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Output
Enter the character:R
The character entered is in uppercase

PRACTICAL NO. 16
Write a program to check whether a character entered is a special symbol.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
clrscr();
char ch;
printf(Enter the character:);
scanf(%c,&ch);
if(isalpha(ch))
{
printf(The character is not a special symbol);
}
else if(isdigit(ch))
{
printf(The character is not a special symbol);
}
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

else
{
printf(The character is a special symbol);
}
getch();
}

Ouput
Enter the character:4
The character is not a special symbol

PRACTICAL NO. 17
Write a program to check whether a given number is prime or not.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,i,flag=1;
printf(Enter the number:);
scanf(%d,&n);
for(i=2;i<n;i++)
{
if(n%i==0)
flag=0;
}
if(flag)
printf(The number entered is prime);
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

else
printf(The number entered is not prime);
getch();
}

Output
Enter the number:17
The number entered is prime

PRACTICAL NO. 18
Write a program to find the division obtained by a student.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int p;
printf(Enter the percentage obtained by student:);
scanf(%d,&p);
if(p>85)
printf(Student has got Distincion);
else if(p>70)
printf(Student has got First Division);
else if(p>50)
printf(Student has got Second Division);
else if(p>30)
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(Student has got Low division);


else
printf(Student has failed);
getch();
}

Ouput
Enter the percentage obtained by student:93
Student has got distinction

PRACTICAL NO. 19
Write a program to find the sum of all the digits of a 5 digit number.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,r,s;
printf(Enter the 5 digit number:);
scanf(%d,&n);
s=0;
while(n!=0)
{
r=n%10;
s=s+r;
n=n/10;
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

}
printf(Sum = %d,s);
getch();
}

Output
Enter the 5 digit number:25143
Sum = 15

PRACTICAL NO. 20
Write a program to find factorial of a given number.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int f=1,n;
printf(Enter the number:);
scanf(%d,&n);
for(i=1;i<=n;i++)
f=f*i;
printf(Factorial of %d = %d,n,f);
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

getch();
}

Output
Enter the number:6
Factorial of 6 = 720

PRACTICAL NO. 21
Write a program to check whether the number is armstrong or not.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
}

Output
Enter a positive integer:250
250 is not an Armstrong number.

PRACTICAL NO. 22
Write a program to print all numbers from 1-20.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int i;
for(i=1;i<=20;i++)
printf(%d\n,i);
getch();
}
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PRACTICAL NO. 23
Write a program to find one number power of the other.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
int a,n,b;
printf(Enter the number:);
scanf(%d,&a);
printf(Enter the power:);
scanf(%d,&n);
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

b=pow(a,n);
printf(%d to the power %d = %d,a,n,b);
getch();
}

Output
Enter the number:2
Enter the power:6
2 to the power 6 = 64

PRACTICAL NO. 24
Write a program to print the fibonacci series.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,i,a=0,b=1,c=0;
printf("Enter Nth term of Fibonacci series:");
scanf("%d",&n);
printf("\n %d %d ",a,b);
for(i=2;i<n;i++)
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
printf("\n\n");
return(0);
getch();
}

Output
Enter the Nth term of Fibonacci series:10
0 1 1 2 3 5 8 13 21 34

PRACTICAL NO. 25
Write a program to print the multiplication table of a given number.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int n,i;
printf(Enter the number:);
scanf(%d,&n);
for(i=1;i<=10;i++)
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

{
printf( %d x %d = %d,n,i,n*i);
}
getch();
}

Output
Enter the number:5
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
PRACTICAL NO. 26
Write a program to print the following pattern.
1
12
123
1234
#include<stdio.h>
#include<conio.h>
main()
{
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

int i, j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
getch();
}

Output
1
12
123
1234

PRACTICAL NO. 27
Write a program to print the following pattern.
*
*

* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
main()
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
return 0;
getch();
}
Output
*
*

* * *
* * * *
* * * * *
PRACTICAL NO. 28
Write a program to swap two numbers using call by reference.
#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
main()
{
int a,b;
printf(Enter any two numbers:);
scanf(%d%d,&a,&b);
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(\nBefore swapping values:);


printf(a=%d & b=%d,a,b);
swaping(&a,&b);
printf(\nAfter swapping values:);
printf(a=%d & b=%d,a,b);
getch();
return 0;
}
void swaping(int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
}

Output
Enter any two numbers:10 20
Before swapping values:a=10 & b=20
After swapping values:a=20 & b=10
PRACTICAL NO. 29
Write a program to find factorial of a number using recursion.
#include<stdio.h>
#include<conio.h>
main()
{
int fact(int);
int n,c;
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(Enter any number:);


scanf(%d,&n);
c=fact(n);
printf(Factorial of %d = %d,n,c);
getch();
}
int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}

Output
Enter any two number:5
Factorial of 5 = 120

PRACTICAL NO. 30
Write a program to print fibonacci series using recursion.
#include<stdio.h>
#include<conio.h>
main()
{
int fib(int);
int n,c,i=0;
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(Enter any number:);


scanf(%d,&n);
printf(Fibonacci series\n);
for(c=1;c<=n;c++)
{
printf(%d\t,fib(i));
i++;
}
getch();
}
int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return (fib(n-1)+fib(n-2));
}
Output
Enter any two number:8
Fibonacci series
0 1 1 2 3 5 8 13
PRACTICAL NO. 31
Write a program to read and write a matrix.
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],m,n,i,j;
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(Enter the order of the matrix:);


scanf(%d%d,&m,&n);
printf(Enter the elements of matrix\n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&a[i][j]);
printf(The entered matrix\n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(%d\t,a[i][j]);
}
printf(\n);
}
getch();
}
Output
Enter the order of the matrix:3 2
Enter the elements of matrix
1 3 4
2 9 7
The entered matrix
1 3 4
2 9 7
PRACTICAL NO. 32
Write a program to add and subtract two matrices.
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],m,n,i,j;
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(Enter the order of the matrices:);


scanf(%d%d,&m,&n);
printf(Enter the elements of matrix a\n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&a[i][j]);
printf(Enter the elements of matrix b\n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
d[i][j] = a[i][j]-b[i][j];
}
printf(The addition of matrices\n);

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(%d\t,c[i][j]);
}
printf(\n);
}
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

printf(The subtraction of matrices\n);


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(%d\t,d[i][j]);
}
printf(\n);
}
getch();
}
Output
Enter the order of the matrices:3 2
Enter the elements of matrix a
6 9 7
5 8 1
Enter the elements of matrix b
4 2 5
8 7 3
The addition of matrices
10 11 12
13 15 4
The subtraction of matrices
2 7 2
-3 1 -2
PRACTICAL NO. 33
Write a program to find transpose of a matrix.
#include<stdio.h>
#include<conio.h>
main()
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

{
int a[10][10],m,n,i,j,b[10][10];
printf(Enter the order of the matrix:);
scanf(%d%d,&m,&n);
printf(Enter the elements of matrix\n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&a[i][j]);
printf(The matrix entered\n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(%d\t,a[i][j]);
}
printf(\n);
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
b[i][j] = a[j][i];
}
printf(Transpose of matrix\n);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(%d\t,b[i][j]);
}
printf(\n);
}
getch();
}
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Output
Enter the order of the matrix:2 3
Enter the elements of matrix
3 6
4 2
1 8
The matrix entered
3 6
4 2
1 8
Transpose of matrix
3 4 1
6 2 8

PRACTICAL NO. 34
Write a program to find the multiplication of two matrices.
#include<stdio.h>
#include<conio.h>
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

main()
{
int a[10][10],b[10][10],c[10][10],m,n,i,j,p,q,k,sum=0;
printf(Enter the order of the matrix a:);
scanf(%d%d,&m,&n);
printf(Enter the elements of matrix a\n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&a[i][j]);
printf(Enter the order of the matrix b:);
scanf(%d%d,&p,&q);
if(n!=p)
printf(Matrices with the entered orders can not be multiplied\n);
else
{
printf(Enter the elements of matrix b\n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(%d,&b[i][j]);
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++ )
{
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

for ( j = 0 ; j < q ; j++ )


printf("%d\t", c[i][j]);
printf("\n");
}
}
getch();
}

Output
Enter the order of the matrix a:2 2
Enter the elements of matrix a
1 3
2 4
Enter the order of the matrix b:2 2
Enter the elements of matrix b
3 2
4 2
Product of matrices
15 8
22 12

PRACTICAL NO. 35
Write a program to store roll no. and marks of a student side by side in
a matrix.
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

#include<stdio.h>
main()
{
int i,j,stud[4][2];
printf("Enter roll no. and marks:");
for(i=0;i<=3;i++)
{
scanf("%d %d",&stud[i][0],&stud[i][1]);
}
printf(The entered roll no. and marks are\n);
for(i=0;i<=3;i++)
{
printf("\n%d %d",stud[i][0],stud[i][1]);
}
}

Output
Enter roll no. and marks:
1 80
2 90
3 70
4 95
The entered roll no. and marks are
1 80
2 90
3 70
4 95

PRACTICAL NO. 36
Write a program to find length of a string using string function.
#include<stdio.h>
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

#include<conio.h>
#include<string.h>
main()
{

int length;
char str[100];
printf("Enter a string to calculate it's length\n");
gets(str);
length = strlen(str);
printf("Length of entered string is = %d\n",length);
getch();
}

Output
Enter a string to calculate it's length
Programming
Length of entered string is = 11

PRACTICAL NO. 37
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Write a program to show the use of strcpy() and strcat() functions.


#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char example[100];
strcpy(example,"Football ");
strcat(example,"is ");
strcat(example,"my ");
strcat(example,"life.");
puts(example);
getch();
}

Output
Football is my life.

PRACTICAL NO. 38
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Write a program to compare two strings using string functions.


#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if(strcmp(a,b)==0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
}

Output
Enter the first string
Rohan
Enter the second string
rohan
Entered strings are not equal.
PRACTICAL NO. 39
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Write a program convert uppercase string into lowercase using string


functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[30];
printf(Enter a string\n);
gets(str);
printf(After conversion from uppercase to lowercase the string becomes %s,strlwr(str));
getch();
}

Output
Enter a string
PROGRAMMING
After conversion from uppercase to lowercase the string becomes programming

PRACTICAL NO. 40
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Write a program convert lowercase string into uppercase using string


functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[30];
printf(Enter a string\n);
gets(str);
printf(After conversion from lowercase to uppercase the string becomes %s,strupr(str));
getch();
}

Output
Enter a string
programming
After conversion from uppercase to lowercase the string becomes PROGRAMMING
PRACTICAL NO. 41
Write structure and tenure shit
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
struct employee
{
char name[20];
int age;
int salary;
int tenure;
}emp[10];
printf(Enter the number of employees : );
scanf(%d,&n);
for(i=1;i<=n;i++)
{
printf(\nEnter the name of the employee %d ,i);
gets(emp[i].name);
printf(\nEnter the age, salary and tenure of employee %d ,i);
scanf(%d%d%d,&emp[i].age,&emp[i].salary,&emp[i].tenure);
}
printf(\nEmployees whose tenure is more than 3 years are :\n)
for(i=1;i<=n;i++)
{
if(emp[i].tenure>3)
puts(emp[i].name);
printf(\n);
}
getch();
}
Output
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Enter the number of employees : 6


Enter the name of the employee 1 john
Enter the age, salary and tenure of employee 1 30 30000 3
Enter the name of the employee 2 jack
Enter the age, salary and tenure of employee 2 32 30000 3
Enter the name of the employee 3 posner
Enter the age, salary and tenure of employee 3 45 40000 12
Enter the name of the employee 4 james
Enter the age, salary and tenure of employee 4 38 35000 7
Enter the name of the employee 5 zayn
Enter the age, salary and tenure of employee 5 40 38000 9
Enter the name of the employee 6 faheem
Enter the age, salary and tenure of employee 6 28 20000 2
Employees whose tenure is more than 3 years are :
Posner
James
zayn

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 42
Write a program to reverse a string using pointers.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
clrscr();
char str[100];
char *strp,l;
int i;
printf("Enter a String\n");
gets(str);
printf("The Reversed String is : ");
l=strlen(str);
strp=&str[l];
for(i=0;i<=l;i++)
{
printf("%c",*strp);
strp--;
}
getch();
}

Output
Enter a string
rohan
The Reversed String is : nahor
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

PRACTICAL NO. 43
Write a program to read and display information about a student.
#include<stdio.h>
#include<conio.h>
main()
{
struct student
{
char name[40];
int rollno;
int marks;
}std;
printf(Enter the information about student\n);
printf(Enter the name : );
gets(std.name);
printf(Enter the roll number : );
scanf(%d,&std.rollno);
printf(Enter the marks : );
scanf(%d,&std.marks);
printf(\nDisplaying information);
printf(The name of the student : );
puts(std.name);
printf(The roll number of student : %d,std.rollno);
printf(The marks of the student : %d,std.marks);
getch();
}

Output
Enter the information about student
Enter the name : adele
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Enter the roll number : 43


Enter the marks : 98
Displaying information
The name of the student : adele
The roll number of student : 43
The marks of the student : 98

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

PRACTICAL NO. 44
Write a program to delete all vowels from a string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[20],s[20];
int i,j=0;
printf("Enter any string\n");
gets(str);
printf("The string is : %s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u' ||str[i]=='A'||
str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
str[i]=' ';
else
s[j++]=str[i];
}
s[j]='\0';
printf("\nThe string without vowel is : %s",s);
return 0;
}

Output
Enter any string
rohan
The string is : rohan
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

The string without vowel is : rhn

PRACTICAL NO. 45
Write a program to write data on text file and read it.
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
char s[80];
fp=fopen(essay.txt,w);
if(fp==NULL)
{
puts(Cannot open file);
exit(1);
}
printf(\nEnter a few lines of text:\n);
while(strlen(gets(s)) > 0)
{
fputs(s,fp);
fputs(\n,fp);
}
fclose(fp);
}

Output
TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST
SEM..

Enter a few lines of text:


I am new to this college.
And the work given here is very vast.
I am in love with football.

TOSHAR BANSAL [ C014257 ] | CIVIL, 1ST


SEM..

Você também pode gostar