Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CP Record

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 72

CERTIFICATE

This is to certify that this a bonafide record of C-PROGRAMMING LAB

Performed by :Mr.VAKKALANKA ANIRUDH

of B.tech. Ist year Ist semester(ECE) Regd. No:193J1A04G9

During the year 2019-2020.

No.of Exercise/Practical’s/Programs performed:

INTERNAL EXAMINER HEAD OF THE DEPARTMENT

EXTERNAL EXAMINER
S.NO INDEX PAGE.NO
1. Algorithms and Flow charts design and evaluation (Minimum 2)
2. Write C Programs to demonstrate C-tokens and operators
3. Write C Programs to demonstrate Decision Making And Branching (Selection)
4. Write a C program to demonstrate different loops
5. Write a C program to demonstrate arrays
6. Write a C program to demonstrate functions
7. Write a C program to implement the following
a. To manipulate strings using string handling functions.
b. To manipulate strings without using string handling functions
8. Write a C program to demonstrate different library functions
9. Write a C program to implement the following
a. To exchange two values using call by value and reference
b. To multiply two matrices using pointers
10. Write a C program to demonstrate functions using pointers
11. Write a C program to implement the following operations using structure and functions:
i) Reading a complex number
ii) Writing a complex number
12. Write a C program
a. To copy data from one file to another.
b. To reverse the first n characters in a given file (Note: The file name and n are specified on
the command line)

SIGNATURE OF
INTERNAL EXAMINER
Experiment 1:
Algorithms and Flow charts design and evaluation (Minimum2)
Algorithm: Write an algorithm to add two numbers entered by user.

 Step 1: Start
 Step 2: Declare variables num1, num2 and sum.
 Step 3: Read values num1 and num2.
 Step 4: Add num1 and num2 and assign the result to sum.
 sum←num1+num2
 Step 5: Display sum
 Step 6: Stop

Write an algorithm to find the largest among three different numbers entered by user.

 Step 1: Start
 Step 2: Declare variables a,b and c.
 Step 3: Read variables a,b and c.
 Step 4: If a>b
 If a>c
 Display a is the largest number.
 Else
 Display c is the largest number.
 Else
 If b>c
 Display b is the largest number.
 Else
 Display c is the greatest number.
 Step 5: Stop

Write an algorithm to find the factorial of a number entered by user.

 Step 1: Start
 Step 2: Declare variables n, factorial and i.
 Step 3: Initialize variables
 factorial←1
 i←1
 Step 4: Read value of n
 Step 5: Repeat the steps until i=n
 5.1: factorial←factorial*i
 5.2: i←i+1
 Step 6: Display factorial
 Step 7: Stop.

1
Draw a flowchart for add two numbers:

Draw a flowchart for find the given number is even or odd:

2
Experiment 2: Write C Programs to demonstrate C-tokens and operators

Tokens: The smallest individual elements or units in a program are called as Tokens. C has
following tokens.

 Identifiers
 Keywords
 Constants
 Operators
 Special characters

Example:

#include<stdio.h>

int main()

int a,b,c;

printf(“Enter a, b values”);

Scanf(“%d%d”,&a,&b);

c=a+b;

Printf(“C value is %d”,c);

return 0;

 Where int , return, main these all are keywords.


 a, b, c are variables and some values are assigned to those variables(Identifiers) like
10, 200 ect.. Those are called constants.
 = and + are operators.
 &, % is called special characters.

3
Operators:

An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.

a) Write a program to implement all arithmetic operators in

c. Algorithm:

Step1: Start

Step2: declare all variables

Step 3: Accept a, b values

Step 4: find c, d, e, f, g values

Step 5: Display all values

Step 6: Stop

Flowchart:

4
Program:

#include<stdio.h>
int main()
{
int a,b,c,d,e,f,g;
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
Printf(“%d %d %d %d %d”,c,d,e,f,g);
return 0;
}

Output:

b) Write a program to implement all relational operators in c.

Algorithm:
Step1: Start
Step2: declare all variables
Step 3: Accept a, b values

5
Step 4: find c, d, e, f values
Step 5: Display all values
Step 6: Stop
Flow Chart:

Program:

#include<stdio.h>
int main()
{
int a,b,c,d,e,f;
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=a>b;
d=a<=b;
e=a==b;

6
f=a!=b;
printf("%d %d %d %d ",c,d,e,f);
return 0;
}
Output:

c) Write a program to implement all bitwise operators in c.

Algorithm:

Step1: Start
Step2: declare all variables
Step 3: Accept a, b values
Step 4: find c, d, e, f, g values
Step 5: Display all values
Step 6: Stop.
Flow Chart:

7
Program:

#include<stdio.h>

int main()
{
int a,b,c,d,e,f,g;
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=a&b;
d=a|b;
e=b<<2;
f=a>>2;
g=~a;
printf("%d %d %d %d %d ",c,d,e,f,g);
return 0;
}
Output:

d) Write a program to implement all logical operators in c.

Algorithm:

Step1: Start

Step2: declare all variables


Step 3: Accept a, b values
Step 4: find c, d, e values

8
Step 5: Display all values
Step 6: Stop
Flow Chart:

Program:

#include<stdio.h>
int main()
{
int a,b,c,d,e;
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=a&&b;
d=a||b;
e=!a;
printf("%d %d %d ",c,d,e);
return 0;
}

9
Output:

e) Write a program to implement all Inc/Dec operators in c.

Algorithm:

Step1: Start

Step2: declare all variables


Step 3: Accept a, b values
Step 4: find c, d, e, f values
Step 5: Display all values
Step 6: Stop
Flow Chart:

10
Program:

#include<stdio.h>
int main()
{
int a,b,c,d,e,f;
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=++a;
d=a++;
e=b--;
f=--b;
printf("%d %d %d %d ",c,d,e,f);
return 0;
}
Output:

f) Write a program to implement assignment operators in c.

Algorithm:
Step1: Start
Step2: declare all variables
Step 3: Accept a, b values
Step 4: find c, d values
Step 5: Display all values
Step 6: Stop

11
Flowchart:

Program:
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("enter a,b values");
scanf("%d%d",&a,&b);
a+=a;
b*=b;
printf("%d %d ",a,b);
return 0;
}
Output:

12
g) Write a program to implement Conditional operator in c. (Ternary or conditional
statement)

Algorithm:
Step1: Start
Step2: declare all variables
Step 3: Accept a, b values
Step 4: find c values
Step 5: Display all values
Step 6: Stop

Flowchart:

Program:

#include<stdio.h>
int main()
{
int a,b,c;

13
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=(a<b)?a:b;
printf("%d ",c);
return 0;
}
Output:

h) Write a program to implement size of operator in c.

Algorithm:
Step1: Start
Step2: declare all variables
Step 3: Accept a, b values
Step 4: find c values
Step 5: Display c value
Step 6: Stop
Flowchart:

14
Program:

#include<stdio.h>
int main()
{
int a,c,d;
char b;
printf("enter a,b values");
scanf("%d%c",&a,&b);
c=sizeof(a);
d=sizeof(b);
printf("%d %d ",c,d);
return 0;
}
Output:

15
Experiment 3: Write C Programs to demonstrate Decision Making And Branching
(Selection)

There are different types of decision making and branching statements in c. Those are
listed below

 If Statement
 If else statement
 Nested if statement
 Else if ladder statement
 Switch statement
a) Write a program to implement simple if statement

Algorithm:

Step1: Start
Step2: declare all variables
Step 3: Accept a values
Step 4: check a value is less than or equal to 0 if it is true print negative number otherwise go
to step 5.
Step 5: Display the given number is an integer.
Step 6: Stop
Flowchart:

16
Program:

#include<stdio.h>
int main()
{
int a;
printf("Enter a value\n");
scanf("%d",&a);
if(a<=0)
{
printf("%d is negetive number\n",a);
}
printf("%d is an integer \n");
getch();
return 0;
}
output:

b) Write a program to check the given number is even or odd.

Algorithm:
Step1: Start
Step2: declare all variables
Step 3: Accept a values
Step 4: check a%2==0 if it is true print the given number is even otherwise print the given
number is odd and go to step 6.
Step 6: Stop

17
Flowchart:

Program:

#include<stdio.h>

int main()
{
int a;
printf("Enter a value\n");
scanf("%d",&a);
if(a%2==0)
{
printf("%d is even number\n",a);
}
else
{
printf("%d is odd number \n");
}
getch();
return 0;
}

18
Output:

c) Write a program to print prime numbers in the given range using nested loops
Algorithm:
Step 1: Start
Step 2: Declare all variables
Step 3: Accept n value
Step 4: Declare a loop from i=1 to i<=n, for each loop increment the i value by 1
Step 5: Declare a nested loop j=1 to j<=n for each loop increment the j value by 1
Step 6: Check the condition i%j==0 if it is true increment fact value by 1 otherwise go to step
5.this process is continuous up to the step 5 false.
Step 7: check fact value is == 2 if it is true print the i value and this process is continuous up to
the step 4 is false.
Step 6: Stop
Flowchart:

19
Program:
#include<stdio.h>
int main()
{
int n,i,fact,j;
printf("Enter the Number");
scanf("%d",&n);
printf("Prime Numbers are: \n");
for(i=1; i<=n; i++)
{
fact=0;
for(j=1; j<=n; j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("%d " ,i);
}
return 0;
}
output:

d) Write a program to find given number is positive or negative.


Algorithm:
Step 1: Start
Step 2: Declare all variables
Step 3: Accept a value
Step 4: check the condition a>0 if it is true print given No is positive otherwise goto step 5.
Step 5: check the condition a==0 if it is true print given No is zero otherwise goto step 6.
Step 6: check the condition a<0 if it is true print given No is negative otherwise goto step 7.
Step 7: stop

20
Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a;
printf("Enter a Number: ");
scanf("%d",&a);
if(a > 0)
{
printf("Given Number is Positive");
}
else if(a == 0)
{
printf("Given Number is Zero");
}
else if(a < 0)
{
printf("Given Number is Negative");
}
getch();
}

21
Output:

e) Write a program to perform arithmetic operations using switch statements


Algorithm:
Step 1: Start.
Step 2: Declare all variables.
Step 3: Display a menu for all arithmetic operations.
Step 4: Accept choice from the user.
Step 5: Declare switch case.
Step 6: Based on the option, perform different arithmetic operations +,*,-,%,/
Step 7: Default case is declared
Step 8: Stop
Flowchart:

22
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n
4.Division\n"); printf("Enter the values of a & b: "); scanf("%d
%d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch (op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
}
Output:

23
Experiment 4: Write a C program to demonstrate different loops

a) While loop: Write a C program to find the sum of individual digits of a positive
integer and find the reverse of the given number.

Algorithm:
Step 1: START

Step 2: Declare Integer Variables n,rem=0,sum=0,reverse=0

Step 3: Read n Value

Step 4: While n!=0

do

4.1 rem = n%10

4.2 sum =sum+rem

4.3 reverse = (rev*10)+rem

4.4 n=n/10

done

Step 5: Display Sum

Step 6: Display reverse

Step 7: STOP

24
Flow chart:

PROGRAM:

#include<stdio.h>

#include<math.h>

int main()

int n,rem=0,sum=0,reverse=0;

clear();

printf(“enter value of n”);

scanf(“%d”,&n);

while(n!=0)

25
{

rem=n%10;

sum=sum+rem;

reverse=(rev*10)+rem;

n=n/10;

printf(“ sum is %d”,sum);

printf(“reverse is %d”,reverse);

return(0);

}
OUTPUT:

b) do-while: Write a c program to print table of 5 using do while loop ?

ALGORITHM:

Step1: Start

Step2: Declare i=1

Step3:
Step 3.1: display 5*I before checking the condition.
Step 3.2: loop will continue where i<=10 and i is incremented until loop fails.

Step4: Stop

26
FLOWCHART

PROGRAM:
#include<stdio.h>
int main()

int i=1;

clrscr();
do
{

printf(“5*%d=%d\n”,i ,5*i);
i++;

27
}while(i<=10);

return(0);

}
OUTPUT:

c) for loop: Write a c program to print the multiplication table of a given number n up to
a given value, where n is entered by the user.

ALGORITHM:

Step1: Start

Step2: Declare i, and n

Step3: Read n

Step4:

Step4.1: i is initialized as 1 and condition i<=n is checked and I


is incremented in steps of 1 , until loop fails

Step4.2: second iteration is checked where j is initialized as 1 & j<=10 and j is


incremented until loop fails

Step5: Display table, i*j

Step6: Stop

28
FLOWCHART:

Start

Declare i, and n

Read n

true
false
For I in steps of 1 where i<=n

False
true
For j in terms of 1 where j<=10

Display
table

Stop

29
PROGRAM:
#include<stdio.h>

int main()

int i,j,n;

clear();

printf(“enter n”);

scanf(“%d”,&n);

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

for(j=1;j<=10;j++)

printf(“%d*%d=%d\n”,i, j,i*j);

return(0);

OUT PUT:

30
Experiment 5:
a) C Program to interchange the largest and smallest element in an array

ALGORITHM:

Step 1: Start

Step 2: Declare integer variables n ,i ,minp ,maxp ,max=0,min=0,temp

Step3: Initialize max 0, min a[0]

Step 4:Read n

Step 5: i is initialized as 0 and in steps of 1 do the condition i<n is checked until it fails

Step 5.1: read a[i]

Step 6: i is initialized as 0 and in steps of 1 do the condition i<n is checked until it fails

Step 6.1: check a[i]>max then

Step 6.1.1: max a[i] Step

6.1.2: max i

Step 7: Display max ,maxp

Step 8 :i is initialized as 0 and in steps of 1 do the condition i<n is checked until it


fails Step 8.1: check a[i]<min

Step 8.1.1 : min a[i] Step

8.1.2: minpi

Step 9: Display min ,minp Step 10: temp max

Step 11: a[maxp] a[minp] Step 12: a[minp] temp

Step 13: for i=0 in steps of 1 do wherei<n Steps 13.1Display a[i]

Step 14: Stop

31
PROGRAM:

#include<stdio.h>

int main()

int n,i,minp,maxp,max=0,min=0,temp;

clear();

printf(“enter the value of n\n”);

scanf(“%d”,&n);

printf(“enter the elements”);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);

for(i=0;i<n;i++)

if(a[i]>max)

max=a[i];

maxp=i;

}
printf(“max=%d position=%d”,max,maxp); for(i=0;i<n;i++)

if(a[i]<a[i+1])

32
min=a[i];

minp=i;

printf(“min=%d position=%d”,min,minp); temp=max;

a[maxp]=a[minp];

a[minp]=temp;

for(i=0;i<n;i++)

printf(“”%d”,a[i]);

return(0);

OUTPUT :

b) Program to implement linear search

ALGORITHM:

Step 1: Start

Step 2: Declare integer variables a[50],n,f=0,size;

33
Step 3: Read size

Step 4: i is initialized as 0 in steps of 1 do the condition i<size is checked until it fails Step 4.1:

read a[i]

Step 5:Read n

Step 6: i is initialized as 0 in steps of 1 do the condition i<size is checked until it fails Step 6.1:

check a[i]==n then

Step 6.1.1: f++

Step 6.1.2: stop otherwise goto

step 6.1 Step 7: Check f==1 then

Step 7.1: Display element found at position I otherwise Step 7.2:

Display element not found

Step 8: Stop

PROGRAM:
#include<stdio.h>

int main()

int a[50],n,f=0,size;

printf(“enter the size”);

scanf(“%d”,&size);

printf(“enter the elements”)

for(i=0;i<size;i++)

scanf(“%d”,&a[i]);

printf(“enter the element to be searched”);

34
scanf(“%d”,&n);

for(i=0;i<size;i++)

if(a[i]==n)

f++;

break;

if(f==1)

printf(“element found in %d location”,i);

else

printf(“element not found”);

return(0);

OUTPUT :

35
c) Write a c-program to implement sorting of an array of elements.

ALGORITHM:

STEP-1: Start.

STEP-2: Declare integer variables i,j,temp,size,a[50].


STEP-3: Read integer variables size,a[i].

STEP-4: Check the conditions and the iterations.

4.1: First iteration is checked where I is initialized as 0 and (i<size) is checked and i is
incremented until the loop fails.

4.2: Inner loop is executed where j is initialized as i+1 and (j<size) is checked and j is
incremented until the loop fails.

4.3: If condition (a[i]>a[j]) is checked if true:

temp a[i]

a[i] a[j]

a[j] temp

4.3.1: if condition is false comes out of the if condition and also the loop.

STEP-5: Print the elements.

STEP-6: Stop.

36
FLOWCHART:

START

DECLARE a[50],i,j,temp,size

READ size

For i in steps of 1 do where i<size


FALSE
TRUE

READ a[i]

FALSE
For i in steps of 1 do where i<size

TRUE
For j in steps of 1 do where j<size

FALSE
TRUE
FALSE
(a[i]>a[j])

TRUE

temp=a[i]

a[i]=a[j]

a[j]=temp

STOP

37
PROGRAM:

#include<stdio.h>
int main()

int a[50],i,j,temp,size;

clear();

printf(“enter the size of an array”);

scanf(“%d”,&size);

printf(“enter the elements”);

for(i=0;i<size;i++)

scanf(“%d”,&a[i]);

for(i=0;i<size;i++)

for(j=i+1;j<size;j++)

if(a[i]>a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

38
printf(“sorted elements are:”);

for(i=0;i<size;i++)

printf(“%d”,size);

return(0);

ORIGINAL OUTPUT :

39
Experiment 6:
a) Passing no arguments and returning no

values Algorithm:

Step 1: Start

Step 2: Declare function SI

Step 3: Call the Function SI, SI()

Step 4: Stop

ALGORITHM FOR FUNCTION SI():

Step 1: Start

Step 2: Declare P,T,R

Step 3: Read the values of P,T,R

Step 4: Display (P*T*R)/100

Step 5: Stop

40
Program:

#include <stdio.h>

int si();

int main()

printf(" *** Passing No arguments and returning No values *** \n\n"); si();

return 0;

int si()

int p,t,r;

printf("Enter the values of P,T and R\n\n");

scanf("%d %d %d",&p,&t,&r);

printf("%d",(p*t*r)/100);

return 0;

41
b) Passing no arguments and returning

values Algorithm:

Step 1: Start

Step 2: Declare variable ans,function SI

Step 3: ans=SI()

Step 4: Display ans

Step 4: Stop

ALGORITHM FOR FUNCTION SI():

Step 1: Start

Step 2: Declare P,T,R

Step 3: Read the values of P,T,R

Step 4: return (P*T*R)/100

Step 5: Stop

Flow Chart:

42
Program:

#include <stdio.h>

int si();

int main()

int ans;

printf(" *** Passing No arguments and returning Values *** \n\n");

ans=si();

printf("%d",ans);

return 0;

int si()

int p,t,r;

printf("Enter the values of P,T and R\n\n");


43
scanf("%d %d %d",&p,&t,&r);

return ((p*t*r)/100));

Output:

c)Passing arguments and returning no values

Algorithm:

Step 1: Start

Step 2: Declare Variables P,T,R,function SI

Step 3: Read P,T,R

Step 4: Call the Function SI, SI(P,T,R)

Step 5: Stop

ALGORITHM FOR FUNCTION SI():

Step 1: Start

Step 2: Display (P*T*R)/100

Step 3: Stop

Flow Chart:

44
Program:

#include <stdio.h>

int si(int p,intt,int r);

int main()

printf(" *** Passing arguments and returning No Values *** \n\n"); int
p,t,r;

printf("Enter the values of P,T and R\n\n");

scanf("%d %d %d",&p,&t,&r);

si(p,t,r);

return 0;

45
}

int si(int p,intt,int r)

printf("%d",(p*t*r)/100);

Output:

d) Passing arguments and returning values


Algorithm:

Step 1: Start

Step 2: Declare Variables ans,P,T,R,function SI

Step 3: Read P,T,R

Step 4: ans=SI(P,T,R)

Step 5: Display ans

Step 6: Stop

ALGORITHM FOR FUNCTION SI():

46
Step 1: Start

Step 2: return (P*T*R)/100

Step 3: Stop

Flow chart:

Program:

#include <stdio.h>

int si(int p,intt,int r);

int main()

printf(" *** Passing arguments and returning Values *** \n\n"); int
ans,p,t,r;

printf("Enter the values of P,T and R\n\n");

scanf("%d %d %d",&p,&t,&r);

47
ans=si(p,t,r);

printf("%d",ans);

return 0;

int si(int p,intt,int r)

return (p*t*r)/100;

Output:

Experiment 7:
Write a C program to implement the following
a) To manipulate strings using string handling functions.
i) strcpy():
Algorithm:
step 1: start

step 2 : declare source and destination one dimensional strings.

step 3 : display Input a string.

48
step 4 : read source string.

step 5 : copy the content in destination array from source array.

step 6 : display source string.

step 7 : display destinationstring

step 8 : stop

Program:

#include <stdio.h>

#include <string.h>

int main()

{
char source[100], destination[100];

printf("Input a string\n");
gets(source);

strcpy(destination, source);

printf("Source string: \"%s\"\n", source);


printf("Destination string: \"%s\"\n",destination);

return 0;

49
Output:

ii) concatenate
Algorithm:
step 1: start

step 2 : declare two one dimensional strings a and b.


step 3 : display Enter the first string.

step 4 : read string a

step 5 : display Enter the Second string. step 6 : read string b

step 7 : concatenate strings a and b

step 8: display String concatenation value.

step 9 : stop

Program:

#include <stdio.h>

#include <string.h>

int main()

50
char a[1000], b[1000];
printf("Enter the first string\n");
gets(a);

printf("Enter the second string\n");

gets(b);

strcat(a,b);

printf("String obtained on concatenation is

%s\n",a); return 0;

}
Output:

iii) length ():

Algorithm:

step 1 : start

step 2 : declare string s and variable length.

step 3 : display Enter a string to calculate it'slength.

51
step 4 : read string a value

step 5 : compute length of the string.

step 6 : display Length of entered string.

step 7 : stop

Program:

#include <stdio.h>

#include <string.h>

int main()

{
char a[100];

int length;

printf("Enter a string to calculate it's length\n");

gets(a);

length = strlen(a);

printf("Length of entered string is = %d\n",length);

return 0;

Output:

52
iv)compare()

Algorithm:

step 1 : start

step 2 :declare two strings a and b.


step 3 : display Enter the first string.
step 4 : read string a value.

step 5 : display Enter the second string.


step 6 : read string b value

step 7 : compare the two strings a and b if they are equal


display Entered strings are equal.

step 8 : else

display Entered strings are not equal.


step 9 : stop

Program:

#include <stdio.h>

#include <string.h>

int main()

{
char a[100], b[100];
printf("Enter the firststring\n");
gets(a);

printf("Enter the second


string\n");

53
gets(b);

if (strcmp(a,b) == 0)

printf("Entered strings are equal.\n");

else

printf("Entered strings are not equal.\n");

return 0;

}
Output:

54
b) To manipulate strings without using string handling functions.
i)C program to concatenate two strings without using strcat()
Program:

#include<stdio.h>
void main(void)
{
char str1[25],str2[25]; int
i=0,j=0; printf("\nEnter First
String:"); gets(str1);

printf("\nEnter Second
String:"); gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}

OUTPUT:

Enter First String: MAHESH

Enter Second String: RIT

Concatenated String is MAHESHRIT

55
ii)C program to Compare two strings without using strcmp()

Program:

#include <string.h>

#include<conio.h>

void main(void)

char str1[25],str2[25];

int dif,i=0;

clrscr();

printf("\nEnter the first String:");

gets(str1);

printf("\nEnter the second String;");

gets(str2);

while(str1[i]!='\0'||str2[i]!='\0')

dif=(str1[i]-str2[i]);

if(dif!=0)

break;

i++;

if(dif>0)

printf("%s comes after %s",str1,str2);

else

56
{

if(dif<0)

printf("%s comes after %s",str2,str1);

else

printf("both the strings are same");

OUTPUT:

Enter the first String: MAHESH

Enter the second String: MAHESH

both the strings are same

57
Experiment 8:

Write a C program to demonstrate different library functions


C programming you can find the square root by just using sqrt() function
Program:
#include <stdio.h>
#include <math.h>
int main()
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);
/ Computes the square root of num and stores in root.
root = sqrt(num);
printf("Square root of %.2f = %.2f", num, root);
return 0;
}
OUT PUT:
Enter a number: 12
Square root of 12.00 = 3.46

58
Experiment 9:
Write a C program to implement the following
a. To exchange two values using call by value and reference
c program to swap using call by value
#include<stdio.h>
#include<conio.h>
int swap(int , int); // Declaration of function
main( )
{
int a = 10, b = 20 ; // call by value
swap(a,b); // a and b are actual parameters
printf ( "\na = %d b = %d", a, b ) ;
getch();

int swap( int x, int y ) // x and y are formal parameters


{
int t ;
t=x;
x=y;
y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}
OUTPUT:
X=20 y=10

Call by reference:
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
int n1,n2;
printf("Enter first number (n1) : ");
scanf("%d",&n1);
printf("Enter second number (n2) : ");

59
scanf("%d",&n2);
printf("\nBefore swapping values:");
printf("\n\tn1=%d \n\tn2=%d",n1,n2);
swaping(&n1,&n2);
printf("\nAfter swapping values:");
printf("\n\tn1=%d \n\tn2=%d",n1,n2);
getch();
return 0;
}
void swaping(int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
}
Output:
Enter first number (n1) : 100
Enter second number (n2) : 200
Before swapping values: n1=100 n2=200
After swapping values: n1=200 n2=100

b) To multiply two matrices using pointers


Program:
#include <stdio.h>
#define ROW 3
#define COL 3
/* Function declarations */
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL]);
int main()
{
int mat1[ROW][COL];

60
int mat2[ROW][COL];
int product[ROW][COL];

printf("Enter elements in first matrix of size %dx%d\n", ROW, COL);


matrixInput(mat1);
printf("Enter elements in second matrix of size %dx%d\n", ROW, COL);
matrixInput(mat2);
matrixMultiply(mat1, mat2, product);
printf("Product of both matrices is : \n");
matrixPrint(product);
return 0;
}
void matrixInput(int mat[][COL])
{
int row, col;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
scanf("%d", (*(mat + row) + col));
}
}
}
void matrixPrint(int mat[][COL])
{
int row, col;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
printf("%d ", *(*(mat + row) + col));
}
printf("\n");
}
}

61
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL])
{
int row, col, i;
int sum;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
sum = 0;
for (i = 0; i < COL; i++)
{
sum += (*(*(mat1 + row) + i)) * (*(*(mat2 + i) + col));
}
*(*(res + row) + col) = sum;
}
}
}
Output:
Enter elements in first matrix of size 3x3
10 20 30
40 50 60
70 80 90
Enter elements in second matrix of size 3x3
123
456
789
Product of both matrices is :
300 360 420
660 810 960
1020 0 1500

62
Experiment 10:
Write a C program to demonstrate functions using pointers
Program to copy one array to another using pointers
Program:
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
/* Function declaration to print array */
void printArray(int arr[], int size);
int main()
{
int source_arr[MAX_SIZE], dest_arr[MAX_SIZE];
int size, i;
int *source_ptr = source_arr; // Pointer to source_arr
int *dest_ptr = dest_arr; // Pointer to dest_arr int
*end_ptr;
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for (i = 0; i < size; i++)
{
scanf("%d", (source_ptr + i));
}
end_ptr = &source_arr[size - 1];
printf("\nSource array before copying: ");
printArray(source_arr, size);
printf("\nDestination array before copying: ");
printArray(dest_arr, size);
while(source_ptr <= end_ptr)
{
*dest_ptr = *source_ptr;

/ Increment source_ptr and


dest_ptr source_ptr++;
dest_ptr++;
}

63
printf("\n\nSource array after copying: ");
printArray(source_arr, size);
printf("\nDestination array after copying: ");
printArray(dest_arr, size);
return 0;
}
void printArray(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d, ", *(arr + i));
}
}
OUTPUT:
Enter size of array: 10
Enter elements in array: 10 -1 100 90 87 0 15 10 20 30

Source array before copying: 10, -1, 100, 90, 87, 0, 15, 10, 20, 30,
Destination array before copying: 0, 0, 127, 127, 0, 1, 0, 16777472, 0, 0,

Source array after copying: 10, -1, 100, 90, 87, 0, 15, 10, 20, 30,
Destination array after copying: 10, -1, 100, 90, 87, 0, 15, 10, 20, 0

64
Experiment 11:
Write a C program to implement the following operations using structure and functions:
i)Reading a complex number
ii)Writing a complex number
Program:

#include <stdio.h>

typedef struct complex

{
float real;

float imag;

} complex;

complex add(complex n1,complex n2);

int main()

{
complex n1, n2, temp;

printf("For 1st complex number \n");

printf("Enter real and imaginary part respectively:\n");

scanf("%f %f", &n1.real, &n1.imag);

printf("\nFor 2nd complex number \n");

printf("Enter real and imaginary part respectively:\n");

scanf("%f %f", &n2.real, &n2.imag);

temp = add(n1, n2);

printf("Sum = %.1f + %.1fi", temp.real,

temp.imag); return 0;

}
complex add(complex n1, complex n2)

{
complex temp;

65
temp.real = n1.real + n2.real;
temp.imag = n1.imag +
n2.imag; return(temp);

}
Output:

66
Experiment 12:

Write a C program
a) To copy data from one file to another

Algorithm:

Step 1: start.

Step 2: declare char ch, and two characters arrays source_file and target_file.

Step 3: declare source and target file pointers.

Step 4: display Enter name of file to copy. Step 5: read the file using gets ()function.

Step 6: open the source_file in read mode using fopen function and
assign to source pointer.

Step 7: if source is equal to NULL

Step 8: exit from the program.

Step 9: display Enter name of target file.

Step 10: open the target_file in write mode using fopen function and assign to target
pointer.

Step 11: if target is equal to NULL

Step 12: close the source file and exit from the program.

Step 13: check ( ch = fgetc(source) ) != EOF repeat

13.1: write the character ch in the target file using fputc ()function.

Step 14: display File copied successfully.

Step 15: close the source and targetfiles.

Step 16: stop.

67
Program:

#include <stdio.h>

#include <stdlib.h>

int main()

{
char ch, source_file[20],
target_file[20];

FILE *source, *target;

printf("Enter name of file to copy\n");

gets(source_file);

source = fopen(source_file, "r");

if( source == NULL ) {

exit(1);

printf("Enter name of target file\n");

gets(target_file);

target = fopen(target_file,"w");

if( target == NULL ) {


fclose(source);

exit(1);

while((ch=getc(source))!=EOF
)

putc(ch,target);

printf("\nfile copied successfully");

fclose(source);

68
fclose(target);

return 0;

OUTPUT:

b) To reverse the first n characters in a given file (Note: The file name and n are specified
on the command line)
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>

void main(int argc, char *argv[])


{
char a[15];
char s[20];
char n; int
k;
int j=0;
int i; int
len; FILE
*fp;

if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");

69
exit(0);
}

k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='';
getch();
}/*(Note: The file name and n are specified on the command line.)*/
OUTPUT:
Executed on command line….

70

You might also like