CPU Lab Manual 2014-15: Practical Set-01

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

CPU Lab Manual 2014-15

PRACTICAL SET-01
PRACTICAL -01

AIM: Write a program to print “HELLO FRIENDS”.

#include<stdio.h>
Void main()
{
Printf(“Hello Freinds”);
getch();
}

OUTPUT:
Hello Freinds

PRACTICAL-02

AIM: Write a program that reads two nos. from key board and gives their addition,
subtraction, multiplication, division and modulo.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,add,sub,mul,div,mod;
clrscr();
printf("ENTER VALUES OF A AND B\n");
scanf("%d\t%d",&a,&b);
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
mod=a%b;
printf("ADDITION=%d\nSUBSTRACTION=%d\nMULTIPLICATION=%d\n",add
,sub,mul);
printf("DIVISION=%d\n MODULUS=%d\n",div,mod);
getch();
}

OUTPUT:
ENTER VALUES OF A AND B
10
3
ADDITION=13
SUBSTRACTION=7
MULTIPLICATION=30
DIVISION=3
MODULUS=1

1
CPU Lab Manual 2014-15

PRACTICAL-03

AIM: Write a program to convert days into months and days.

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

void main( )
{
int days ,yr,mn,wk,d;
printf("Enter the no of days");
scanf("%d",&days);
yr = days /365;
mn =(days %365)/30;
wk = (((days%365)%30)/7);
d =(((days%365)%30)%7);
printf("Years=%d\nMonths=%d\n Weeks =%d\ndays=%d",yr,mn,wk,d);
getch();
}

OUTPUT:
Enter the no of days
450
Years=1
Months=2
Weeks=3
days=4

PRACTICAL-04

AIM: Write a program to solve Quadratic Equation.

#include<stdio.h>
#include<math.h>

void main(){
float a,b,c;
float d,root1,root2;
clrscr();

printf("Enter a, b and c of quadratic equation: ");


scanf("%f%f%f",&a,&b,&c);

d = b * b - 4 * a * c;
if(d < 0)
{
printf("Roots are complex number.\n");
printf("Roots of quadratic equation are: ");
printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf("%.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
}
else if(d==0){
printf("Both roots are equal.\n");
root1 = -b /(2* a);
printf("Root of quadratic equation is: %.3f ",root1);

2
CPU Lab Manual 2014-15

}
else{
printf("Roots are real numbers.\n");

root1 = ( -b + sqrt(d)) / (2* a);


root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are:
%.3f,%.3f",root1,root2);
}
getch();
}

OUTPUT:

PRACTICAL-05

AIM: Write a program to select & print the largest of the three nos. using Nested-If-Else statement

#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c,d;
clrscr();
printf("\nEnter any number :");
scanf("%d%d%d",&a,&b,&c);
if (a>b)
{ if(a>c)
printf("\nMaximim Number = %d ",a);
else
printf("\nMaximum Number = %d ",c);
}
else
{
if(b>c)
printf("\nMaximim Number = %d ",b);
else
printf("\nMaximum Number = %d ",c);
}
getch();
}
OUTPUT:

3
CPU Lab Manual 2014-15

PRACTICAL SET-02
PRACTICAL-01

AIM: Write a program to display multiplication table.

#include<stdio.h>
void main()
{
int n, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
getch();
}

OUTPUT:

PRACTICAL-02

AIM: Write a program to print 1+1/2+1/3+1/4+………+1/N series.

#include<stdio.h>
#include<conio.h>
void main()
{
long int n,i;
clrscr();
printf("\n Please Give The Value of N: ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
if(i==1)
printf("\n 1 +");
else
printf(" (1/%d) + ",i);
}
getch();

4
CPU Lab Manual 2014-15
}

OUTPUT:

PRACTICAL-03

AIM: Write a program to find sum of all integers greater than 100 & less than 200 and are
Divisible by 5.

#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum=0;
clrscr();
printf("All nos. between 100 - 200 which is divisible by 5\n");
for(i=101;i<200;i++)
{
if(i%5==0)
{
printf("%5d",i);
sum+=i;
}
}
printf("\n sum = %d",sum);
getch();
}

OUTPUT:

PRACTICAL-04

AIM: The distance between two cities (In KM) is input through key board. Write a program
to convert and print this distance in meters, feet, inches & centimetres.

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

void main()
{

5
CPU Lab Manual 2014-15
long int km,meter,cm;
long float inch,feet;
clrscr();
printf("Enter the distance between two cities in KM : ");
scanf("%ld",&km);
//Conversion of km in meter
meter = km * 1000;
printf("\nDistance between two cities is : %ld meter",meter);
//converion of meter in centi meter
cm = meter * 100;
printf("\nDistance between two cities is : %ld c.m.",cm);
//Conversion of c.m. in inch
inch = cm / 2.54f;
printf("\nDistance between two cities is : %lf inch",inch);
//Conversion of inch in feet
feet = inch / 12;
printf("\nDistance between two cities is : %lf feet",feet);
getch();
}

OUTPUT:

PRACTICAL-05

AIM: Write a program to find sum of first N odd numbers.


Ex.1+3+5+7+………..+N.

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

void main()
{
int i, N, oddSum = 0, evenSum = 0;
clrscr();
printf("Enter the value of N\n");
scanf ("%d", &N);
for (i=1; i <=N; i++)
{
if (i % 2 == 0)
evenSum = evenSum + i;
else
oddSum = oddSum + i;
}
printf ("Sum of all odd numbers = %d\n", oddSum);
printf ("Sum of all even numbers = %d\n", evenSum);
getch();

6
CPU Lab Manual 2014-15
}
OUTPUT:

7
CPU Lab Manual 2014-15

PRACTICAL-SET-03
PRACTICAL-01

AIM: Write a program for use of putchar( ) and getchar( ) function.

#include <stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a;
printf("Enter the character\n");
a=getchar();
putchar(a);
getch();
}

OUTPUT:

PRACTICAL-02

AIM: Program to print Patterns.


*
**
***
****

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

void main()
{
int i, j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

8
CPU Lab Manual 2014-15

OUTPUT:

PRACTICAL-03

AIM: Program to print Patterns.


12345
2345
345
45
5

#include <stdio.h>
#include <conio.h>
void main()
{
int i, j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
OUTPUT:

9
CPU Lab Manual 2014-15

PRACTICAL-04

AIM: Program to print Patterns.


AAAAA
BBBB
CCC
DD
E

# include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf(“%c”,‟A‟-1 + i);
}
printf(“\n”);

getch();
}

OUTPUT:
AAAAA
BBBB
CCC
DD
E

PRACTICAL-05

AIM: Program to print Patterns.


1
01
101
0101

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
for(i=1;i<=4;i++)
{
for(j=i;j>=1;j--)
{
printf("%d",j%2);
}
printf("\n");
}

10
CPU Lab Manual 2014-15

getch();
}

OUTPUT:

11
CPU Lab Manual 2014-15

PRACTICAL-SET-04

PRACTICAL-01

AIM: Write a program to print Fibonacci series. 1,1,2,3,5,……N.


#include<stdio.h>
#include<conio.h>
void main()
{
int k,r;
long int i=0l,j=1,f;
clrscr();
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);

printf("FIBONACCI SERIES: ");


printf("%ld %ld",i,j); //printing firts two values.

for(k=2;k<r;k++)
{
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
getch();
}

OUTPUT:

PRACTICAL-02

AIM: Write a program to reverse the digit.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,reverse=0;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);

while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;

12
CPU Lab Manual 2014-15

printf("Reversed of number: %d",reverse);

getch();
}

OUTPUT:

PRACTICAL-03

AIM: Add, subtract and multiply two nos. using switch statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,add,sub,mul;

clrscr();
printf("enter the two value A & B");
scanf("%d%d",&a,&b);
printf("ENTER THE CHOICE\n");
printf("1.ADD\n2.SUBTRACT\n3.MULTIPLY");
scanf("\n%d",&c);
switch(c)
{
case 1:
add=a+b;
printf("ADDITION=%d",add);
break;
case 2:
sub=a-b;
printf("SUBTRACT=%d",sub);
break;
case 3:
printf("MULTIPLY=%d",mul);
mul=a*b;
break;
default:
printf("wrong choice");
break;
}

getch();
}

13
CPU Lab Manual 2014-15

OUTPUT:

PRACTICAL-04

AIM: Write a program to add two matrixes.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nEnter the Second matrix->");

for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");

for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");

14
CPU Lab Manual 2014-15
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
getch();
}

OUTPUT:

PRACTICAL-05

AIM: Write a program to given no in ascending order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,t;
clrscr();
printf("Enter 5 nos.\n\n");
for (i=0;i<5;i++)
scanf("%d",&a[i]);
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}

15
CPU Lab Manual 2014-15
}
}
printf("Ascending Order is:");
for(j=0;j<5;j++)
printf("\n%d",a[j]);
getch();
}

OUTPUT:

PRACTICAL-06

AIM: W.A.P to read array of integers and print it in reverse order

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();

ptr=&arr[0];

printf("Enter the size of array : ");


scanf("%d",&size);

printf("nEnter %d integers into array:n",size);


for(i=0;i<size;i++)
{
scanf("%d",ptr);
ptr++;
}

ptr=&arr[size-1];

printf("nElements of array in reverse order are:n");

16
CPU Lab Manual 2014-15

for(i=size-1;i>=0;i--)
{
printf("nElement%d is %d :",i,*ptr);
ptr--;
}

getch();
}
OUTPUT:

17
CPU Lab Manual 2014-15

PRACTICAL-SET-5
PRACTICAL-01

AIM: Write a program to count total words in text.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char s[50],ch;
int i,c=0;
clrscr();
printf("Enter any string : ");
for(i=0;ch!='\n';i++){
ch=getchar();
s[i]=ch;
}
s[i]='\0';

for(i=0;s[i]!='\0';i++){
if(s[i]==' '){
c++;
while(s[i]==' ')
i++;
}
}
printf("\n\nTotal words are %d",c+1);
getch();
}
OUTPUT:

18
CPU Lab Manual 2014-15

PRACTICAL-02

AIM: Find length of string using strlen( ) function


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

void main()
{
int b;
char a[50];
clrscr();
printf("Enter text");
scanf("%s",&a);
b=strlen(a);
printf("string length is %d",b);

getch();
}

OUTPUT:

19
CPU Lab Manual 2014-15

PRACTICAL-03

AIM: Write a program to copy one string to another string.


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

void main()
{

char a[50],b[50];
clrscr();
printf("Enter string\n");
scanf("%s",a);
strcpy(b,a);
printf("the a string is %s\n",a);
printf("the b string is %s\n",b);

getch();
}
OUTPUT:

20
CPU Lab Manual 2014-15

PRACTICAL-04

AIM: Write a program to join two strings.


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

void main()
{

char a[50],b[50];
clrscr();
printf("Enter a string\n");
scanf("%s",a);
printf("Enter b string\n");
scanf("%s",b);

strcat(a,b);
printf("the merged string is %s\n",a);
getch();
}

OUTPUT:

21
CPU Lab Manual 2014-15

PRACTICAL-05

AIM: Write a program convert character into TOggLe character.

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

void main()
{
char str[20];
int i;
clrscr();
printf("Enter any string=");
scanf("%s",str);
printf("The string is=%s",str);
for(i=0;i<strlen(str);i++)
{
if(str[i]>=65 && str[i]<=90)
str[i]=str[i]+32;
else
str[i]=str[i]-32;
}
printf("\nThe string in togle char is=%s",str);
getch();
}

OUTPUT:

22
CPU Lab Manual 2014-15

PRACTICAL-06

AIM: Find given string is palindrome or not using string library function.

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

void main()
{

char str[20],str2[20];
clrscr();
printf("Enter any string : ");
scanf("%s",str);
strcpy(str2,str);
strrev(str2);
if(strcmp(str,str2)==0)
printf("string is palindrome");
else
printf("string is not palindrome");

getch();
}

OUTPUT:

23
CPU Lab Manual 2014-15

PRACTICAL-SET-6

PRACTICAL-01

AIM: Write a function program to add first N numbers.

#include <stdio.h>
int sum(int n);
int main()
{
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n)
{
if(n==0) return n;
else return n+sum(n-1); /*self call to function sum() */
}

Output:
Enter a positive integer:
5
15

24
CPU Lab Manual 2014-15

PRACTICAL-02

AIM: Write a function find out maximum out of three numbers.


#include<iostream.h>
#include<conio.h>

int largest(int,int,int);

int main()
{
int value_1,value_2,value_3,maximum;
clrscr();
printf("\n Enter the value_1 = ");
scanf(“%d”,&value_1);
printf("\n Enter the value_2 = ");
scanf(“%d”,&value_2);
printf("\n Enter the value_3 = ");
scanf(“%d”,&value_3);
maximum=largest(value_1,value_2,value_3);
printf("\n The largest integer is =%d", maximum);
getch();
return 0;
}

int largest(int x,int y,int z)


{
int largest=x;
if(y>largest)
largest=y;
if(z>largest)
largest=z;
return largest;
}

Output:

25
CPU Lab Manual 2014-15

PRACTICAL-03

AIM: Write a function power that computes x raised to the power y for integer x and y and
returns double type value.

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

void main()
{
int x,y;
double power();
clrscr();
printf("Enter value of x : ");
scanf("%d",&x);
printf("Enter value of y : ");
scanf("%d",&y);

printf("%d to power %d is = %f\n",x,y,power(x,y));


getch();
}
double power(x,y)
int x,y;
{
double p;
p=1.0;
if(y>=0)
while(y--)
p*=x;
elsewhile(y++)
p/=x;
return(p);
}

Output:

26
CPU Lab Manual 2014-15

PRACTICAL-04

AIM: Write a program to find factorial of a number using recursion.

#include<stdio.h>
int factorial(int n);
int main() { int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1) return n*factorial(n-1);
}

Output

Enter an positive integer: 6


Factorial of 6 = 720

27
CPU Lab Manual 2014-15

PRACTICAL-05

AIM: Write a program that used user defined function Swap ( ) and interchange the value of
two variable.

#include<stdio.h>
void main()
{
int a=40;
int b=70;
void swap(int *x,int *y);
clrscr();
printf("a=%d,b=%d",a,b);
swap(&a,&b);
printf("\nAfter swaping result is:\n a=%d b=%d ",a,b);
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}

output

28
CPU Lab Manual 2014-15

PRACTICAL-06

AIM: Write a function prime that return 1 if it„s argument is prime and return 0 otherwise.
#include<stdio.h>
#include<conio.h>
int prime(int);
int main()
{
int n,p;
clrscr();
printf(“Enter a number : “);
scanf(“%d”,&n);
p=prime(n);
if(p==1)
printf(“%d is prime\n”,n);
else
printf(“%d is not prime\n”,n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}

Output:
Enter a number : 18
18 is not prime
Enter a number : 5
5 is prime

29
CPU Lab Manual 2014-15

PRACTICAL-07

AIM: Write a calculator program (add, subtract, multiply, divide). Prepare user defined function
for each functionality.

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

void add(int x,int y) /* formal arguments*/


{
int z;
z=x+y;
printf("\nX =%d",x);
printf("\nY =%d",y);
printf("\nAddition =%d",z);
}
void sub(int x,int y) /* formal arguments*/
{
int z;
z=x-y;
printf("\nX =%d",x);
printf("\nY =%d",y);
printf("\nsubtraction =%d",z);
}

void mul(int x,int y) /* formal arguments*/


{
int z;
z=x*y;
printf("\nX =%d",x);
printf("\nY =%d",y);
printf("\nMultiplication =%d",z);
}
void main()
{
int ch,a,b;
clrscr();
for(;;)
{
printf("\n 1 Add ");
printf("\n 2 sub");
printf("\n 3 multiply");
printf("\n 4 Exit ");
scanf("%d",&ch);if(ch==4)break;
printf("\nEnter the two values");
scanf("%d%d",&a,&b);
switch(ch)
{
case 1:add(a,b);break; //actual args
case 2:sub(a,b);break; //actual args
case 3:mul(a,b);break;
default: break;
}
}
getch();

30
CPU Lab Manual 2014-15
}
Output

31
CPU Lab Manual 2014-15

PRACTICAL-SET-7
PRACTICAL-01

AIM: Define a structure type, personal, that would contain person name, date of joining and
salary. Using this structure, write a program to read this information for one person from the
key board and print the same on the screen.

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

struct Personal{

char p_name[20];
char doj[10];
int salary;
};

void main()
{
struct Personal per;
clrscr();
printf("Enter Name ");
gets(per.p_name);
printf("\nDate Of Joining:");
gets(per.doj);
printf("Salary ");
scanf("%d",&per.salary);

printf("Name = ");
puts(per.p_name);
puts("Data Of Join ");
puts(per.doj);
puts("Salary =");
printf("%d",per.salary);

getch();
}
Output:
Enter Name hiren
Date Of Joining: 10/09/2013
Salary 20000
Name=hiren
Date Of Join 10/09/2013
Salary=20000

32
CPU Lab Manual 2014-15

PRACTICAL-02

AIM: Define a structure called cricket that will describe the following information:
a. Player name
b. Team name
c. Batting average

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

struct cricket
{
char nm[20],team[20];
int avg;
};

#define total 5

int main()
{
struct cricket player[total],temp;
int i,j;
clrscr();
for(i=0;i<total;i++)
{
printf("For player %d\n",i+1);
printf("Enter the name of player : ");
fflush(stdin);
gets(player[i].nm);
printf("Enter the team : ");
fflush(stdin);
gets(player[i].team);
printf("Enter the batting average : ");
fflush(stdin);
scanf("%d",&player[i].avg);
}
printf("\nTeam Name Average\n");
printf(" \n");
for(i=0;i<total;i++)
{
printf("%-10s %-10s %7d\n",player[i].team,player[i].nm,player[i].avg);
}
getch();
return 0;
}

33
CPU Lab Manual 2014-15

Output:
For player 1
Enter the name of player : Diz
Enter the team : India
Enter the batting average : 100
For player 2
Enter the name of player : Tiwari
Enter the team : India
Enter the batting average : 5
For player 3
Enter the name of player : Tendulkar
Enter the team : India
Enter the batting average : 45
For player 4
Enter the name of player : Dhoni
Enter the team : India
Enter the batting average : 48
For player 5
Enter the name of player : Yuvi
Enter the team : India
Enter the batting average : 39

Team Name Average


-----------------------------
India Diz 100
India Tiwari 5
India Tendulkar 45
India Dhoni 48
India Yuvi 39

34
CPU Lab Manual 2014-15

PRACTICAL-03

AIM: Write a function to enter roll-no, marks of the three subject for 3 students and find total
obtained by each student

#include<stdio.h>
#include<conio.h>
struct student
{
int rollno,tot;
char name[25];
int mark[5];
};
void main()
{
struct student s[5]; //Data type of '*s' is struct student
int i,n,j;
clrscr();
printf("Enter the number of students:");
scanf("%d",&n);
printf("\t*Students Records*\n");
//take input from user
for(i=0;i<n;i++)
{
printf("\nEnter Student Roll Number: ");
scanf("%d",&s[i].rollno);
printf("\nEnter Student name: ");
scanf("%s",s[i].name);
printf("\nEnter Student 3 subject's marks: ");
for(j=0;j<3;j++)
scanf("%d",&s[i].mark[j]);
}
//calculation
for(i=0;i<n;i++)
{
s[i].tot=0;
for(j=0;j<3;j++)
s[i].tot = s[i].tot+ s[i].mark[j];
}
//Display result
for(i=0;i<n;i++)
{
printf("\t*Students Records*\n");
printf("\n==================================\n");
printf("\nStudent's Roll no. - %d", s[i].rollno);
printf("\nStudent's Name - %s", s[i].name);
printf("\nStudent's Total Marks - %d", s[i].tot);
}
getch();
}

35
CPU Lab Manual 2014-15

OUTPUT
Enter the number of students:2
*Students Records*
Enter Student Roll Number: 01
Enter Student name: rathi
Enter Student 3 subject's marks:
12
67
89
Enter Student Roll Number: 02
Enter Student name: raghu
Enter Student 3 subject's marks:
56
89
90
*Students Records*
==================================
Student's Roll no. - 1
Student's Name - rathi
Student's Total Marks - 168 *Students Records*
==================================
Student's Roll no. - 2
Student's Name - raghu
Student's Total Marks – 235

36
CPU Lab Manual 2014-15

PRACTICAL-SET-8
PRACTICAL-01

AIM: Write a program using pointer and function to determine the length of string.

Program:
#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main()
{
char str[20];
int l;
clrscr();
printf("Enter any string:n");
gets(str);
l=string_ln(str);
printf("The length of the given string %s is : %d",str,l);
getch();
}
int string_ln(char*p) /* p=&str[0] */
{
int count=0;
while(*p!='')
{
count++;
p++;
}
return count;
}

Output:
Enter the String : pritesh
Length of the given string pritesh is : 7

37
CPU Lab Manual 2014-15

PRACTICAL-02

AIM: Write a program using pointer to compare two strings.

Program:
#include<stdio.h>>
#include<conio.h>
void main()
{
char *p,*q;
int i,j,flag=0;
printf("To compare two strings without strCmp \n");
printf("\nInput two strings\n");
gets(p);
gets(q);
while(*p!='\0' || *q!='\0')
{
if(*p!=*q)
flag=1;
*p++;
*q++;
}
if (flag==0)
printf("\nTwo Strings are same");
else
printf("\nTwo Strings are different");
}

Output:

38
CPU Lab Manual 2014-15

PRACTICAL-03

AIM: Write a program using pointer to concate two strings.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0;
char *str1,*str2,*str3;
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(*str1){
str3[i++]=*str1++;
}
while(*str2){
str3[i++]=*str2++;
}
str3[i]='\0';
printf("After concatenation the strings are\n");
puts(str3);
getch();
}

Output:

39
CPU Lab Manual 2014-15

PRACTICAL-04

AIM: Write a program using pointer to copy one string to another string.

Program:

#include<stdio.h>
#include<conio.h>
void stcpy(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf(“\n\n\t ENTER A STRING…: “);
gets(str1);
stcpy(str1,str2);
printf(“\n\t THE COPIED STRING IS…: “);
puts(str2);
getch();
}
void stcpy(char *str1, char *str2)
{
int i, len = 0;
while(*(str1+len)!=‟\0′)
len++;
for(i=0;i<len;i++)
*(str2+i) = *(str1+i);
*(str2+i) = „\0′;
}
Output:

40
CPU Lab Manual 2014-15

PRACTICAL-05

AIM: Write a program using pointer to read an array if integer and print element in reverse
order.

Program:
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();
ptr=&arr[0];
printf("Enter the size of array : ");
scanf("%d",&size);
printf("nEnter %d integers into array:n",size);
for(i=0;i<size;i++)
{
scanf("%d",ptr);
ptr++;
}
ptr=&arr[size-1];
printf("nElements of array in reverse order are:n");
for(i=size-1;i>=0;i--)
{
printf("nElement%d is %d :",i,*ptr);
ptr--;
}
getch();
}

Output :

41
CPU Lab Manual 2014-15

PRACTICAL-SET-9
PRACTICAL-01

AIM: Write a program that uses a table of integers whose size will be specified interactively
at run time.

#include <stdio.h>
#include <stdlib.h>
#define NULL 0

main()
{
int *p, *table;
int size;
printf(“\nWhat is the size of table?”);
scanf(“%d”,size);
printf(“\n”)
/*------------Memory allocation --------------*/
if((table = (int*)malloc(size *sizeof(int))) == NULL)
{
printf(“No space available \n”);
exit(1);
}
printf(“\n Address of the first byte is %u\n”, table);
/* Reading table values*/

printf(“\nInput table values\n”);

for (p=table; p<table + size; p++)


scanf(“%d”,p);
/* Printing table values in reverse order*/
for (p = table + size –1; p >= table; p --)
printf(“%d is stored at address %u \n”,*p,p);

Output:

What is the size of the table? 5


Address of the first byte is 2262
Input table values
11 12 13 14 15
15 is stored at address 2270
14 is stored at address 2268
13 is stored at address 2266
12 is stored at address 2264
11 is stored at address 2262

42
CPU Lab Manual 2014-15

PRACTICAL-02

AIM: Write a program to store a character string in block of memory space created by malloc and
then modify the same to store a large string.

#include <stdio.h>
#include<stdlib.h>
#define NULL 0
Void main()
{
char *buffer;
/* Allocating memory */
if((buffer = (char *)malloc(10)) == NULL)
{
printf(“malloc failed.\n”);
go to l;
}
printf(“Buffer of size %d created \n”,_msize(buffer));
strcpy(buffer, “HYDERABAD”);
printf(“\nBuffer contains: %s \n “, buffer);
/* Realloction */
if((buffer = (char *)realloc(buffer, 15)) == NULL)
{
printf(“Reallocation failed. \n”);
go to l;
}
printf(“\nBuffer size modified. \n”);
printf(“\nBuffer still contains: %s \n”,buffer);
strcpy(buffer, “SECUNDERBAD”);
printf(“\nBuffer now contains: %s \n”,buffer);
/* Freeing memory */

free(buffer);
l:
getch();
}

Output:

Buffer of size 10 created


Buffer contains: HYDERABAD
Buffer size modified
Buffer still contains: HYDERABAD
Buffer now contains: SECUNDERABAD

43
CPU Lab Manual 2014-15

PRACTICAL-SET-10

PRACTICAL-01

AIM: A program to illustrate reading files contents

#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *fptr;
char filename[15];
char ch;

printf("Enter the filename to be opened \n");


scanf("%s", filename);

/* open the file for reading */

fptr = fopen(filename, "r");


if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
}

44
CPU Lab Manual 2014-15

PRACTICAL-02

AIM: A program to illustrate the use of fgets( ).


#include <stdio.h>

void main()
{
FILE *fp;
char str[60];

/* opening file for reading */


fp = fopen("file.txt" , "r");
if(fp == NULL) {
perror("Error opening file");
return(-1);
}
if( fgets (str, 60, fp)!=NULL ) {
/* writing content to stdout */
puts(str);
}
fclose(fp);

getch();
}

45
CPU Lab Manual 2014-15

PRACTICAL-03

AIM: A program to illustrate the use of fputc ( ) and fputs( ).


#include <stdio.h>
int main(void)
{
FILE *stream;
int file_state;
int input1=0;
stream = fopen("data3.txt", "w");
if (stream == NULL)
puts("file open error");
puts("input data");
while (input1 != EOF)
{
input1=fgetc(stdin);
putchar(input1);
}
file_state=fclose(stream);
if(file_state==EOF)
puts("file close error");
return 0;
}

46

You might also like