0% found this document useful (0 votes)
2 views

CS3271 Programming in C lab

The document is a practical record for the CS3271 Programming in C Laboratory course at Arulmurgan College of Engineering, detailing various C programming exercises. It includes aims, algorithms, sample programs, outputs, and results for tasks such as using I/O statements, operators, and control structures. The document serves as a record of the student's work and is intended for submission for university practical examinations.

Uploaded by

dhandathamizh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS3271 Programming in C lab

The document is a practical record for the CS3271 Programming in C Laboratory course at Arulmurgan College of Engineering, detailing various C programming exercises. It includes aims, algorithms, sample programs, outputs, and results for tasks such as using I/O statements, operators, and control structures. The document serves as a record of the student's work and is intended for submission for university practical examinations.

Uploaded by

dhandathamizh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

ARULMURUGAN COLLEGE OF ENGINEERING

(Approved by AICTE, New Delhi and Affiliated to Anna University, Chennai & ISO 9001:2015 Certified Institution)

THENNILAI, KARUR - 639 206.

DEPARTMENT OF COM P UT ER S CI E NCE AND


ENGINEERING

CS3271 – PROGRAMMING IN C LABORATORY

I YEAR / II SEMESTER

Name :

Reg No :

Subject name :

Academic Year :

1
ARULMURUGAN COLLEGE OF ENGINEERING
(Approved by AICTE and Affiliated to Anna University)
THENNILAI, KARUR – 639 206.

PRACTICAL RECORD

Register Number

Name

Year / Sem

Degree / Branch

Subject Code & Name

Certified that this is a bonafide record of work done by the above student
during the year 20 - 20

Staff in-charge Head of the Department

Submitted for the University Practical Examination held on

Internal Examiner External Examiner


INDEX

Ex. Staff
Date Name of the Experiment Marks Page No
No Signature
EX.NO.:1A PROGRAMS USING I/O STATEMENTS

AIM:
To Write a C program to use Input and Output Statements

ALGORITHM:
Step 1:Start the program.
Step 2:Declare all required variables and initialize them.
Step 3:Get input values from the user for arithmetic operation.
Step 4:Use input function scanf() and output function printf() to display the output after
the calculation.
Step 5:Stop the program.

PROGRAM:
#include<stdio.h>
void main()
{
int age, bir_year, cur_year;
printf("Enter the year of birth:\n");
scanf("%d",&bir_year);
printf("Enter the current year:\n");
scanf("%d",&cur_year);
age=cur_year-bir_year;
printf("The age is:%d years",age);
}

OUTPUT:

Enter the year of birth:


1996
Enter the current year:
2022
The age is:26 years

RESULT:
Thus the program for I/O statements has been written & executed successfully.
EX.NO. :1B PROGRAMS USING OPERATORS

AIM:
To Write a C programs using operators.

Algorithm:
Step 1. Start the program
Step 2. Use Arithmetic operators (+,-,*,/,%)
Step 3. Use Increment & Decrement Operators (++,--)
Step 4. Use comparison operators (>,<,>=,<=,==,!=)
Step 5. Use Bit wise operators (~,^,|,&)
Step 6. Use logical operators (||,&&,!)
Step 7. Use Ternary Operator (?:)
Step 8. Stop the program

PROGRAMS:
//Arithmetic Operators

#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Addition - Value of c is %d\n", c );
c = a - b;
printf("Subraction - Value of c is %d\n", c );
c = a * b;
printf("Multiplication - Value of c is %d\n", c );
c = a / b;
printf("Division - Value of c is %d\n", c );
c = a % b;
printf("Modulo - Value of c is %d\n", c );
c = a++;
printf("Increment- Value of c is %d\n", c );
c = a--;
printf("Decrement - Value of c is %d\n", c );
}

//Assignment Operators

#include <stdio.h>
int main()
{
int a = 99, result;
result = a;
printf("Value of result = %d\n", result);
result += a; // or result = result + a
printf("Value of result = %d\n", result); // After Addition
result -= a; // or result = result - a
printf("Value of result = %d\n", result); // After Subtraction
result *= a; // or result = result * a
printf("Value of result = %d\n", result); // After Multiplication
result /= a; // or result = result / a
printf("Value of result = %d\n", result);
return 0;
}
//Relational Operators
#include <stdio.h>
int main()
{
int j = 6, t = 4;

printf("%d == %d is %d \n", j, t, j == t);


printf("%d > %d is %d \n", j, t, j > t);
printf("%d < %d is %d \n", j, t, j < t);
printf("%d != %d is %d \n", j, t, j != t);
printf("%d >= %d is %d \n", j, t, j >= t);
printf("%d <= %d is %d \n", j, t, j <= t);

return 0;
}

//Logical Operators

#include <stdio.h>
int main()
{
int i = 5, j = 5, k = 10, final;
printf("i is equal to j or k greater than j is is %d \n", (i == j) && (k > j));
printf("i is equal to j or k less than j is %d \n", (i == j) || (k < j));
printf("i not equal to j or k less than j is %d \n", (i != j) || (k < j));
return 0;
}
OUTPUT:
Arithmetic Operators
Addition - Value of c is 31
Subraction - Value of c is 11
Multiplication - Value of c is 210
Division - Value of c is 2
Modulo - Value of c is 1
Increment- Value of c is 21
Decrement - Value of c is 22
Assignment Operators
Value of result = 99
Value of result = 198
Value of result = 99
Value of result = 9801
Value of result = 99
Relational Operators
6 == 4 is 0
6 > 4 is 1
6 < 4 is 0
6 != 4 is 1
6 >= 4 is 1
6 <= 4 is 0
Logical Operators
i is equal to j or k greater than j is is 1
i is equal to j or k less than j is 1
i not equal to j or k less than j is 0

RESULT:
Thus the program was written & executed successfully
EX.NO.:1C FINDING ODD OR EVEN

AIM:
To Write a C program to find whether a given number is odd or even

ALGORITHM:
Step 1. Start the program
Step 2. Read an input Value
Step 3. Divide it by 2.
Step 4. Take the remainder, if the remainder is greater than zero , then
Step 5. Print the given Number is EVEN.
Step 6. Otherwise, print the given Number is ODD.
Step 7. Stop the Programs

PROGRAM:
#include<stdio.h>
int main()
{
// This variable is to store the input number
int num;

printf("Enter an integer: ");


scanf("%d",&num);

// Modulus (%) returns remainder


if ( num%2 == 0 )
printf("%d is an even number", num);
else
printf("%d is an odd number", num);

return 0;
}

OUTPUT:

Enter an integer: 25
25 is an odd number

RESULT:
Thus the program was written & executed successfully.
EX.NO.:2 A POSITIVE NUMBER OR NEGATIVE NUMBER

AIM:
To write a C program to check whether the given number is positive number or negative
number

ALGORITHM:

Step 1:Start the program.


Step 2:Declare all required variables.
Step 3:Get an input from the user and store it in a variable.
Step 4:Check the input value whether it is a positive number or negative number.
Step 5:If the number is less than ZERO, then print the result as “NEGATIVE”. Otherwise
display the result as “POSITIVE”.
Step 6:Stop the program.

PROGRAM:

#include <stdio.h>

void main()
{
int num;

printf("Enter a number: \n");


scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}
OUTPUT:

Enter a number:
25
25 is a positive number

RESULT:
Thus the program was written & executed successfully.
EX.NO.:2B MULTIPLICATION TABLE

AIM:
To write a C program to generate multiplication table for given value using goto
statement

ALGORITHM:

Step 1:Start the program.


Step 2:Declare all required variables.
Step 3: Get the table value from user
Step 4:Declare label statement
Step 5: Iterate the loop till it reaches condition
Step 6:Stop the program.

PROGRAM:

#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
OUTPUT:
Enter the number whose table you want to print?
3
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

RESULT:
Thus the program was written & executed successfully.
EX.NO.:2 C CALCULATE THE AREA OF SHAPES

AIM:
To Write a C program to calculate the area of shapes using switch condition

ALGORITHM:
Step 1: Start
Step 2: Initialize variables
Step 3: Take input for choice and then for area variables from the user
Step 4: Apply the formula for shape
Step 5: Display output according to case
Step 6: Stop

PROGRAM:
#include<stdio.h>
void main()
{
int n;
printf("enter any number from 1t07:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("happy monday");
break;
case2:
printf("thought full tuesday");
break;
case3:
printf("wonderfull wednesday");
break;
case4:
printf("thundering thursday");
break;
case 5:
printf("fantastic friday");
break;
case 6:
printf("super saturday");
break;
case 7:
printf("sunny sunday");
break;
}
}
OUTPUT:

enter any number from 1t07:6


super saturday

RESULT:
Thus the program was written & executed successfully.
EX.NO.:3A STAR PYRAMID

AIM:
To Write a C program to print start pyramid using for loop.

ALGORITHM:
Step 1: Start
Step 2: Read number num
Step 3: Initialize r=1
Step 4: Repeat step 4 through 10 until num>=r
Step 5: initialize c=1
Step 6: Repeat step 6 through 8 until c<=r
Step 7: print */#
Step 8: c=c+1
Step 9: go to next line
Step 10: r=r+1
Step 11: stop

PROGRAM:
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
for(r=1; num>=r; r++)
{
for(c=1; c<=r; c++)
printf("#");
printf("\n");
}
return 0;
}
OUTPUT:

Enter loop repeat number(rows): 4


#
##
###
####

RESULT:
Thus the program was written & executed successfully.
EX.NO.:3B FIND WHETHER GIVEN NUMBER IS A ARMSTRONG NUMBER
OR NOT

AIM:
To write a C program to check whether the given number is an Armstrong number or not
using while loop.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare all required variables and initialize the result variable “sum” to 0.
Step 3: Get an input from the user and store it in “n” and “n_copy” variables.
Step 4: Using the user input, perform the following operation until it becomes ZERO
r = n%10;
sum = sum + (r*r*r);
n = n/10;
Step 5: compare the value of “sum” and n_copy”. If they are equal then go to step no 6
otherwise go to step no 7.
Step 6: print the result as “It is an Armstrong Number” and go to step 8.
Step 7: print the result as “It is not an Armstrong Number”.
Step 8: Stop the program.

PROGRAM:

#include <stdio.h>
void main()
{
int n, sum=0;
int r, n_copy;
printf("Enter a number\t");
scanf("%d", &n);
n_copy = n;
while(n>0)
{
r = n%10;
sum = sum + (r*r*r);
n = n/10;
}
if(sum == n_copy)
printf("%d is an armstrong number", n_copy);
else
printf("%d is not an armstrong number", n_copy);
}
OUTPUT:

Enter a number6
6 is not an armstrong number

RESULT:
Thus the program was written & executed successfully.
EX.NO.:3C FIND WHETHER A NUMBER IS PALINDROME OR NOT

AIM:
To write a C program to check whether the given number is an palindrome number or not
using do while loop.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare all required variables and initialize the result variable s to 0.
Step 3: Get an input from the user and store it in “n”
Step 4: Using the user input, perform the following operation until it becomes ZERO
r = n%10;
s=s*10+r;
n = n/10;
Step 5: compare the value of a and s. If they are equal then go to step no 6 otherwise go
to step no 7.
Step 6: print the result as “It is an Palindrome Number” and go to step 8.
Step 7: print the result as “It is not an Palindrome Number”.
Step 8: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,r,s=0;
printf("\n Enter The Number:");
scanf("%d",&n);
a=n;

do
{
r=n%10;
s=s*10+r;
n=n/10;
}while(n>0);
if(a==s)
{
printf("\n %d is a Palindrome Number",a);
}
else
{
printf("\n %d is a not Palindrome Number",a);
}
}
OUTPUT:

Enter The Numeber:121


121 is a Palindrome Number

RESULT:
Thus the program was written & executed successfully.
EX.NO:4A CALCULATE MEDIAN OF AN ARRAY

AIM:
To write a C program to calculate Median of an array

ALGORITHM:

Step 1: Read the items into an array while keeping a count of the items.
Step 2: Sort the items in increasing order.
Step 3: Compute median.

PROGRAM:
#include<stdio.h>
#define N 5

void main()
{
int i, j, n;

float median, a[N], temp;


printf("Enter number of elements:\n");
scanf("%d", &n);

/* Reading array elements */


printf("Input %d values \n", n);
for(i=0; i<n; i++)
{
scanf("%f", &a[i]);
}

temp =0;
/* sorting */
for(i=0;i<n-1;i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]<a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
/* calculation median */
if( n%2 == 0)
median = (a[(n/2)-1]+a[(n/2)])/2.0;
else
median = a[(n/2)];

/*printing result */

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


printf("%f\t", a[i]);

printf("\nMedian is %f\n", median);


}

OUTPUT:

Enter number of elements:


8
Input 8 values

RESULT:
Thus the program was written & executed successfully.
EX.NO:4B MULTIPLICATION OF TWO MATRICES

AIM:
To write a C program find multiplication of two matrices using two dimensional array.

ALGORITHM:
Step 1: Start the Program.
Step 2: Enter the row and column of the first (a) matrix.
Step 3: Enter the row and column of the second (b) matrix.
Step 4: Enter the elements of the first (a) matrix.
Step 5: Enter the elements of the second (b) matrix.
Step 6: Print the elements of the first (a) matrix in matrix form
Step 7: Print the elements of the second (b) matrix in matrix form.
Step 8: Set a loop up to row.
Step 9: Set an inner loop up to the column.
Step 10: Set another inner loop up to the column.
Step 11: Multiply the first (a) and second (b) matrix and store the element in the third
matrix (c)
Step 12: Print the final matrix.
Step 13: Stop the Program.

PROGRAM:
#include <stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
printf("Enter the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the second matrix\n");
scanf("%d%d",&r,&s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
{
printf("\n Enter the elements of first matrix ");
for(i= 0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\n Enetr the elements of second matrix ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\n The element of first matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\n The element of second matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplication of two matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",c[i][j]);
}
}
OUTPUT:

Enter the first matrix


11
Enter the second matrix
21
The matrix cannot multiplied
Multiplication of two matrix is
8

RESULT:
Thus the program was written & executed successfully.
EX.NO:5A STRING LENGTH

AIM:
To write a C program to find String Length.

ALGORITHM:

Step 1: Start the Program.


Step 2: Call string module
Step 3: Get the values
Step 4: Calculate length using building function
Step 5: Display the result
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}

OUTPUT:

Length of string is: 10

RESULT:
Thus the program was written & executed successfully.
EX.NO:5B COPY STRING

AIM:
To write a C program to copy a string from source to destination.

ALGORITHM:
Step 1: Start the Program.
Step 2: Call string module
Step 3: Get the values
Step 4: Copy string using building function
Step 5: Display the result
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
main()
{
char source="KANI";
char target[10];
strcpy(target,source);
printf("\n Source string is %s",source);
printf("\n Target string is %s",target);
}

OUTPUT:

Source string is KANI


Target string is KANI

RESULT:
Thus the program was written & executed successfully.
EX.NO:5C STRING CONCATENATION

AIM:
To write a C program to concatenate two strings.

ALGORITHM:
Step 1: Start the Program.
Step 2: Call string module
Step 3: Get the values
Step 4: concatenate two strings using building function
Step 5: Display the result
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}

OUTPUT:

Value of first string is: helloc

RESULT:
Thus the program was written & executed successfully.
EX.NO:5D STRING COMPARE

AIM:
To write a C program to compare two strings.

ALGORITHM:
Step 1: Start the Program.
Step 2: Call string module
Step 3: Get the values
Step 4: Compare two strings using building function
Step 5: Display the result
Step 6: Stop the program

PROGRAM:

#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}

OUTPUT:

Enter 1st string: KANI


Enter 2nd string: NANI
Strings are not equal

RESULT:
Thus the program was written & executed successfully
EX.NO:5E STRING REVERSE

AIM:
To write a C program to print a reverse of a string

ALGORITHM:
Step 1: Start the program.
Step 2: Read a paragraph.
Step 3: Count the no. of words
Step 4: Capitalize the first word of each sentence.
Step 5: Print the capitalized text and no. of words.

PROGRAM:
#include <stdio.h>
#include <string.h>
void revstr(char *str1)
{
int i, len, temp;
len = strlen(str1);
// use for loop to iterate the string
for (i = 0; i < len/2; i++)
{
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
}
}
int main()
{
char str[50];
printf (" Enter the string: ");
gets(str);
printf (" \n Before reversing the string: %s \n", str);
revstr(str);
printf (" After reversing the string: %s", str);
}
OUTPUT:

Enter the string: HAI NANDHU


Before reversing the string: HAI NANDHU
After reversing the string: UHDNAN IAH

RESULT:
Thus the program was written & executed successfully.
.
EX.NO.: 6A CHECK WHETHER A NUMBER IS PRIME OR NOT

AIM:
Write a C program to check whether a number is Prime or not using function.
ALGORITHM:
Step 1: Start
Step 2: Declare a variable.
Step 3: Initialize the variable
Step 4: Call a function to check whether the number is prime or not
Step 5: Use a for loop that iterates from 2 to N/2
Step 6: Declare the count and initialize it to 0.
Step 7: If the number is divisible by any of the numbers in between the loop then
increment the count.
Step 8: If the count is not equal to 0 then, it is not a prime number.
Step 9: If the count is equal to 0, then it is a prime number.
Step 10: Stop
PROGRAM:
#include <stdio.h>
void checkPrime(int num); //Function Definition

int main()
{
int num; //Declare a number
printf("Enter the number\n");
scanf("%d",&num); //Initialize the num

checkPrime(num); //Function Call

return 0;
}
void checkPrime(int num) //Function Definition
{
int count=0;
for(int i=2;i<=num/2;i++) //Loop to check for factors
{
if(num%i==0)
{
count++; //If factors found then increment the count
break;
}
}
if(count!=0) //Check whether prime or not
{
printf("Not a prime number\n");
}
else
{
printf("Prime number\n");
}
}
OUTPUT:

Enter the number


5
Prime number

RESULT:
Thus the program was written & executed successfully.
EX.NO.: 6B SORT THE LIST OF NUMBERS USING PASS BY REFERENCE

AIM:
Write a C program to sort numbers using pass by Reference
ALGORITHM:
Step 1: Start the Program
Step 2: Read the size of array
Step 3: Read the elements one by one
Step 4: Call the function sort()
Step 5: Pass the stating address of the array to the sort function as input.
Step 6: Store the address in the formal argument of that function (Pointer variable)
Step 7: Access the array values by (increasing the addresses) the pointer variable
Step 8: Sort the values using the pointer variable
Step 9: Print the Result
Step 10: Stop the program

PROGRAM:
#include<stdio.h>
// Function to sort the numbers using pointers
void sort(int n, int* ptr)
{
int i, j, t; // Sort the numbers using pointers
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
{
if (*(ptr + j) < *(ptr + i))
{
t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}
// print the numbers
for (i = 0; i < n; i++)
printf("%d ", *(ptr + i));
}
// Driver code
int main()
{
int n = 7;
int arr[] = { 0, 20, 17, 32, 7, 19, 77 };
sort(n, arr);
return 0;
}
OUTPUT:

0 7 17 19 20 32 77

RESULT:
Thus the program was written & executed successfully
EX.NO.: 7 FACTORIAL OF A GIVEN NUMBER USING RECURSIVE FUNCTION

AIM:
To Write a C program to find factorial of a given number using Recursive function.
ALGORITHM:
Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return f
PROGRAM:

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

OUTPUT:

Enter a positive integer: 6


Factorial of 6 = 720

RESULT:
Thus the program was written & executed successfully
EX.NO.: 8A EMPLOYEE DETAILS

AIM:
To Write a C program to generate employee details using structure & pointer.

ALGORITHM:
Step 1:Start the program.
Step 2:Create a structure called Employee with empid,empname,dept,designation & salary
fields.
Step 3:Create a pointer to a structure.
Step 4:Call a function to generate the Salary slip of particular employee
Step 5:Stop the program.

PROGRAM:
#include <stdio.h>
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare structure variable*/
struct employee emp;
/*read employee details*/
printf("\nEnter details :\n");
printf("Name ?:"); gets(emp.name);
printf("ID ?:"); scanf("%d",&emp.empId);
printf("Salary ?:"); scanf("%f",&emp.salary);
/*print employee details*/
printf("\nEntered detail is:");
printf("Name: %s" ,emp.name);
printf("Id: %d" ,emp.empId);
printf("Salary: %f\n",emp.salary);
return 0;
}
OUTPUT:

Enter details :
Name ?: mohan raj
mohan raj
ID ?:12
Salary ?:10000
Entered detail is:Name: mohan rajId: 12Salary: 10000.000000

RESULT:

Thus the program was written & executed successfully.


EX.NO.: 8B FIND BIGGEST AMONG THREE NUMBERS USING POINTER

AIM:
To Write a C program to find biggest among three numbers using pointer.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare a pointer variable
Step 3: Compare three numbers
Step 4: Stop the program.

PROGRAM:

#include<stdio.h>
int main()
{
int a,b,c,*pa, *pb, *pc;
printf("Enter three numbers:\n");
scanf("%d%d%d", &a,&b,&c);
/* Referencing */
pa= &a;
pb= &b;
pc= &c;
if(*pa > *pb && *pa > *pc){
printf("Biggest is: %d", *pa);
}
else if(*pb > *pc && *pb > *pc)
{
printf("Biggest is : %d", *pb);
}
else
{
printf("Biggest is= %d", *pc);
}
return 0;
}
OUTPUT:

Enter three numbers:


3
4
5
Biggest = 5

RESULT:

Thus the program was written & executed successfully


EX. NO.: 9A NESTED STRUCTURE

AIM:
To display your name and date of birth using nested structure

ALGORITHM:
Step 1: Start
Step 2: Declare nested structure for a person
Step 3: Assign person details to structure
Step 4: Print structure details
Step 5: Stop

PROGRAM:

#include <stdio.h>
struct student {
char name[30];
int rollNo;
struct dateOfBirth {
int dd;
int mm;
int yy;
} DOB; /*created structure varoable DOB*/
};
int main()
{
struct student std;
printf("Enter name: ");
gets(std.name);
printf("Enter roll number: ");
scanf("%d", &std.rollNo);
printf("Enter Date of Birth [DD MM YY] format: ");
scanf("%d%d%d", &std.DOB.dd, &std.DOB.mm, &std.DOB.yy);
printf("\nName : %s \nRollNo : %d \nDate of birth : %02d/%02d/%02d\n", std.name,
std.rollNo, std.DOB.dd, std.DOB.mm, std.DOB.yy);
return 0;
}
OUTPUT:

Enter name: LOGESH


Enter roll number: 114
Enter Date of Birth [DD MM YY] format: 25 06 2000
Name : LOGESH
RollNo : 114
Date of birth : 25/06/2000

RESULT:
Thus the program was written & executed successfully
EX.NO.: 9B STUDENT INFORMATION USING ARRAY OF STRUCTURES

AIM:
To write a C program to store information of a student using an array of Structures with
three fields (Name, Roll number, marks).

ALGORITHM:
Step1: start
Step2: enter the student name
Step3: if(strcmp(s[i].name,roll no and
marks)
sname==0)
Then
{
Print the marks of students name & USN
}
Else
{
Print the given date is invaid
}
Step4: stop

PROGRAM:

#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
OUTPUT:

Enter information:
Enter name: GOBINATH
Enter roll number: 14
Enter marks: 89
Displaying Information:
Name: GOBINATH
Roll number: 14
Marks: 89.0

RESULT:

Thus the program was written & executed successfully


EX.NO.: 9C UNION

AIM:
To Write a C program to display accessing union members using union
ALGORITHM:
Step 1: Start
Step 2: Declare union for job
Step 3: Assign worker salary and worker
no
Step 4: Print union details
Step 5: Stop
PROGRAM:

#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 15;
// when j.workerNo is assigned a value,
// j.salary will no longer hold 12.3
j.workerNo = 100;
printf("Salary = % .1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}
OUTPUT:

Salary = 0.0
Number of workers = 100

RESULT:
Thus the program was written & executed successfully
EX.NO.: 10A DISPLAY THE CONTENTS OF A FILE

AIM:
To Write a C program to display the contents of a file.
ALGORITHM:
Step 1: Start
Step 2: Read file name
Step 3: Open file in read mode
Step 4: Repeat until end of the file and print the contents if the file
Step 5: Close the file
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
FILE *fp1,*fp2;
char c;
void main()
{
clrscr();
printf("enter the text\n");
fp1 = fopen("abc.txt", "w");
while((c = getchar()) != EOF)
putc(c, fp1);
fclose(fp1);
fp1 = fopen("abc.txt","r");
fp2=fopen("xyz.txt","w");
while(!feof(fp1))
{
c = getc(fp1);
putc(c,fp2);
}
fclose(fp1);
fclose(fp2);
printf("the copied data is \n");
fp2 = fopen("xyz.txt", "r");
while(!feof(fp2))
{
c = getc(fp2);
printf("%c", c);
}
getch();
}
OUTPUT:

enter the text


engineering students are very good.
the copied data is
engineering students are very good.

RESULT:
Thus the program was written & executed successfully
EX.NO: 10B RANDOM ACCESS FILE

AIM:
To Write a C program to update telephone details of an individual or a company into a
telephone directory using random access file.

ALGORITHM:
Step 1: Start the program
Step 2: Store the telephone details into a file
Step 3: Read the data & Display it
Step 4: Enter the telephone number to be modified & the new number
Step 5: Use fseek() function to randomly access the record
Step 6: Copy the contents from source file to destination file
Step 7: Store the updated record into the new file
Step 8: Stop the program

PROGRAM:

#include "stdio.h"
#include "string.h"
struct dir
{
char name[20];
char number[10];
};
void insert(FILE *);
void update(FILE *);
void del(FILE *);
void display(FILE *);
void search(FILE *);
int record = 0;
int main(void) {
int choice = 0;
FILE *fp = fopen( "telephone.dat", "rb+" );
if (fp == NULL ) perror ("Error opening file");
while (choice != 6)
{
printf("\n1 insert\t 2 update\n");
printf("3 delete\t 4 display\n");
printf("5 search\t 6 Exit\n Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(fp); break;
case 2: update(fp); break;
case 3: del(fp); break;
case 4: display(fp); break;
case 5: search(fp); break;
default: ;
}
}

fclose(fp);
return 0;
}

void insert(FILE *fp)


{

struct dir contact, blank;


fseek( fp, -sizeof(struct dir), SEEK_END );
fread(&blank, sizeof(struct dir), 1, fp);
printf("Enter individual/company name: ");
scanf("%s", contact.name);
printf("Enter telephone number: ");
scanf("%s", contact.number);
fwrite(&contact, sizeof(struct dir), 1, fp);
}

void update(FILE *fp)


{
char name[20], number[10];
int result;
struct dir contact, blank;
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strcmp(name, contact.name) == 0)
{
printf("Enter number:");
scanf("%s", number);
strcpy(contact.number, number);
fseek(fp, -sizeof(struct dir), SEEK_CUR);
fwrite(&contact, sizeof(struct dir), 1, fp);
printf("Updated successfully\n");
return;
}
}
printf("Record not found\n");
}

void del(FILE *fp)


{
char name[20], number[10];
int result, record=0;
struct dir contact, blank = {"", ""};
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strcmp(name, contact.name) == 0)
{
fseek(fp, record*sizeof(struct dir), SEEK_SET);
fwrite(&blank, sizeof(struct dir), 1, fp);
printf("%d Deleted successfully\n", record-1);

return;
}
record++;
}
printf("not found in %d records\n", record);

void display(FILE *fp)


{
struct dir contact;
int result;
rewind(fp);
printf("\n\n Telephone directory\n");
printf("%20s %10s\n", "Name", "Number");
printf("*******************************\n");
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strlen(contact.name) > 0)
printf("%20s %10s\n",contact.name, contact.number);
}
printf("*******************************\n");

void search(FILE *fp)


{
struct dir contact;
int result; char name[20];
rewind(fp);
printf("\nEnter name:");
scanf("%s", name);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strcmp(contact.name, name) == 0)
{
printf("\n%20s %10s\n",contact.name, contact.number);
return;
}
}
printf("Record not found\n");

}
OUTPUT:

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4

Telephone directory
Name Number
*******************************
bb 11111
*******************************

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 5

Enter name: bb

bb 11111

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 1
Enter individual/company name: aa
Enter telephone number: 222222

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 2
Enter name: aa
Enter number: 333333
Updated successfully

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4

Telephone directory
Name Number
*******************************
bb 11111
aa 333333
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 3
Enter name: aa
1 Deleted successfully

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4

Telephone directory
Name Number
*******************************
bb 11111
*******************************

1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 6

RESULT:
Thus the program was written & executed successfully.
EX.NO.: 10C PREPROCESSOR DIRECTIVES

AIM:
To write a C program to compute the volume for spheres of radius 5, 10 and 15 meters.
ALGORITHM:
Step 1: Start
Step 2: Define PI constant value 3.14
Step 3: Define macro function to pass a radius value as a argument
Step 4: Call the macro function
Step 5: Print the result
Step 6: Stop

PROGRAM:

#include<stdio.h>
#include<math.h>
#define PI 3.142
#define volume(r) ((4/3.0)*PI*pow(r,3))
void main()
{
int r;
float v;
scanf("%d",&r);
v=volume(r);
printf("\n volume of the sphere v=%.3f",v);
}
OUTPUT:

5,10 and 15
volume of the sphere v=523.667

RESULT:
Thus the program is written & executed successfully

You might also like