CP Record
CP Record
CP Record
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
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:
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;
return 0;
3
Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.
c. Algorithm:
Step1: Start
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:
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:
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:
Algorithm:
Step1: Start
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:
Algorithm:
Step1: Start
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:
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:
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:
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:
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:
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
do
4.4 n=n/10
done
Step 7: STOP
24
Flow chart:
PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
int n,rem=0,sum=0,reverse=0;
clear();
scanf(%d,&n);
while(n!=0)
25
{
rem=n%10;
sum=sum+rem;
reverse=(rev*10)+rem;
n=n/10;
printf(reverse is %d,reverse);
return(0);
}
OUTPUT:
ALGORITHM:
Step1: Start
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
Step3: Read n
Step4:
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 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 6: i is initialized as 0 and in steps of 1 do the condition i<n is checked until it fails
6.1.2: max i
8.1.2: minpi
31
PROGRAM:
#include<stdio.h>
int main()
int n,i,minp,maxp,max=0,min=0,temp;
clear();
scanf(%d,&n);
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;
a[maxp]=a[minp];
a[minp]=temp;
for(i=0;i<n;i++)
printf(%d,a[i]);
return(0);
OUTPUT :
ALGORITHM:
Step 1: Start
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:
Step 8: Stop
PROGRAM:
#include<stdio.h>
int main()
int a[50],n,f=0,size;
scanf(%d,&size);
for(i=0;i<size;i++)
scanf(%d,&a[i]);
34
scanf(%d,&n);
for(i=0;i<size;i++)
if(a[i]==n)
f++;
break;
if(f==1)
else
return(0);
OUTPUT :
35
c) Write a c-program to implement sorting of an array of elements.
ALGORITHM:
STEP-1: Start.
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.
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-6: Stop.
36
FLOWCHART:
START
DECLARE a[50],i,j,temp,size
READ size
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();
scanf(%d,&size);
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 4: Stop
Step 1: Start
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;
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 3: ans=SI()
Step 4: Stop
Step 1: Start
Step 5: Stop
Flow Chart:
42
Program:
#include <stdio.h>
int si();
int main()
int ans;
ans=si();
printf("%d",ans);
return 0;
int si()
int p,t,r;
return ((p*t*r)/100));
Output:
Algorithm:
Step 1: Start
Step 5: Stop
Step 1: Start
Step 3: Stop
Flow Chart:
44
Program:
#include <stdio.h>
int main()
printf(" *** Passing arguments and returning No Values *** \n\n"); int
p,t,r;
scanf("%d %d %d",&p,&t,&r);
si(p,t,r);
return 0;
45
}
printf("%d",(p*t*r)/100);
Output:
Step 1: Start
Step 4: ans=SI(P,T,R)
Step 6: Stop
46
Step 1: Start
Step 3: Stop
Flow chart:
Program:
#include <stdio.h>
int main()
printf(" *** Passing arguments and returning Values *** \n\n"); int
ans,p,t,r;
scanf("%d %d %d",&p,&t,&r);
47
ans=si(p,t,r);
printf("%d",ans);
return 0;
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
48
step 4 : read source string.
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);
return 0;
49
Output:
ii) concatenate
Algorithm:
step 1: start
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);
gets(b);
strcat(a,b);
%s\n",a); return 0;
}
Output:
Algorithm:
step 1 : start
51
step 4 : read string a value
step 7 : stop
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
gets(a);
length = strlen(a);
return 0;
Output:
52
iv)compare()
Algorithm:
step 1 : start
step 8 : else
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the firststring\n");
gets(a);
53
gets(b);
if (strcmp(a,b) == 0)
else
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:
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();
gets(str1);
gets(str2);
while(str1[i]!='\0'||str2[i]!='\0')
dif=(str1[i]-str2[i]);
if(dif!=0)
break;
i++;
if(dif>0)
else
56
{
if(dif<0)
else
OUTPUT:
57
Experiment 8:
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();
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
60
int mat2[ROW][COL];
int product[ROW][COL];
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;
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>
{
float real;
float imag;
} complex;
int main()
{
complex n1, n2, temp;
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 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 10: open the target_file in write mode using fopen function and assign to target
pointer.
Step 12: close the source file and exit from the program.
13.1: write the character ch in the target file using fputc ()function.
67
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, source_file[20],
target_file[20];
gets(source_file);
exit(1);
gets(target_file);
target = fopen(target_file,"w");
exit(1);
while((ch=getc(source))!=EOF
)
putc(ch,target);
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>
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