0% found this document useful (0 votes)
67 views40 pages

C Record

Uploaded by

Semi Stephen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views40 pages

C Record

Uploaded by

Semi Stephen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

SREE SARASWATHI THYAGARAJA COLLEGE

An Autonomous, NAAC Re-Accreted with ‘A+’ Grade, ISO 21001:2018 Certified


Institution, Affiliated to Bharathiar University, Coimbatore.

Approved by AICTE for MBA/MCA and UGC for 2(f) & 12(B) status) Palani Road,
Pollachi-642107

School of Computing Sciences


UG Department of Digital and Cyber Forensic Science

C PROGRAMMING LAB

Name :_______________________________________

Reg. No. :_______________________________________

Subject :_______________________________________
SREE SARASWATHI THYAGARAJA COLLEGE
(An Autonomous, NAAC Re-Accreted with ‘A’ Grade, ISO 21001:2018 Certified
Institution, Affiliated to Bharathiar University, Coimbatore.
Approved by AICTE for MBA/MCA and UGC for 2(f) & 12(B) status) Palani Road,
Pollachi-642107

UG Department of Digital and Cyber Forensic Science

Programming in C Lab

This is to certify that this is a bonafide record work done


by…………………………………………of……………………duri
ng the academic year…………….for the Autonomous Practical
Examination held on ………………………..at Sree Saraswathi
Thyagaraja College, Coimbatore.

Staff In-Charge Head of the Department

Internal Examiner External Examiner


CONTENTS

Ex. Page Staff


Programs Date
No. No Signatu
re
Develop a C Program to find the sum and
1. average of a mark.

Develop a C program using Switch case.


2.

Develop a C program to find the Fibonacci


3.
series for a given limit.
Develop a C program to check whether the
given number is prime or not and display the n
4. range of prime numbers.

Develop a C program to illustrate recursive


5. function

Develop a C program to find the palindrome in


6.
a given sentence
Develop a C program to manipulate strings
using string functions.
7.

8. Develop a C Program using Functions

Develop a C program to swap two integers


9.
using pointers

10. Develop a C program using Array of Pointers

11. Develop a C program using the structures

Develop a C program using Array of


Structures
12.
Develop a C program to calculate electricity
13
bill using files.

14 C program to encrypt and decrypt a string

15 Encrypt and decrypt files


Ex. No: 1
Date:

SUM AND AVERAGE

AIM :
ALGORITHM :-

1. Start the program


2. Declare integer variables as i, a, n, total and average.
3. Get the input values from the user.
4. Calculate the total marks and average of the student.
5. Compile and execute the program.
6. Display the result.
7. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a,total=0,average;
clrscr();
printf(“\n***********************”);
printf(“ \nSUM AND AVERAGE”);
printf(“\n***********************”);
printf("\n Enter the number of Subjects : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
Printf(“\n Enter the marks one by one:”);
scanf("%d",&a);
total=total+a;
}
printf("\n The total is = %d",total);
average=total/n;
printf("\n The average is = %d",average);
getch();
}

1
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

2
Ex. No: 2
Date:

SWITCH CASE

AIM :
ALGORITHM :-

1. Start the program.


2. Declare the variables.
3. Take input for operation and the respective operands.
4. First case is to perform addition, second case is subtraction, third case is multiplication,
fourth case is division.
5. Select the required case.
6. Compile and execute the program.
7. Display output according to case.
8. End the program.

PROGRAM :-

#include<stdio.h>
int main()
{
char operation;
double n1,n2;
printf(“\nEnter an operator (+,-,*,/): ”);
scanf(“%c”,&operation);
printf(“\nEnter two operands : ”);
scanf(“%lf %lf”,&n1,&n2);
switch(operation)
{
case '+':
printf("%.1lf+%.1lf=%.1lf",n1,n2,n1+n2);
break;
case '-':
printf("%.1lf-%.1lf=%.1lf",n1,n2,n1-n2);
break;
case '*':
printf("%.1lf*%.1lf=%.1lf",n1,n2,n1*n2);
break;
case '/':
printf("%.1lf/%.1lf=%.1lf",n1,n2,n1/n2);
break;
default:
printf("Error! Operator is invalid");
break;
}
getch();

3
return 0;
}

OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

4
Ex. No: 3
Date:

FIBONACCI SERIES
AIM :

ALGORITHM :-

1. Start the program.


2. Declare i,n,t1,t2,nexterm.
3. Add values to t1=0 and t2=1.
4. Take number of limit from user, n.
5. Create a loop to get value upto n.
6. Add the value of t1 and t2 and the value is stored in nexterm.
7. Using assignment operation t2 value is assigned to t1.
8. Next term is assigned to t2 and using for loop, repeat the process until condition
satisfy.
9. Compile and execute the program.
10. The fibonacci series is printed upto given number.
11. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,t1,t2,nexterm;
t1=0;
t2=1;
printf("\n******************");
printf("\nFIBONACCI SERIES");
printf("\n******************");
printf("\nEnter the number to calculate Fibonacci : ");
scanf("%d",&n);
printf("Fibonacci series :-");
for(i=1;i<=n;i++)
{
printf("\n%d",t1);
nexterm=t1+t2;
t1=t2;
t2=nexterm;
}
getch();
}

5
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

6
Ex. No: 4
Date:

PRIME NUMBERS
AIM :-

ALGORITHM :-

1. Start the program.


2. Declare the integer variables as a, i, j, n and c.
3. Get the input value from the user.
4. In for loop, check the condition whether the given number is prime or not, if so set flag.
5. If the flag value is equal to other them it point the given number is not prime otherwise
it display the given number is not prime.
6. Compile and execute the program.
7. End the program

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,j,n,c=0;
clrscr();
printf(“Give a number : ”);
scanf(“%d”,&n);
printf(“Prime numbers from 1 to %d\n”,n);
for(j=1;j<=n;j++)
{
c=0;
a=j;
for(i=1;i<=a;i++)
{
if(a%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf(“\n%d”,a);
}
}
getch();
}

7
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

8
Ex. No: 5
Date:

FACTORIAL USING RECURSIVE FUNCTION

AIM :

ALGORITHM :-

1. Start the program.


2. Get the input value from user.
3. Using formula (n*fact(n-1)) calculate the factorial value of given number using
recursive function.
4. Compile and execute the program.
5. End the program.

PROGRAM :-

#include <stdio.h>
#include<conio.h>
void main()
{
int num,f,fact();
clrscr();
printf("\n******************************************");
printf("\n FACTORIAL USING RECURSIVE FUNCTION");
printf("\n******************************************");
printf(“\nEnter the number to calculate Factorial: ”);
scanf(“%d”,&num);
f=fact(num);
printf(“\n Factorial is %d”,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}

9
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

10
Ex. No: 6
Date:

PALINDROMES IN A GIVEN SENTENCE


AIM :

ALGORITHM :-

1. Start the program.


2. Declare the characters array text.
3. With the gets statement get input from text .
4. Using a while loop split each word using space still the end of sentence.
5. Take each word and check if its a palindrome
6. If palindrome increase count and display the word
7. Check each word and repeat step 6 and 7.
8. Compile and execute the program.
9. Display the total number of palindrome words in the sentences.
10. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
char t[50],s[20];
int i,j=0,count=0,p=0;
clrscr();
printf("Give a Sentence ");
gets(t);
while(t[j]!='\0')
{
i=0;
count=0;
while(t[j]!=' ' && t[j]!='\0')
s[i++]=t[j++];
s[i]='\0';
if (t[j]!='\0')
j++;
i=0;
while(s[i++]!='\0')
count++;
for(i=0;i<count/2;i++)
if(s[i]!=s[count-1-i])
break;

if(i==count/2)

11
{
printf("%s is palindrome \n",s);
p++;
}
else
printf("%s is not palindrome \n",s);
}
printf("%d number of palaindrome in %s",p,t);
getch();
}

12
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

13
Ex. No: 7
Date:

STRING FUNCTION
AIM : -

ALGORITHM :-

1. Start the program.


2. Get two strings as input.
3. Calculate the length of the two strings using strlen() and display them.
4. Convert first string into upper case letters using strupr().
5. Convert second string into lower case letters using strlwr().
6. Compare the strings and display whether they are equal or not.
7. Concatenate the two strings using strcat().
8. Compile and execute the program.
9. Display the result.
10. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
int diff,l,l2;
clrscr();
printf("STRING FUNCTIONS\n");
printf("\nEnter your first string : ");
gets(s1);
printf("Enter your second string : ");
gets(s2);
l=strlen(s1);
l2=strlen(s2);
printf("\nString 1 length is = %d",l);
printf("\nString 2 length is = %d",l2);
printf("\nThe given string 1 in upper case is = %s",strupr(s1));
printf("\nThe given string 2 in lower case is = %s",strlwr(s2));
diff=strcmp(s1,s2);
printf("\nString comparison is = ");
if(diff==0)
{
printf("Both strings are equal");
}
else
{

14
printf("Both strings are not equal");
}
printf("\nString 1 + String 2 is = %s",strcat(s1,s2));

15
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

16
Ex. No: 8
Date:

FUNCTIONS

AIM :-

ALGORITHM :-

1. Start the program.


2. Declare the user defined functions add(), sub(), mul(), div().
3. Get input of two numbers as operand values .
4. Using the user defined functions and operand get the process done.
5. Compile and execute the program.
6. Display the result.
7. End the program.

PROGRAM :-

#include<stdio.h>
int add(int n1, int n2);
int sub(int n1, int n2);
int mul(int n1, int n2);
int div(int n1, int n2);
int main()
{
int num1, num2;
printf("\n***********");
printf("\nFUNCTIONS");
printf("\n***********");
printf("\nEnter two numbers:- \n");
scanf("%d %d", &num1, &num2);
printf("\nArithmetic operation using functions: \n");
printf("%d + %d = %d\n", num1, num2, add(num1, num2));
printf("%d - %d = %d\n", num1, num2, sub(num1, num2));
printf("%d * %d = %d\n", num1, num2, mul(num1, num2));
printf("%d / %d = %d\n", num1, num2, div(num1, num2));
getch();
return 0;
}
int add(int n1, int n2)
{
int result;
result = n1 + n2;
return result;
}
int sub(int n1, int n2)
{

17
int result;
result = n1 - n2;
return result;
}
int mul(int n1, int n2)
{
int result;
result = n1 * n2;
return result;
}

int div(int n1, int n2)


{
int result;
result = n1 / n2;
return result;
}

18
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

19
Ex. No: 9
Date :

SWAP TWO INTEGERS USING POINTERS


AIM :-

ALGORITHM :-

1. Start the program.


2. Declare a, b, t and two pointer variables *p, *q.
3. Get the values for a and b from the user .
4. Using printf function print the values swapping.
5. Assign address of a to p and b to q.
6. Using pointer variable move the value of p in t then swap the values of p and q.
7. Now assign the value of t to p.
8. Compile and execute the program.
9. Print the value of pointer p and q after swapping.
10. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p,*q,t;
clrscr();
printf("\n*************************************");
printf("\nSWAP TWO INTEGERS USING POINTERS");
printf("\n*************************************");
printf(“\nEnter the two values:”);
scanf(“%d %d”,&a,&b);
printf(“\n before swapping: %d , %d”,a,b);
p=&a;
q=&b;
t=*p;
*p=*q;
*q=t;
printf(“\n after swapping: %d , %d”,*p,*q);
getch();
}

20
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

21
Ex. No: 10
Date :

ARRAY OF POINTERS

AIM :-

ALGORITHM :-
1. Start the program.
2. Declare the integer variables a, b, c, and I.
3. Declare pointer array arr as *arr[3].
4. Initialize a, b, c with 10, 20 and 50.
5. Initialize array pointers to a, b, and c.
6. Compile and execute the program.
7. Print the address and values of a, b, and c using pointer array.
8. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
int *arr[3];
int a = 10, b = 20, c = 50, i;
printf("\n********************");
printf("\nARRAY OF PONTERS");
printf("\n********************");
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
for(i = 0; i < 3; i++)
{
printf("\nAddress = %u\t Value = %d\n", arr[i], *arr[i]);
}
getch();
}

22
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

23
Ex. No: 11
Date :

STRUCTURE
AIM :

ALGORITHM :-

1. Start the program.


2. Declare the structure as keyword struct and structure record name as employee.
3. Declaring three variables name, adr, city, sal, da, hra, pf, ns, pin at different datatypes
as int, char and long.
4. Using strcpy string functions copy the employee name.
5. Using structure dot member access the data value of each employee .
6. Calculate the salary of the employee.
7. Compile and execute the program.
8. Display the salary details.
9. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
struct employee
{
char name[20],adr[20],city[20];
int sal,da,hra,pf,ns;
long pin;
}emp;
clrscr();
printf("\n *******************************");
printf("\n ******PGM:11 STRUCTURES******");
printf("\n *******************************");
printf("\n Enter the employee name=");
scanf("%s",&emp.name);
printf("\n Enter basic salary=");
scanf("%d",&emp.sal);
emp.hra=emp.sal*0.10;
emp.pf=emp.sal*0.12;
emp.da=emp.sal*0.20;
emp.ns=emp.sal+emp.da+emp.hra+emp.pf*0.20;
printf("\n Enter employee address=");
scanf("%s",&emp.adr);
printf("\n Enter city=");
scanf("%s",&emp.city);

24
printf("\n *******************************");
printf("\n Employee Information");
printf("\n *******************************");
printf("\n Employee Name= %s",emp.name);
printf("\n Employee Address= %s",emp.adr);
printf("\n City %s",emp.city);
printf("\n Employee salary= %d",emp.sal);
printf("\n DA= %d",emp.da);
printf("\n HRA= %d",emp.hra);
printf("\n PF= %d",emp.pf);
printf("\n Net Salary= %d",emp.ns);
getch();
}

25
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

26
Ex. No: 12
Date :

ARRAY OF STRUCTURES
AIM :

ALGORITHM :-

1. Start the program.


2. Declare the structure name as student.
3. Declare the data type int as rollno and char as name.
4. Create a structure array st[2] here it is the student record .
5. Initialize the value id i = 0 and check the condition using for loop its value is less than 3.
6. Enter the names and roll numbers of two students.
7. Access each record array name as st[i] st[i].rollno, st[i].name
8. Compile and execute the program.
9. Display the names and roll numbers.
10. End the program.

PROGRAM :-
#include<stdio.h>
#include <conio.h>
struct student
{
int rollno;
char name[10];
};
void main()
{
int i;
struct student st[2];
clrscr();
printf("****************************");
printf("ARRAY OF STRUCTURES");
Printf("****************************");
printf("Enter Records of 2 students");
for(i=0;i<2;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<2;i++)
{
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}

27
getch();
}
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

28
Ex. No: 13
Date :

ELECTRICITY BILL USING FILE


AIM :

ALGORITHM :-

1. Start the program.


2. Declare the file pointer *fp.
3. Using the file pointer ,with the fopen() open a file in w+ mode char the reading and
writing contexts.
4. Enter the name and address of the customer.
5. Enter the units at electricity bills consumed.
6. Based on the units consumed calculate the amount using if else statement.
7. Calculate the total Bill Amount.
8. Compile and execute the program.
9. Display the total amount .
10. End the program.

PROGRAM :-

#include<stdio.h>
#include<conio.h>
void main()
{
int unit;
float amt, charge,total;
char name[25],address[20];
FILE *fp;
clrscr();
fp=fopen(“data.txt”,”w+”);
printf(“\n Enter the name “);
scanf(“%s”, &name);
printf(“\n Enter the address \n”);
scanf(“%s”, &address);
printf(“\n Enter the units consumed \n”);
scanf(“%d”,&unit);
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{

29
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}
charge = amt * 0.20;
total = amt + charge;
printf(“\nName: %s \nAddress: %s \nUnits: %d \nAmount: %f”,name,address,unit,total);
printf(”\n %s %s %d %f”, name,address,unit,total);
fclose(fp);
}

30
OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

31
Ex. No: 14
Date :

C PROGRAM TO ENCRYPT AND DECRYPT A STRING

AIM :

ALGORITHM :-

1. Start the program.


2. Declare variables of different datatypes like i, x and str[100].
3. Get input of string from user .
4. Using switch statement choose Encryption or Decryption
case 1: Encryption
case 2: Decryption
5. Perform encryption or decryption according to the case chosen.
6. Compile and execute the program.
7. Display the final string.
8. End the program.

PROGRAM :-

#include <stdio.h>
int main()
{
int i, x;
char str[100];
printf("Please enter a string: ");
gets(str);
printf("\nPlease choose following options:\n");
printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);
switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 3;
printf("\nEncrypted string: %s\n", str);
break;
case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 3;
printf("\nDecrypted string: %s\n", str);
break;
default:
printf("\nError\n");
}
return 0;

32
}

OUTPUT :-

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

33
Ex. No: 15
Date :

ENCRYPT AND DECRYPT FILES


AIM :

ALGORITHM :-

1. Start the program.


2. Declaring character variables ch, fname[20].
3. Declare file pointer fpt1 and fpt2.
4. Input the name of file to be encrypted also input the key for encryption.
5. Open the file in write mode.
6. In a loop still the end of file, take every character in the file and encrypt(add) it with
key, and write it to the file.
7. Display the encrypted content in the file
8. For decryption take the same file and decrypt it using the key. Use a loop to take all
character .
9. Compile and execute the program.
10. Display the contents in file.
11. End the program.

PROGRAM :

#include <stdio.h>
#include <string.h>
#define MAX 256

int main()
{
FILE *fp1, *fp2;
int key, val, ch, i = 0;
char filename[MAX], temp[] = "temp.txt";

printf("Enter your file name:");


scanf("%s", filename);
fp1 = fopen(filename, "r");

if (!fp1)
{
printf("Unable to open the input file!!\n");
return 0;
}

fp2 = fopen(temp, "w");


if (!fp2) {
printf("Unable to open the temporary file!!\n");
return 0;
}

34
printf("Enter the key to create cipher text:");
scanf("%d", &key);

while ((ch = fgetc(fp1)) != EOF)


{

val = ch + key;
fprintf(fp2, "%d ", val);
i++;

if (i % 10 == 0) {
fprintf(fp2, "\n");
}
}

fclose(fp1);
fclose(fp2);

fp2 = fopen(temp, "r");


printf("\nCipher text for the given plain text:\n");
while (!feof(fp2)) {
fscanf(fp2, "%d", &ch);
if (!feof(fp2)) {
printf("%c", ch);
}
}
rewind(fp2);

printf("\n\nConverting the cipher text to plain text:\n");


while (!feof(fp2)) {
fscanf(fp2, "%d", &ch);
if (!feof(fp2)) {
val = ch - key;
printf("%c", val);
}
}
printf("\n");

fclose(fp2);
return 0;
printf("\n");

RESULT:-

Thus the program has been successfully compiled and the output
has been verified.

35

You might also like