Professional Documents
Culture Documents
CP-I (ES113) - Lab Manual - Mechanical Engg. Dept.
CP-I (ES113) - Lab Manual - Mechanical Engg. Dept.
CP-I (ES113) - Lab Manual - Mechanical Engg. Dept.
FACULTY OF TECHNOLOGY
DHARMSINH DESAI UNIVERSITY
LAB MANUAL
OF
COMPUTER PROGRAMMING (ES113)
SEMESTER-I
Name: _________________________________________________________
________________________________________________________
DATE: DATE:
INDEX
3 PROGRAMS TO LEARN
FORMATTING OF INPUT AND
OUPUT VALUES.
7 PROGRAMS TO LEARN
CHARACTER ARRAY (STRING).
Objectives:
#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:
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
Assignment Programs:
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
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
Objectives:
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 ___________________________________________
___________________________________________
___________________________________________
___________________________________________
#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:
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:
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
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:
___________________________________ ___________________________________
___________________________________ ___________________________________
___________________________________ ___________________________________
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
Objectives:
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.
2345 ___________________________________________
7812 ___________________________________________
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.
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:
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
2. Write a Program to check validity of a variable by checking its first
character.
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________
LAB-4
Objectives:
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 ___________________________________________
3 ___________________________________________
4 ___________________________________________
9 ___________________________________________
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
Objectives:
#include <stdio.h>
void main()
{
int i;
clrscr();
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 ___________________________________________
___________________________________________
#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 ___________________________________________
___________________________________________
input: Output:
5 ___________________________________________
5 ___________________________________________
___________________________________________
Assignment Programs:
#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 ___________________________________________
___________________________________________
___________________________________________
#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
#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
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
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
#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 ()
{
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
Output:
___________________________________________
___________________________________________
___________________________________________
#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
Objectives:
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
___________________________________________
___________________________________________
___________________________________________
Input Output:
10 5
___________________________________________
50
___________________________________________
2 -3
___________________________________________
LAB-9
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();
}
#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();
}
#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:
void main( )
{
int k=10, *p, **m;
clrscr();
p=&k;
m=&p;
printf(“%d %d”,p,m);
getch();
}
Output:
___________________________________ ___________________________________
LAB-10
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
Objectives:
#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
___________________________________________
#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
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________