C Practical File
C Practical File
C Practical File
Index
1. Program to find the sum of two numbers
2. Program to find area and circumference of circle
3. Program to find the simple interest
4. Program to convert temperature from degree centigrade to Fahrenheit
5. Program to calculate sum of 5 subjects and find percentage
6. Program to swap two numbers with third variable
7. Program to swap two numbers without third variable
8. Program to Check Number is Even or Odd
9. Program to Check Leap Year
10. Program to Find the Greatest of three Numbers
11. Program to display Monday to Sunday using Switch Statement
12. Program for Arithmetic Operations using Switch Statement
13. Program to use conditional operator
14. Program to show Bitwise AND Operator between two integers
15. Program to shift the input data by two bits to left
16. Program to print sum and average of First n Natural Numbers
17. Program to find Factorial using loop
18. Program to print first n elements of Fibonacci Series
19. Program to Print Multiplication Table of number
20. Program to Reverse an Integer
21. Program to check Armstrong Number
22. Program to print star Sequence 1
23. Program to print star Sequence 2
24. Program to print star Sequence 3
25. Program to Check Whether Number is Prime or not
26. Program to display an Array
27. Program to find the Maximum Number in an Array
28. Program to print sum & average of 10 elements of an Array
29. Program to Display Matrix
30. Program to find sum of two Matrices
31. Program to find transpose of a Matrix
32. Program to find factorial of a number using Functions
33. Program to find multiplication of a two Matrix
34. Program to swap two numbers using Functions
35. Program to find square of a number using Functions
36. Program to show Call by Value
37. Program to show call by Reference
38. Program to find factorial of a number using recursion
39. Program to show input and output of a string
40. Program to find whether a string is Palindrome or not
41. Program to print the roll number, name, age and marks of a student using
structures
42. Program to add two numbers using Pointers
43. Program to subtract two number using pointers
44. Program to search an element in the array using Linear Search
45. Program to implement Linked List
1. AIM: Program to find the sum of two numbers.
Code:
#include<stdio.h>
int main() {
int a, b, sum;
sum = a + b;
return(0);
}
Output:
#include<stdio.h>
int main() {
int rad;
float PI = 3.14, area, ci;
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}
Output:
Enter radius of a circle : 1
Area of circle : 3.14
Circumference : 6.28
#include<stdio.h>
int main() {
int amount, rate, time, si;
return(0);
}
Output:
#include<stdio.h>
int main() {
float celsius, fahrenheit;
Output:
#include<stdio.h>
int main() {
int s1, s2, s3, s4, s5, sum, total = 500;
float per;
sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d", sum);
return (0);
}
Output:
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %.2lf\n", first);
printf("After swapping, secondNumber = %.2lf", second);
return 0;
}
Output:
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Output:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Output:
Enter an integer: -7
-7 is odd.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output:
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter a no.: ");
scanf("%d",&num);
switch(num)
{
case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
case 3: printf("Wednesday");
break;
case 4: printf("Thursday");
break;
case 5: printf("Friday");
break;
case 6: printf("Saturday");
break;
case 7: printf("Sunday");
break;
default: printf("You have entered a wrong choice.");
break;
}
return 0;
}
Output:
Enter a no.: 1
Monday
#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;
}
return 0;
}
Output:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, Greatest;
printf("Enter two numbers: ");
scanf("%d", &a);
scanf("%d", &b);
Greatest = (a >= b) ? a : b;
printf("Greatest = %d", Greatest);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, bitwiseAND;
printf("Enter two numbers: ");
scanf("%d", &a);
scanf("%d", &b);
bitwiseAND = a&b;
printf("Bitwise AND = %d", bitwiseAND);
return 0;
}
Output:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
printf("Data Shifted by 2 bits to left = %d", a << 2);
return 0;
}
Output:
Enter a number: 2
Data Shifted by 2 bits to left = 8
Code:
#include<stdio.h>
int main() {
int i = 1, n, sum=0;
printf("Enter Number: ");
scanf("%d", &n);
return (0);
}
Output:
Enter Number: 3
SUM = 6
AVG = 2
17. AIM: Program to find Factorial using loop.
Code:
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is: 120
Code:
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Output:
Code:
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
Output:
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Code:
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
Output:
Enter an integer: 2345
Reversed number = 5432
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number: ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1; j<=i ; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
for(i=1; i<=5 ; i++)
{
for(j=5; j>=i ; j--)
{
printf(" ");
}
for(k=1; k<=i ; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=5; j>=i ; j--)
printf(" ");
{
for(k=1; k<=i*2-1; k++)
printf("*");
}
printf("\n");
}
return 0;
}
Output:
*
***
*****
*******
*********
#include<stdio.h>
int main() {
int num, i, count = 0;
if (count == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
return 0;
}
Output:
Enter a number: 12
12 is not a prime number
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i;
printf("enter elements of an array: ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<5;i++)
{
printf("Elements = %d",a[i]);
}
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],max,i;
Output:
#include<stdio.h>
#include<conio.h>
int main() {
int a[10],i,sum=0;
float av;
printf("enter elements of an array: ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++) {
sum=sum+a[i];
}
printf("sum=%d",sum);
av=sum/10;
printf("average=%.2f",av);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][2],b[3][2],i,j;
printf("enter value for a matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
Output:
a matrix is
7 8
9 4
5 6
b matrix is
3 2
1 4
5 6
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][2], b[3][2] ,c[3][2] ,i , j;
printf("Enter value for 1 matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("Sum of matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Sum of matrixis
5 7
9 5
8 8
int main()
{
int a[3][2], b[2][3], i, j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(‘%d",&a[i][j]);
}
printf("Matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
b[j][i]=a[i][j];
}
printf("Transpose matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf(" %d ",b[i][j]);
printf("\n");
}
return 0;
}
Output:
Transpose matrix:
4 6 2
5 1 3
#include<stdio.h>
#include<conio.h>
int main()
{
int a,f;
int fact(int);
printf("enter a no: ");
scanf("%d",&a);
f=fact(a);
printf("factorial= %d",f);
return 0;
}
int fact(int x)
{
int fac=1,i; for(i=x;i>=1;i--)
{
fac=fac*i;
}
return fac;
}
Output:
enter a no: 5
factorial=120
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
void swap(int,int);
int a,b,r;
Output:
entervaluefora&b:4 5
after swapping the value for a & b : 5 4
#include<stdio.h>
#include<conio.h>
int main() {
int square(int);
int r,a;
printf("enter any no: ");
scanf("%d",&a);
r=square(a);
printf("square is : %d",r);
return 0;
}
int square(int x)
{
return x*x ;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,swap();
a=5;
b=10;
printf("value of a=%d & value of b=%d before swap ",a,b);
swap(a,b);
printf("\nvalue of a =%d & b=%d after swap",a,b);
}
int swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
Output:
int main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf("value of a= %d & value of b=%d before swap",a,b);
swap(aa,bb);
printf("\nvalue of a=%d & b=%d after swap",a,b);
return 0;
}
int swap(int *x, int *y)
{
int temp;
temp=*x; *x=*y;
*y=temp; }
Output:
#include<stdio.h>
#include<conio.h>
int main() {
int n;
printf("enter number: ");
scanf("%d",&n);
if(n<0)
printf("invalid number");
else
printf("%d!=%d",n,fact(n));
return 0;
}
int fact(int x) {
if(x==0)
return 1;
else
return (x*fact(x-1));
}
Output:
enter number: 5
5!=120
#include<stdio.h>
#include<conio.h>
int main()
{
char a[50];
printf("enter any string: ");
gets(a);
puts(a);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
char s1[20],s2[20];
printf("enter a string: ");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf("string is a palindrome");
else
printf("not a palindrome string");
return 0;
}
Output:
41. AIM: Program to print the roll number, name, age and
marks of a student using structures.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
struct student
{
int roll_no;
char name[30];
int age;
int marks;
};
struct student p1 = {1,"Brown",14,78};
printf("%d\n %s\n %d\n %d",p1.roll_no,p1.name,p1.age,p1.marks);
return 0;
}
Output:
1
Brown
14
78
#include<stdio.h>
#include<conio.h>
int main()
{
int *p1,*p2,sum;
printf("enter two no’s: ");
scanf("%d%d",&*p1,&*p2);
sum=*p1+*p2;
printf("sum=%d",sum);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int *p1,*p2,c;
printf("enter two no’s: ");
scanf("%d%d",&*p1,&*p2);
c=*p1-*p2;
printf("sum=%d" ,c);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10], i, item, flag=0;
if(flag==0)
printf("Element Not Found");
else
printf("Element Found at Position =%d" ,i);
return 0;
}
Output:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node {
int info;
struct node *next;
};
int main() {
int ch=0,i=0,cnt;
while(1) {
printf("***********menu******** ****");
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.count");
printf("\n5.exit");
scanf("%d", &ch);
switch(ch) {
case 1: ins(); break;
case 2: del(); break;
case 3: display(); break;
case 4: cnt=count();
printf("\n the no of nodes : %d\n", cnt); break;
case 5: exit(1);
} }
}
void ins()
{
int j=0,ch1=0;
printf("\n enter your choice");
printf("\n1.insert at the beggning");
printf("\n2.insert at the middle");
printf("\n3.insert at the end");
scanf ("%d",&ch1);
switch(ch1)
{
case 1: ins_at_beg();break;
case 2: ins_at_mid(); break;
case 3: ins_at_end();
}
}
void ins_at_beg() {
int info;
struct node *t=(struct node *)malloc(sizeof(struct node));
printf("\n enter information to be inserted in the beginning");
scanf("%d", &info);
t->info=info;
t->next=start;
start=t;
}
void ins_at_mid() {
int inform, x, i;
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *p=start;
printf("\nenter the location after which new node to be added");
scanf("%d", &x);
for(i=1;i<x;i++)
p=p->next;
printf("\nenter information of the new node");
scanf("%d", &inform);
t->info=inform;
t->next=p->next;
p->next=t;
}
void ins_at_end()
{
int inform1;
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *p=start;
printf("\n enter information to be added"); scanf("%d",&inform1);
t->info=inform1; while(p->next!=NULL) p=p->next;
p->next=t; t->next=NULL;
}
void del()
{
int k=0,ch2=0;
printf("\n enter your choice"); printf("\n1.delete at the beginning");
printf("\n2.delete at the middle"); printf("\n3.delete at the end");
scanf ("%d",&ch2);
switch(ch2)
{
case 1:del_at_beg();
break;
case 2:del_at_mid();
break;
case 3:del_at_end();
break;
}
}
void del_at_beg()
{
struct node *t=start; start=start->next; free(t);
}
void del_at_mid()
{
int n;
struct node *cur=start;
struct node *pre=start;
printf("\nenter information to be deleted");
scanf("%d",&n);
while(cur->info!=n)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next; free(cur);
}
void del_at_end()
{
struct node *cur=start;
struct node *pre=start;
while(cur->next!=NULL) {
pre=cur;
cur=cur->next;
}
pre->next=NULL;
free(cur);
}
void display()
{
struct node *p=start;
printf("\n\n***************LINK LIST*****************\n\n");
while(p!=NULL)
{
printf("%d\n",p->info); p=p->next;
}
}
int count()
{
int c=0;
struct node *q=start;
while(q!=NULL)
{
q=q->next; c=c+1;
}
return c;
}
Output:
***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice : 1
***************LINK LIST*****************
12
***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice : 1
***************LINK LIST*****************
12
12
***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice :5