CP-I (ES113) - Lab Manual - Mechanical Engg. Dept.

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

DEPARTMENT OF MECHANICAL ENGINEERING

FACULTY OF TECHNOLOGY
DHARMSINH DESAI UNIVERSITY

LAB MANUAL
OF
COMPUTER PROGRAMMING (ES113)
SEMESTER-I

Name: _________________________________________________________

Roll No.: _______________________ ID No. ________________________

Branch: ________________________ Division ______________________


CERTIFICATE

This is to certify that the term-work carried out in the

subject of Computer Programming (ES113) and recorded

in this journal is the bonafide work of Mr./Miss

________________________________________________________

Roll No: _______________ ID No: _____________________of

B. Tech. semester-I in the branch of Mechanical

Engineering during the academic year _____________.

LAB TEACHER HEAD OF DEPARTMENT

DATE: DATE:
INDEX

SR. TITLE PAGE DATE OF DATE OF SIGN.


NO. NO. PRACTICAL REPORT

1 BASIC C PROGRAMS WITH


STANDARD INPUT AND OUTPUT
FUNCTIONS.

2 PROGRAMS TO LEARN USE OF C


OPERATORS FOR MAKING THE
EXPRESSIONS.

3 PROGRAMS TO LEARN
FORMATTING OF INPUT AND
OUPUT VALUES.

4 PROGRAMS TO LEARN DECISION


MAKING AND BRANCHING.

5 PROGRAMS TO LEARN DECISION


MAKING AND LOOPING.

6 PROGRAMS TO LEARN SINGLE


AND TWO-DIMENSIONAL ARRAY.

7 PROGRAMS TO LEARN
CHARACTER ARRAY (STRING).

8 PROGRAMS TO LEARN USER


DEFINED FUNCTION.

9 PROGRAMS TO LEARN BASIC OF


POINTER.

10 PROGRAMS TO LEARN USE OF


STRUCTURE.

11 PROGRAMS TO LEARN USE OF


FILE HANDLING IN C LANGUAGE.
LAB-1

BASIC C PROGRAMS WITH STANDARD INPUT AND OUTPUT FUNCTIONS.

Objectives:

1. Basic understanding of C programming.


2. How to compile and execute a C program.
3. How to use different escape sequence/backslash character.
4. To learn how to assign value to the variable.
5. To learn run time and compile time program.

Program 1: Write program to print DDU address using function printf().

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Dharmsinh Desai University, College Road, Nadiad, Kheda");
getch();
}
Program 2: Modify above program to print DDU address as per post-envelope
using ‘\n’ backslash character.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Dharmsinh Desai University\nCollege Road\nNadiad\nKheda");
getch();
}
Program 3: Write a program to print “Let’s Learn C” using appropriate
backslash character.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\"Let's Learn C\"");
getch();
}
Program 4: Write a compile time program to print value of integer, float and
character variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b;
char c;
clrscr();
a=10;
b=99.99;
c='$';
printf("a=%d\nb=%f\nc=%c",a,b,c);
getch();
}
Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________

Program 5: Write a program to calculate area of a circle for radius entered by


user.
#include<stdio.h>
#include<conio.h>
void main()
{
float radius, area;
clrscr();
printf("\nEnter the radius of Circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf("\nArea of Circle : %f", area);
getch();
}
Output:

___________________________________ ___________________________________
___________________________________ ___________________________________

Assignment Programs:

1. Write a C program to print followings outputs using backslash character.


a) “Hello”
b) How are you?
c) \\ single line comment in C
d) // invalid syntax for comment in C

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
2. Write a Program to read the following input using scanf() statements and
print in same format. i) 01/09/2010 ii) 324 A 13.67

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
3. Predict the output of the following:

void main()
{
enum fruits
{
orange, banana, mango=4, peach
};
printf(“ \npeach=%d”, peach);
}

Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
LAB-2

PROGRAMS TO LEARN USE OF C OPERATORS FOR MAKING THE


EXPRESSIONS.

Objectives:

1. To learn different operators to perform certain mathematical or logical


manipulation.
2. How to deal with mathematical expression.
3. Understanding of type conversion in Expressions.

Program 1: Write a program to display minutes and seconds, where input


time is in decimal hours during run time program.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,s;
float h;
clrscr();
printf("Enter the time in decimal hours:");
scanf("%f",&h);
n=h*3600;
m=n/60;
s=n%60;
printf("\nMinutes=%d\nSeconds=%d",m,s);
getch();
}
input: Output:

12.5 hours ___________________________________________


4.25 hours ___________________________________________
6.1 hours ___________________________________________

Program 2: Write program to print the following


1234
234
34
4
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Enter a number of four digit:");
scanf("%d",&a);
b=a%1000;
c=a%100;
d=a%10;
printf("\n%d\n%d\n%d\n%d",a,b,c,d);
getch();
}

input: Output:

5678 ___________________________________________
___________________________________________
___________________________________________
___________________________________________

Program 3: Write program to read three integer values and print following
results:
(i) Sum of the values
(ii) Average of the three values
(iii) Largest of the three values
(iv) Smallest of the three values

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum,max,min;
float avg;
clrscr();
printf("Enter the value of a,b and c:");
scanf("%d %d %d",&a,&b,&c);
sum=a+b+c;
avg=(float)(a+b+c)/3;
max=a>b?a:b;
max=max>c?max:c;
min=a<b?a:b;
min=min<c?min:c;
printf("\nsum=%d\navg=%f\nmax=%d\nmin=%d",sum,avg,max,
min);
getch();
}
input: Output:

45,13,27 ___________________________________________
___________________________________________
___________________________________________
___________________________________________

Program 4: Write a program to find real roots of a quadratic equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float delta,x1,x2;
clrscr();
printf("Enter the value of a,b and c:");
scanf("%d %d %d",&a,&b,&c);
delta=pow(b,2)-4*a*c;
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
printf("\nx1=%f\nx2=%f",x1,x2);
getch();
}
input: Output:

quadratic eqn ___________________________________________


x2+9x+18 ___________________________________________
a=1, b=9, c=18
Assignment Programs:

1. Predict the output:

void main( )
{
float a,b,c,x,y,z;
a=9;
b=12;
c=3;
clrscr();
x = a - b / 3 + c * 2 - 1;
y = a - b / ( 3 + c ) * ( 2 - 1 );
z = a - ( b / ( 3 + c ) * 2 ) - 1;
printf("x=%f\n",x);
printf("y=%f\n",y);
printf("z=%f\n",z);
getch();
}
Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________

2. Predict the output:

void main( )
{
int a,b;
float c = 0;
clrscr();
printf("Enter value of a & b");
scanf("\t %d \t %d",&a,&b);
c+ = (a>0 && a<=10)?++a:a/b;
printf("value of a, b, c is %d \t %d \t %f",a,b,c);
getch();
}
Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
3. Predict the output:

void main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
clrscr();
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a / b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
getch();
}

Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________

4. Predict the output:

void main()
{
int x=10;
clrscr();
printf("Value of x is %d ",10+ x++);
printf("Value of x is %d ",10+ ++x);
getch();
}

Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
LAB-3

PROGRAMS TO LEARN FORMATTING OF INPUT AND OUPUT VALUES.

Objectives:

1. To learn some inbuild functions for reading, processing and writing


data.
2. To learn various syntax for output function to enhance the readability
of output.

Program 1: Write a program that reads an alphabet from keyboard and then
prints it in reverse case.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf(“Enter an alphabet:”);
ch=getchar();
if(islower(ch)>0)
{
putchar(toupper(ch));
}
else
{
putchar(tolower(ch));
}
getch( );
}
input: Output:

a ___________________________________________
T ___________________________________________
f ___________________________________________
Z ___________________________________________
r ___________________________________________
Program 2: Predict the output for various input formatting options for reading
integer data.

(a) void main()


{
int x,y,z;
clrscr();
printf("Enter two four digit integer number:");
scanf("%2d %4d",&x,&y);
printf("\n%d\t%d",x,y);
getch();
}
input: Output:

2345 ___________________________________________
7812 ___________________________________________

(b) void main()


{
int x,y,z;
clrscr();
printf("Enter three integer number:");
scanf("%d %*d %d",&x,&y,&z);
printf("\n%d\t%d\t%d",x,y,z);
getch();
}

input: Output:

23 ___________________________________________
78 ___________________________________________
12 ___________________________________________
Program 3: Predict the output for various input formatting options for reading
of real numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b;
double p,q;
clrscr();
printf("Enter two real numbers:");
scanf("%f %e",&a,&b);
printf("\na=%f\tb=%f",a,b);
getch();
}

input: Output:

3.14 ___________________________________________
56.7e-5 ___________________________________________

Program 4: Predict the output for various input formatting options for reading
of string.

(a) void main()


{
char name[20];
clrscr();
printf("Enter your first and last name:");
scanf("%[a-z]",name);
printf("\n%s",name);
getch();
}
input: Output:

your first and last ___________________________________________


name with and ___________________________________________
without space
(b) void main()
{
char name[20];
clrscr();
printf("Enter your first and last name:");
scanf("%[^\n]",name);
printf("\n%s",name);
getch();
}
input: Output:

your first and last ___________________________________________


name with space ___________________________________________

Program 5: Predict the output for various output formatting options for
printing an integer data.

#include<stdio.h>
#include<conio.h>
void main()
{
int n=12345;
clrscr();
printf("First number: %d", n);
printf("\nSecond number: %10d", n);
printf("\nThird number: %010d", -n);
printf("\nFourth number: %-10d", n);
getch();
}

Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________

Program 6: Predict the output for various output formatting options for
printing a floating-point data.
#include<stdio.h>
#include<conio.h>
void main()
{
float y=92.5874;
clrscr();
printf("First number: %f", y);
printf("\nSecond number: %10.4f", y);
printf("\nThird number: %-7.2f", y);
printf("\nFourth number: %7.4e", y);
getch();
}

Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________

Program 7: Predict the output for various output formatting options for
characters and strings.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch=’A’;
char name[20]= “ANIL KUMAR GUPTA”;
clrscr();
printf("%c\n%3c\n%5c",ch,ch,ch);
printf("\n%s", name);
printf("\n%0.5s", name);
printf("\n%20.10s", name);
getch();
}

Output:

___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
Assignment Programs:

1. Write a Program to check whether the entered character is an alphabet or


digit or special character.

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
2. Write a Program to check validity of a variable by checking its first
character.

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
LAB-4

PROGRAMS TO LEARN DECISION MAKING AND BRANCHING

Objectives:

1. To learn various if statement.


2. To learn switch…case statement
3. To learn goto statement

Program 1: Write program to print entered year is leap year or not. Four
digits of year will be entered by user.

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter year");
scanf("%d",&year);
if(year%4==0)
{
printf("\nleap year");
}
else
{
printf("\n not leap year");
}
getch();
}
input: Output:

2020 ___________________________________________
2021 ___________________________________________

Program 2: Write program that prints name of weekday by entering number


of weekday (1 to 7). Use switch case statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf("enter the day:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("today is Monday");
break;
case 2:
printf("today is Tuesday");
break;
case 3:
printf("today is Wednesday");
break;
case 4:
printf("today is Thursday");
break;
case 5:
printf("today is Friday");
break;
case 6:
printf("today is Saturday");
break;
case 7:
printf("today is Sunday");
break;
default:
printf("invalid value");
break;
}
getch();
}
input: Output:

3 ___________________________________________
4 ___________________________________________
9 ___________________________________________

1) Program 3: Write a program to read marks of three subjects from


keyboard and display equivalent grade according to following table:

Percentage Grade
100-80 Distinction
60-79 First Class
35-59 Second Class
0-34 Fail

#include<stdio.h>
#include<conio.h>
void main()
{
int roll_no,m1,m2,m3;
float total, per;
clrscr();
printf("enter rno");
scanf("%d",&no);
printf("\nenter marks of Maths, Science and English out of 100");
scanf("%d %d %d",&m1,&m2,&m3);
total= m1+m2+m3;
per=(total/300)*100 ;
if(per>=80)
{
printf("\nDistinction");
}
else if(per>=60&&per<80)
{
printf("\nFirst Class");
}
else if(per>=35&&per<60)
{
printf("\nPass Class");
}
else
{
printf("fail");
}
getch();
}
input: Output:

91 78 71 ___________________________________________
65 48 60 ___________________________________________
30 21 10 ___________________________________________

Program 4: Write a program that evaluates the square root for five positive
integers number by using goto statement.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
Int x,count=1;
float y;
clrscr();
printf("enter any positive integer value:");
read:
scanf("%d",&x);
y=sqrt(x)
printf(“\nRoot of %d is %f”,x,y);
count++;
if (count<=5)
goto read;
printf("End");
getch();
}

input: Output:

9 36 64 81 144 ___________________________________________
___________________________________________
___________________________________________

Assignment Programs:
1) An electricity distribution company has decided the following power tariff
based on the structure below:
Units Consumed Variable Cost Fixed Cost
0-100 5 Rs/Unit 100
101-200 10 Rs/Unit 200
201-300 15 Rs/Unit 300
Above 300 20 Rs/Unit 500
Write a program to calculate the total cost (TC=FC+VC) where number of units
are entered by user.

___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

input: Output:

223 ___________________________________________
348 ___________________________________________
95 ___________________________________________
2) Write a program to create arithmetic calculator using switch case
statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, result;
char op;
clrscr();
printf("Enter an expression: "); //For example 10+12
scanf("%d %c %d", &a, &op, &b);
switch(op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
printf("Result = %d", result);
getch();
}

input: Output:

25+75 ___________________________________________
1550/10 ___________________________________________
2022%4 ___________________________________________
LAB-5

PROGRAMS TO LEARN DECISION MAKING AND LOOPING

Objectives:

4. To learn while statement


5. To learn do statement
6. To learn for statement and their feature, capabilities and application.

Program 1: Write program to print numbers from 1 to 10.

#include <stdio.h>

void main()
{
int i;
clrscr();

for (i = 1; i < 11; ++i)


{
printf("%d ", i);
}
getch();
}

input: Output:

___________________________________________
___________________________________________
___________________________________________

Program 2: Write program to print first n Fibonacci number using for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,k;
int i=0,j=1,h;
clrscr();
printf("enter the nth number up to which series to be printed :");
scanf("%d",&n);
printf("fibonacci series:");
printf("\n%d\n%d",i,j);
for(k=1;k<n;k++)
{
h=i+j;
i=j;
j=h;
printf("\n%d",h);
}
getch();
}

input: Output:

5 ___________________________________________
___________________________________________

Program 3: Write a program to reverse the digits of any number entered by


user with while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
int digit,n,x;
clrscr();
printf("Enter no. of digits:");
scanf("%d",&digit);
printf("\nEnter the value of %d digits:",digit);
scanf("%d",&n);
while(digit>0)
{
x=n%10;
printf("%d",x);
n=n/10;
digit--;
}
getch();
}

input: Output:

5 ___________________________________________
12345 ___________________________________________
___________________________________________

Program 4: Write program to print multiplication table using do while loop


#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,ans,i,j;
clrscr();
printf("Enter no. of rows and columns:");
scanf("%d %d",&r,&c);
i=1;
do
{
j=1;
do
{
ans=i*j;
printf("%d\t",ans);
j++;
}
while(j<=c);
printf("\n");
i=i++;
}
while(i<=r);
getch();
}

input: Output:

5 ___________________________________________
5 ___________________________________________
___________________________________________

Assignment Programs:

1) Write program to check enter number is prime or not.

#include <stdio.h>
#include<conio.h>

void main()
{
int n, i, flag = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}

if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
getch();

input: Output:

01234567 ___________________________________________
___________________________________________
___________________________________________

2) Write a Program to add numbers until the user enters zero

#include <stdio.h>
void main()
{
int number, sum = 0;
clrscr();
do
{
printf("Enter a number: ");
scanf("%d", &number);
sum += number;
}
while(number != 0);

printf("Sum = %d",sum);
getch();
}

input: Output:
___________________________________________
7890 ___________________________________________
LAB-6

PROGRAMS TO LEARN SINGLE AND TWO-DIMENSIONAL ARRAY


Objectives:

1. To learn initialization of one- and two-dimensional array variable.


2. To learn reading and printing of one- and two-dimensional array
variable.
3. To learn how to use array variable in C program.

Program 1: Write a program to initialize float array and print it.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float x[]={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
clrscr();
for(i=0;i<10;i++)
{
printf("\nx[%d]=%f",i+1,x[i]);
}
getch();
}

Output:

___________________________________________
___________________________________________
___________________________________________

Program 2: Write a program to read a float array from user screen and
print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float x[10];
clrscr();
for(i=0;i<10;i++)
{
printf("enter value:");
scanf("%f",&x[i]);
}
for(i=0;i<10;i++)
{
printf("\nx[%d]=%f",i,x[i]);
}
getch();
}
Input: Output:
1.2
___________________________________________
2.5
___________________________________________
1.6
___________________________________________
3.5
2.7

Program 3: Write a program to arrange the given integer array in


descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,temp,a[10],n;
clrscr();
printf("Enter no. of elements:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

Input: Output:
2
___________________________________________
8
___________________________________________
4
___________________________________________
3
1

Program 4: Write a program that reads a 3X3 matrix and print it. Also
print its transpose.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("Enter a 3X3 matrix row wise\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nOriginal Matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nTranspose Matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
getch();
}
Input: Output:
123456789
___________________________________________
___________________________________________

Assignment Programs:

1) Write a program that reads a two 3X3 matrix and print resultant matrix
after element to element to summation.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter a first 3X3 matrix row wise\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter a second 3X3 matrix row wise\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nResultant Matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Input: Output:
Enter a first 3X3
___________________________________________
matrix row wise
___________________________________________
123456789
___________________________________________
Enter a second
3X3 matrix row
wise
123456789

2) Write a program that reads a table having 3 rows and 4 columns and
perform row wise summation and column wise summation. Also calculate
the grand total.

#include<stdio.h>
#include<conio.h>
void main()
{
int r[3],c[4],i,j,grand_total=0;
int a[3][4]={{25,35,45,55},{15,25,35,45},{5,15,25,35}};
clrscr();
printf("\nRow wise total\n");
for(i=0;i<3;i++)
{
r[i]=0;
for(j=0;j<4;j++)
{
r[i]=r[i]+a[i][j];
}
printf("Total of r[%d]=%d\n",i+1,r[i]);
}
printf("\nColumn wise total\n");
for(j=0;j<4;j++)
{
c[j]=0;
for(i=0;i<3;i++)
{
c[j]=c[j]+a[i][j];
}
printf("Total of c[%d]=%d\n",j+1,c[j]);
}
printf("\nGrand total\n");
for(j=0;j<4;j++)
{
grand_total=grand_total+c[j];
}
printf("%d\n",grand_total);
getch();
}

Output:

___________________________________________
___________________________________________
___________________________________________
LAB-7

PROGRAMS TO LEARN CHARACTER ARRAY (STRING)


Objectives:

1. To learn various means for reading of string.


2. To learn use of string handling functions.
3. To learn how to perform string operations.

Program 1: Write a program to read your ‘first name’ and ‘Last name’ using
scanf() and gets().

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[15],s2[15];
clrscr();
printf("enter your first and last name\n");
scanf("%s %s",s1,s2);
printf("\n%s\t%s",s1,s2);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[30];
clrscr();
printf("enter your first and last name\n");
gets(s1);
printf("\n%s",s1);
getch();
}
Input: Output:
enter your first
___________________________________________
and last name
___________________________________________
Happy Singh
___________________________________________
Program 2: Write a program that reads to different string and compares them
character to character. If both strings are same copy second string to first
otherwise connect both the strings and display first string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[15],s2[15];
clrscr();
printf("enter two different strings\n");
gets(s1);
gets(s2);
if(strcmp(s1,s2)!=0)
{
strcat(s1,s2);
}
else
{
strcpy(s1,s2);
}
printf("\n%s",s1);
getch();
}

Input: Output:
enter two
___________________________________________
different strings
___________________________________________
Happy
___________________________________________
Singh

Program 3: Write a program to find out a particular character in a given string


and replace it with appropriate character. Also show the number of
replacement done.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[30],find,replace;
int i,count=0;
clrscr();
printf("Enter a string:");
gets(s1);
printf("\nEnter a character to find:");
scanf("%c",&find);
for(i=0;i<strlen(s1);i++)
{
if(s1[i]==find)
count++;
}
printf("\n%d occurence of %c is found",count,find);
printf("\nEnter a character to replace:");
scanf(" %c",&replace);
count=0;
for(i=0;i<strlen(s1);i++)
{
if(s1[i]==find)
{
s1[i]=replace;
count++;
}
}
printf("\nNew string is:%s",s1);
printf("\n%d replacement with %c is done",count,replace);
getch();
}

Input: Output:
Enter a string :
___________________________________________
AbcAbcAbc
___________________________________________
Enter a character to find: A
___________________________________________
Enter a character to replace: X

Assignment Programs:

1) Write a program that read and concatenate two strings also find length of
string after concatenate.

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

char str1[12] = "Hello";


char str2[12] = "World";
char str3[12];
int len ;
clrscr();

strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );

strcat( str1, str2);


printf("strcat( str1, str2): %s\n", str1 );

len = strlen(str1);
printf("strlen(str1) : %d\n", len );

Output:

___________________________________________
___________________________________________
___________________________________________

2) Write a program to reverse the string.

#include <stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char s1[50];
clrscr();
printf("Enter your string: ");
gets(s1);
printf("\nYour reverse string is: %s",strrev(s1));
getch();
}

Input: Output:
Enter your string:
___________________________________________
ABCDEFGH
___________________________________________
___________________________________________
LAB-8

PROGRAMS TO LEARN USER DEFINED FUNCTION

Objectives:

1. To understand basics and use of user defined function.


2. To learn various types of user defined function.

Program 1: WAP to print following output using No Argument and No Return


type function.
---------------------------------------
USER DEFINED FUNCTION
---------------------------------------
#include<stdio.h>
#include<conio.h>
void dashline(void);
void main()
{
char s1[30];
clrscr();
gets(s1);
dashline();
printf("\n%s\n",s1);
dashline();
getch();
}
void dashline(void)
{
int i;
for(i=0;i<30;i++)
{
printf("-");
}
}

Output:

___________________________________________
___________________________________________
___________________________________________
Program 2: WAP to calculate compound interest using a function interest() of
argument but no return value type.
#include<stdio.h>
#include<conio.h>
void interest(float principle,float interest, int year);
void main()
{
int n;
float i, p;
clrscr();
printf("\nEnter principle amount, year and interest rate\n");
scanf("%f %d %f",&p,&n,&i);
interest(p,i,n);
getch();
}
void interest(float principle,float interest, int year)
{
int i;
float sum=principle;
for(i=0;i<year;i++)
{
sum=sum*(1+interest/100);
}
printf("\nMatured amount=%f",sum);
}
Input Output:
5000 3 5.1
___________________________________________
___________________________________________
___________________________________________

Program 3: WAP to calculate x raised to yusing power() made by you.


#include<stdio.h>
#include<conio.h>
float power(float x, float y);
void main()
{
float x,y,ans;
clrscr();
printf("Enter x and y:");
scanf("%f %f",&x,&y);
ans=power(x,y);
printf("\nans=%f",ans);
getch();
}
float power(float x, float y)
{
int i;
float p=1;
if(y>0)
{
for(i=y;i>0;i--)
{
p=p*x;
}
}
else if(y<0)
{
for(i=y;i<0;i++)
{
p=p/x;
}
}
else
{
p=1;
}
return(p);
}
Input Output:
10 5
___________________________________________
50
___________________________________________
2 -3
___________________________________________
Program 4: WAP to perform arithmetic operations using user defined function
which returns multiple values.
#include<stdio.h>
#include<conio.h>
void mathop(int a, int b, int *sum ,int *diff, float *mul);
void main()
{
int x,y,s,d;
float mul;
clrscr();
printf("\nEnter value of x and y");
scanf("%d %d",&x,&y);
mathop(x,y,&s,&d,&mul);
printf("\nsum=%d\ndiff=%d\nmul=%f",s,d,mul);
getch();
}
void mathop(inta,intb,int *sum,int *diff, float *mul)
{
*sum=a+b;
*diff=a-b;
*mul=a*b;
}

Input Output:
10 5
___________________________________________
50
___________________________________________
2 -3
___________________________________________
LAB-9

PROGRAMS TO LEARN BASICS OF POINTER


Objectives:

1. To learn how to declare pointer variable and initialization.


2. To learn access of variable values using pointer.
3. To learn access of array element using pointer.

Program 1: Write a Program that shows the content and address of the
program variables.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
int *ptr;
clrscr();
x=10;
ptr=&x;
y=*ptr;
printf("value of x=%d",x);
printf("\n%d is stored at %u",x,&x);
printf("\n%d is stored at %u",*ptr,ptr);
printf("\n%d is stored at %u",y,&y);
*ptr=25;
printf("\nnow x=%d",x);
getch();
}

Program 2: Write a Program to understand pointer expressions.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p1,*p2,x,y,z;
clrscr();
a=10;
b=20;
p1=&a;
p2=&b;
x=(*p1)*(*p2)-6;
y=4*(-*(p2))/(*p1)-10;
printf("a=%d\tb=%d\tx=%d\ty=%d",a,b,x,y);
*p1=*p1+20;
*p2=*p2-10;
z=(*p1)*(*p2)-6;
printf("\na=%d\tb=%d\tz=%d",a,b,z);

getch();
}

Program 3: Write a program using pointers to read an array of integers and


print its elements in reverse order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],*p,i,n;
clrscr();
p=&a[0];
printf("Enter no of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",p+i);
}
for(i=1;i<=n;i++)
{
printf("a[%d]=%d\n",i,*(p+i));
}
for(i=n;i>=1;i--)
{
printf("a[%d]=%d\n",i,*(p+i));
}
getch();
}

Assignment Programs:

1. Predict the output:

void main( )
{
int k=10, *p, **m;
clrscr();
p=&k;
m=&p;
printf(“%d %d”,p,m);
getch();
}
Output:

___________________________________ ___________________________________
LAB-10

PROGRAMS TO LEARN USE OF STRUCTURE


Objectives:

4. To understand basics of structure.


5. To learn how to declare structure variable and how to access structure.
6. To learn structure initialization.
7. To understand array of structure.

Program 1: Write a program to define struct student to read and print roll no
and name of two students.

#include<stdio.h>
#include<conio.h>

struct student
{
int roll_no;
char name[20];
};

void main()
{
struct student s1,s2;
clrscr();
printf("Enter roll no and name for 1st student:");
scanf("%d %s",&s1.roll_no,s1.name);
printf("\nEnter roll no and name for 2nd student:");
scanf("%d %s",&s2.roll_no,s2.name);
printf("\nstudent1\tname=%s\troll no=%d",s1.name,s1.roll_no);
printf("\nstudent2\tname=%s\troll no=%d",s2.name,s2.roll_no);
getch();
}

Input: Output:
Enter roll no and name for 1st student:
________________________
10
________________________
Jay
________________________
Enter roll no and name for 2nd student:
________________________
20
________________________
Ram
________________________
Program 2: Write a program that use structure item that can read all
information of item i.e. item code, name and quantity. Also program should
capable to search item information by entering item code and print it.

#include<stdio.h>
#include<conio.h>

struct item
{
int code;
char name[20];
int qty;
};

void main()
{
struct item it[10];
int i,n,icode,flag=0;
clrscr();
printf("How many items?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter item code, name and quantity:\n");
scanf("%d %s %d",&it[i].code,it[i].name,&it[i].qty);
}
printf("Enter item code for search:\n");
scanf("%d",&icode);
for(i=0;i<n;i++)
{
if(it[i].code==icode)
{
flag=1;
printf("Item name=%s\tquantity=%d\n",it[i].name,it[i].qty);
break;
}
}

if(flag==0)
{
printf("item not found");
}
getch();
}
Input: Output:

___________________________________________
___________________________________________
___________________________________________

Assignment Programs:

1) Write a program that print the ID, gender, height and weight of
employees.

__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________

Output:

___________________________________________
___________________________________________
___________________________________________
LAB-11

PROGRAMS TO LEARN USE OF FILE HANDLING IN C LANGUAGE

Objectives:

1. To understand input output operation on file.

2. To learn how to access a particular part of file randomly using functions

available in input output library.

Program 1: Writing String Character by Character into a file.

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
FILE *fp;
int i;
char str[20];
clrscr();
printf("enter string:");
scanf("%s",str);
fp=fopen("char.text","w");
for(i=0;i<strlen(str);i++)
{
putc(str[i],fp);
}
fclose(fp);
getch();
}

Input: Output:
DDIT
___________________________________________
___________________________________________
___________________________________________
Program 2: Reading String Character by Character from a file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("char.text","r");
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}

Output:

___________________________________________
___________________________________________
___________________________________________

Program 3: Write Program to read integer no. from keyboard and stored them
in a file name "INPUT". Then bifurcate values in two separate files with name
"EVEN" and "ODD" and stored them in respective file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int i,n,num;
clrscr();
printf("How many number=");
scanf("%d",&n);
printf("\nEnter %d numbers\n",n);
f1=fopen("INPUT","w");
for(i=0;i<n;i++)
{
scanf("%d",&num);
putw(num,f1);
}
fclose(f1);
f1=fopen("INPUT","r");
f2=fopen("EVEN","w");
f3=fopen("ODD","w");
while((num=getw(f1))!=EOF)
{
if(num%2==0)
putw(num,f2);
else
putw(num,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
printf("content of the file INPUT\n");
f1=fopen("INPUT","r");
while((num=getw(f1))!=EOF)
printf("%d\n",num);
fclose(f1);
printf("content of the file EVEN\n");
f2=fopen("EVEN","r");
while((num=getw(f2))!=EOF)
printf("%d\n",num);
fclose(f2);
printf("content of the file ODD\n");
f3=fopen("ODD","r");
while((num=getw(f3))!=EOF)
printf("%d\n",num);
fclose(f3);
getch();
}

Input: Output:
How many number = 5
___________________________________________
Enter 5 numbers
___________________________________________
12345
___________________________________________

Program 4: Write program to understand use of FTELL, FSEEK and REWIND


Functions.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
long int n;
int i;
char ch;
char a[20];
clrscr();
printf("Enter string:");
scanf("%s",a);
fp=fopen("CHAR.TEXT","w");
for(i=0;i<strlen(a);i++)
{
putc(a[i],fp);
}
fclose(fp);
fp=fopen("CHAR.TEXT","r");
n=ftell(fp);
printf("\n%d",n);
ch=getc(fp);
printf("\n%c\t%d",ch,ftell(fp));
ch=getc(fp);
printf("\n%c\t%d",ch,ftell(fp));
fseek(fp,7,0);
ch=getc(fp);
printf("\n%c\t%d",ch,ftell(fp));
fseek(fp,-5,2);
ch=getc(fp);
printf("\n%c\t%d",ch,ftell(fp));
rewind(fp);
ch=getc(fp);
printf("\n%c\t%d",ch,ftell(fp));
fclose(fp);
getch();
}

Input: Output:
Enter string:
___________________________________________
abcdefgh
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________

You might also like