Ex no 1: Input and Output statements
Ex no: 1 a)
AIM:-
To write the C program to compute area of circle
ALGORITHM:-
Step1: Start the program
Step 2: Read the value of r
Step 3. Calculate area=pi*r*r
Step 4. Print area
Step 5. Stop the program
PROGRAM:-
#include <stdio.h>
#include <math.h>
int main()
float radius, area;
printf("Enter the radius of a circle\n");
scanf("%f", &radius);
area = 3.14159*radius*radius;
printf("Area of the circle = %.2f\n", area); // printing upto
two decimal places
return 0;
OUTPUT:-
Enter the Radius of the circle:
Radius=5
Area=78.500000
RESULT:-
Thus the C program to implement input /output statements and expressions are executed
and verified successfully.
Ex no: 1 b)
/*Program to illustrate the concept of puts() with gets() functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}
Ex no: 1 c)
Program to Find the Size of Variables
#include<stdio.h>
int main() {
int a;
float b;
double c;
char d;
// sizeof evaluates the size of a variable
printf("Size of int: %d bytes\n", sizeof(a));
printf("Size of float: %d bytes\n", sizeof(b));
printf("Size of double: %d bytes\n", sizeof(c));
printf("Size of char: %d byte\n", sizeof(d));
return 0;
Ex no: 1 d)
Program to Print ASCII Value
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
Ex no: 1 e)
C Program to swap two numbers without third
variable
#include<stdio.h>
int main()
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
Ex no: 1 f) Demonstrate Increment and Decrement Operators:-
#include <stdio.h>
#include <conio.h>
void main()
int i=5;
printf("Post Increment i=%d\n",i++);
printf("Pre Increment i=%d\n",--i);
printf("Post Decrement i=%d\n",i--);
printf("Pre Decrement i=%d\n",--i);
getch();
Output:
Post Increment i=5
Pre Increment i=5
Post Decrement i=5
Pre Decrement i=3
2. Control statements – Branching & Looping
Ex no 2 a)
Aim: To write a C program to generate Pascal's triangle.
#include <stdio.h>
void main()
{
int no_row,c=1,blk,i,j;
printf("Input number of rows: ");
scanf("%d",&no_row);
for(i=0;i<no_row;i++)
{
for(blk=1;blk<=no_row-i;blk++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
Input number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Ex no 2 b)
Aim: To write a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +,-,*,/,% and use
Switch Statement)
AIM:-
To write a C program to design a calculator to perform the operations namely, addition,
subtraction, multiplication, division and square of a number.
ALGORITHM:-
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
Step 3: Get the Number.
Step 4: Read the choice to perform the operation using switch case.
Step 5: Print the result.
Step 6: Stop.
PROGRAM:-
#include<stdio.h>
int main()
int a, b, add, subtract, multiply;
float divide;
printf("Enter two integers: \n");
scanf("%d%d", &a, &b);
add = a+b;
subtract = a-b;
multiply = a*b;
divide = a/b;
printf("\nAddition of the numbers = %d\n", add);
printf("Subtraction of 2nd number from 1st = %d\n", subtract);
printf("Multiplication of the numbers = %d\n", multiply);
printf("Dividing 1st number from 2nd = %f\n", divide);
return 0;
With using switch case:
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
int op;
clrscr();
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();
/* Design a Calculator to perform Addition, Subtraction, Multiplication,Division and Square of a
number */
OUTPUT:-
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 4
Enter your Choice : 1
Sum of 4 and 2 is : 6
RESULT:-
Thus the calculator operations are performed successfully.
Ex no 2c)
Aim:To write a C program to find the sum of individual digits of a positive integer.
AIM:-
To write a program to find sum of digits
ALGORITHM:-
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
n,r,sum->integer.
Step 3: Set sum=0
Step 4: If n>0 then
Step 5: Calculate r=n%10
Sum=sum+r
n=n/10
Step 6: Print sum
Step 7: Stop
PROGRAM:-
/*Sum of Digits*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
intn,r,sum=0;
clrscr();
printf("Enter the number:\n");
scanf("%d",&n);
while(n >0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("\nSum of digits =%d",sum);
getch();
}
OUTPUT :-
Enter the number: 92
Sum of digits=11
RESULT:-
Thus the sum of digits using C program has been executed and verified successfully
Ex no 2 d)
Aim: A Fibonacci sequence is defined as follows: the first and second terms in the sequence are
0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write
a C program to generate the first n terms of the sequence.
1. #include<stdio.h>
2.
3. main()
4. {
5. int n, first = 0, second = 1, next, c;
6.
7. printf("Enter the number of terms\n");
8. scanf("%d",&n);
9.
10. printf("First %d terms of Fibonacci series are :-\n",n);
11.
12. for ( c = 0 ; c < n ; c++ )
13. {
14. if ( c <= 1 )
15. next = c;
16. else
17. {
18. next = first + second;
19. first = second;
20. second = next;
21. }
22. printf("%d\n",next);
23. }
24.
25. return 0;
26. }
Ex no 2 e)
Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
Program or code for prime numbers between 1 to n in c
language
#include<stdio.h>
int main(){
int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}
Sample output:
Enter max range: 30
2 3 5 7 11 13 17 19 23 29
Ex no 2 f)
Write a C program to swap Numbers Using Temporary Variables.
3.Arrays
Ex no 3 a) Write a C program to search an array element using linear search.
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
getch();
return 0;
}
Ex no 3 b) Write a C program to find both the largest and smallest number in a list of
integers.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],i,n,large,small;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
getch();
return 0;
}
Ex no 3c) Write a C program that uses functions to perform the following:
i)addition of two matrices
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
ii)multiplication of two matrices
#include <stdio.h>
# include<conio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
clrscr();
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
getch();
return 0;
}
Ex no 3d) Write a C program to implement Bubble sort.
/* Bubble sort code */
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
return 0;
}
4.Strings
Write a C program that uses functions to perform the following operations:
Ex no 4 a) i) To insert a sub-string in to a given main string from a given position.
AIM: - Write a C - program to insert a sub-string in to a given main string from a given
position
Algorithm:
Step 1: Start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string(n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string(p)
Step 7: copy sub string into main string from position p - 1
Step 8: copy temporary string into main string from position p + n - 1
Step 9: print the strings
Step 10: Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str1[20], str2[20];
int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
str1[i + l2] = str1[i];
for(i = 0; i < l2; i++)
str1[n + i] = str2[i];
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
getch();
}
Input & Output:
Enter the string 1
sachin
Enter the string 2
tendulkar
Enter the position where the string is to be inserted
After inserting the string is sachtendulkarin
RESULT:-
Thus the a sub-string in to a given main string from a given position has been inserted using C
program and verified successfully
ii) To delete n Characters from a given position in a given string.
AIM: - Write a C - program to delete n Characters from a given position in a given string
Algorithm:
Step 1: Start
Step 2: read string
Step 3: find the length of the string
Step 4: read the value of number of characters to be deleted and positioned
Step 5: string copy part of string from position to end, and (position + number of characters to
end)
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i < l; i++)
str[i - n] = str[i];
str[i - n] = '\0';
printf("The string is %s", str);
getch();
Input & Output:
Enter the string
sachin
Enter the position where characters are to be deleted
Enter the number of characters to be deleted
The string is sain
RESULT:-
Thus the n Characters from a given position in a given string has been deleted using C program
and verified successfully
Ex no 4 b) Write a C program to determine if the given string is a palindrome or
not
AIM: - Write a C - program to determine if the given string is palindrome or not
Algorithm:
Step 1: start
Step 2: read string A
Step 3: copy string A into B
Step 4: reverse string B
Step 5: compare A & B
If A equals B to got step 6
Else goto step 7
Step 6: print given string A is pallindrom
Step 7: print given string is not pallindroma
Step 8: stop
Program:
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
{
char a[100], b[100];
clrscr();
printf("Enter a string to check if it's a palindrome\n");
gets(a);
strcpy(b, a); // Copying input string
strrev(b); // Reversing the string
if (strcmp(a, b) == 0) // Comparing input string with the reverse string
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
getch();
return 0;
}
Input:
Enter a string
madam
Output:
The length of the string 'madam' = 5
madam is a palindrome
RESULT:-
Thus the given string whether palindrome or not has been executed using C program and
verified successfully
5.Functions &Pointers:
Write C programs that use recursive functions
Ex no 5a) To find factorial of given number
AIM:
To find the factorial of a given number using recursive function.
ALGORITHM:
Main Program
Step 1: start
Step 2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value
Step 5: stop
Sub Program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3
Step 3: return n*fact(n-1) to main program
Program
#include <stdio.h>
int factorial(int n);
int main()
{
int n,result;
printf("Enter a positive integer: ");
scanf("%d", &n);
result=factorial(n);
printf("Factorial of %d = %d", n,result);
return 0;
}
int factorial( int n ) /*Function Definition*/
{
int temp ;
if( n == 0)
return 1 ;
else
temp = n * factorial( n-1 ) ; // Recursive Function Call
return temp ;
}
output:
Enter a positive integer: 5
Factorial of 5 = 120
RESULT:-
Thus the given number’s factorial value using C program has been executed and verified
successfully
Ex no 5b) To solve Towers of HanoiProblem.
AIM:-
To solve tower of Hanoi problem using recursion in C with Turbo C IDE.
ALGORITHM:-
1. START
2. Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest)
move disk from source to dest
Hanoi(disk - 1, aux, dest, source)
3. STOP
PROGRAM:-
#include <stdio.h>
#include<conio.h>
void hanoi(int , char , char , char );
void main()
{
int n = 4; // Number of disks
hanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
getch();
}
void hanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
hanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
hanoi(n-1, aux_rod, to_rod, from_rod);
}
OUTPUT:-
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
RESULT:-
Thus the C program to solve the towers of Hanoi problem is developed and
executed successfully using Turbo C IDE.
Ex no 5 c) To swap the variables using call by value and call by reference.
To swap the variables using call by value:
Aim:-
To write a C program to swap the variables using call by value method
Algorithm:
Main Function
Step 1: Start
Step 2: Declare the sub function
Step 3: Declare variables num1, num2
Step 4: Read values num1, num2
Step 5: Display num1, num2 before calling swap function
Step 6: call sub program as swap(num1, num2) by passing arguments using call
by value method
Step 7: Display num1, num2 after returned from swap function
Step 8: Stop
Sub Function
Step 1: initialize a, b, temp
Step 2: swap the value, a to temp, b to a, temp to b
Step 3: return to main program without passing value
Program:
#include <stdio.h>
#include<conio.h>
void swap(int,int) ;
void main()
{
int num1, num2 ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1,num2) ;
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
getch() ;
}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a=b;
b = temp ;
}
Output:
Before swap: num1 = 10, num2 = 20
After swap: num1 = 10
num2 = 20
Result:
Thus the program to swap the variables using call by value in C programming is
developed and executed successfully.
To swap the variables using Call by reference:
Aim:-
To write a C program to swap the variables using call by reference
Algorithm:
Main Function
Step 1: Start
Step 2: Declare the sub function
Step 3: Declare variables num1, num2
Step 4: Read values num1, num2
Step 5: Display num1, num2 before calling swap function
Step 6: call sub program as swap(num1, num2) by passing arguments using call
by reference method
Step 7: Display num1, num2 after returned from swap function
Step 8: Stop
Sub Function
Step 1: initialize a, b, temp
Step 2: swap the value, a to temp, b to a, temp to b
Step 3: return to main program without passing value
Program:
#include <stdio.h>
#include<conio.h>
void swap(int *,int *) ;
void main()
{
int num1, num2 ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1,&num2) ;
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
getch() ;
}
void swap(int *a, int *b)
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
output:
Before swap: num1 = 10, num2 = 20
After swap: num1 = 20
num2 = 10
Result:
Thus the program to swap the variables using call by reference in C programming
is developed and executed successfully.
6.
Ex no 6a) Generate mark sheet of students using structures.
Ex no 6b). Compute salary slip for five employees using structures and functions.
AIM:-
Write a C program to Generate Salary Slip of Employees Using Structures and Pointers.
ALGORITHM:-
1. Start the program
2. Declare a structure with structure elements to hold values of emp ID, name, basic, HRA,
DA, PF, GROSS, NET
3. Get the user input for the elements in step 2
4. Calculate the Gross salary by adding the percentage of basic salary with respect to HRA
and DA
5. Calculate the net salary by deducting the percentage of PF from the basic salary
6. Print the employee salary slip details
7. Stop the program
PROGRAM CODING:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct employee {
int empId;
char name[32];
int basic, hra, da;
int pf;
float gross, net;
};
void printSalary(struct employee e1)
{
printf("Salary Slip of %s:\n", e1.name);
printf("Employee ID: %d\n", e1.empId);
printf("Basic Salary: %d\n", e1.basic);
printf("House Rent Allowance: %d\n", e1.hra);
printf("daily allowance:%d",e1.da);
printf("\nProvident Fund : %d\n", e1.pf);
printf("Gross Salary: %.2f Rupees\n", e1.gross);
printf("\nNet Salary: %.2f Rupees\n\n", e1.net);
return;
}
void main()
{
int i, ch, num, flag, empID;
struct employee *e1;
clrscr();
printf("Enter the number of employees:");
scanf("%d", &num);
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
for (i = 0; i < num; i++)
{
printf("Employee ID:");
scanf("%d", &(e1[i].empId));
printf("Employee Name:");
scanf("%s",e1[i]);
e1[i].name[strlen(e1[i].name) - 1] = '\0';
printf("Basic Salary");
scanf("%d", &(e1[i].basic));
printf("House Rent Allowance in %");
scanf("%d", &(e1[i].hra));
printf("Daily Allowance in %");
scanf("%d", &(e1[i].da));
printf("Provident Fund in %:");
scanf("%d",&e1[i].pf);
printf("\n");
}
for (i = 0; i < num; i++)
{
e1[i].gross = e1[i].basic + ((e1[i].hra * e1[i].basic) / 100) + ((e1[i].da *
e1[i].basic) / 100 );
e1[i].net = e1[i].gross-(e1[i].basic*e1[i].pf/100) ;
}
while (1)
{
printf("Enter employee ID to get payslip:");
scanf("%d", &empID);
flag = 0;
for (i = 0; i < num; i++)
{
if (empID == e1[i].empId)
{
printSalary(e1[i]);
flag = 1;
}
}
if (!flag)
{
printf("No Record Found!!\n");
}
printf("Do You Want To Continue(1/0):");
scanf("%d", &ch);
if (!ch)
{
break;
}
}
getch();
}
OUTPUT:-
Enter the number of employees : 1
Employee ID : 123
Employee name : bala
Basic salary : 10000
House rent allowance in % : 80
Daily allowance in % : 20
Provident fund in % : 14
Enter employee ID to get pay slip : 123
Salary slip of employee ID : 123
Basic salary : 10000
House rent allowance : 80
Daily allowance : 20
Provident fund : 14
Gross salary : 10168.00 Rupees
Net salary : 10079.00 Rupees
Do you want to continue(1/0) : 0
RESULT:-
Thus the program to generate the salary slip of the employees using structure and pointers
using C programming is developed and executed successfully.
7.
Ex no 7) Insert, Update, delete and append telephone details of an individual or a company into
a telephone directory using random access file
Ex no 8)CBS