You are on page 1of 22

C PROGRAMMING LAB

C PROGRAMMING LAB

PART – A
1. Program to read radius of a circle and to find area and circumference
2. Program to read three numbers and find the biggest of three
3. Program to demonstrate library functions in math.h
4. Program to check for prime
5. Program to generate n primes
6. Program to read a number, find the sum of the digits, reverse the number and check it for
palindrome
7. Program to read numbers from keyboard continuously till the user presses 999 and to find
the sum of only positive numbers
8. Program to read percentage of marks and to display appropriate message (Demonstration
of else-if ladder)
9. Program to find the roots of quadratic equation (demonstration of switch Case statement)
10. Program to read marks scored by n students and find the average of marks
(Demonstration of single dimensional array)
11. Program to remove Duplicate Element in a single dimensional Array
12. Program to perform addition and subtraction of Matrices

PART – B
13. Program to find the length of a string without using built in function
14. Program to demonstrate string functions
15. Program to demonstrate pointers in C
16. Program to check a number for prime by defining isprime() function
17. Program to read, display and to find the trace of a square matrix
18. Program to read, display and add two m x n matrices using functions
19. Program to read, display and multiply two m x n matrices using functions
20. Program to read a string and to find the number of alphabets, digits, vowels, consonants,
spaces and special characters
21. Program to Reverse a String using Pointer
22. Program to Swap Two Numbers using Pointers
23. Program to demonstrate student structure to read & display records of n students
24. Program to demonstrate the difference between structure & union

1
C PROGRAMMING LAB

Exercise: 1
Date:

Write C program to read radius of a circle and to find area and circumference.

AIM
To write a C program to read radius of a circle and to find area and circumference.
The basic formula for area of a circle and circumference of circle.
Area of Circle = PI * R * R
Circumference of Circle = 2 * PI * R

ALOGRITHM
Step 1: Start the process.
Step 2: Input the radius of the circle either from the user or specify it.
Step 3: Calculate the area of circle and circumference with the formula specified along with
radius defined.
Step 4: Display the result.
Step 5: Stop the process.

PROGRAM
// A C Program to read radius of a circle and find area and circumference of it
#define PI 3.1416
#include<stdio.h>
#include<conio.h>
void main()
{
float radius, area, circumference;
clrscr();
printf("Enter the radius of a circle:\n");
scanf("%f",&radius);
area=PI * radius * radius;
circumference=2.0 * PI * radius;
printf("Area of Circle is %f\n",area);
printf("Circumference of Circle is %f\n",circumference);
getch();
}

2
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

3
C PROGRAMMING LAB

Exercise: 2
Date:

Write a C program to read three numbers and find the biggest of three.

AIM
To write a C program to read three numbers and find the biggest of three.

ALOGRITHM
Step 1: Start the process
Step 2: Input the three numbers.
Step 3: Compare the three numbers using if … else if … else statement.
Step 4: Display the biggest as result.
Step 5: Stop the process

PROGRAM
// A C Program to read three numbers and the find the biggest of three
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Biggest is %d\n",a);
else if (b>c)
printf("Biggest is %d\n",b);
else
printf("Biggest is %d\n",c);
getch();
}

4
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

5
C PROGRAMMING LAB

Exercise: 3
Date:

Write a C program to demonstrate library functions in math.h.

AIM
To write a C program to demonstrate library functions in math.h.

ALOGRITHM
Step 1: Start the process.
Step 2: Apply mathematical function contained in math.h.
Step 3: Display the result.
Step 4: Stop the process

PROGRAM
//A C Program to demonstrate library functions in math.h
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
clrscr();
printf("\nResult of ceil(3.6) is %f",ceil(3.6));
printf("\nResult of ceil(3.3) is %f",ceil(3.3));
printf("\nResult of floor(3.6) is %f",floor(3.6));
printf("\nResult of floor(3.2) is %f",floor(3.2));
printf("\nResult of sqrt(16) is %f",sqrt(16));
printf("\nResult of sqrt(7) is %f",sqrt(7));
printf("\nResult of pow(2,4) is %f",pow(2,4));
printf("\nResult of pow(3,3) is %f",pow(3,3));
printf("\nResult of abs(-12) is %d",abs(-12));
getch();
return 0;
}

6
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

7
C PROGRAMMING LAB

Exercise: 4
Date:

Write a C program to check for prime.

AIM
To write a C program to check for prime.

ALOGRITHM
Step 1: Start the process.
Step 2: Input the number.
Step 3: Check whether the number is prime or not using for and if statements.
Step 4: Display the result.
Step 5: Stop the process

PROGRAM
// A C Program to check whether the given number is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,f=0;
clrscr();
printf("Enter the number:\n");
scanf("%d",&num);
for(i=2;i<num;i++)
if((num%i)==0) f=1;
if(f==0)
printf("%d is a prime number\n",num);
else
printf("%d is not a prime number\n",num);
getch();
}

8
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

9
C PROGRAMMING LAB

Exercise: 5
Date:

Write a C program to generate n primes.

AIM
To write a C program to generate n primes.

ALOGRITHM
Step 1: Start the process.
Step 2: Input the number of prime numbers to be generated.
Step 3: Check whether the number is prime or not using for and if statements.
Step 4: Display the number if it is prime.
Step 5: Repeat the Step 3 thru Step 4 until the number of prime number to be reached.
Step 6: Stop the process

PROGRAM
// A C Program to generate a set of prime numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j=1,count=0,f;
clrscr();
printf("Enter how many prime numbers?\n");
scanf("%d",&n);
printf("The %d prime numbers are as follows:\n",n);
while(count<n)
{
f=0;
for(i=2;i<j;i++)
if((j%i)==0) f=1;
if(f==0)
{
printf("%d\n",j);
count++;
}
j++;
}
getch();
}

10
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

11
C PROGRAMMING LAB

Exercise: 6
Date:

Write a C program to read a number, find the sum of the digits, reverse the number and check it
for palindrome.

AIM
To write a C program to read a number, find the sum of the digits, reverse the number and check
it for palindrome.

ALOGRITHM
Step 1: Start the process.
Step 2: Input the number.
Step 3: Find the sum of digits and reverse of that number using for and if statements.
Step 4: Check whether the number is palindrome or not.
Step 5: Display the results.
Step 6: Stop the process

PROGRAM
// Program to read a number, find the sum of the digits, reverse the number
// and check it for palindrome without using functions
#include<stdio.h>
#include<conio.h>
void main()
{
int num,n,rem,rev=0,sum=0;
clrscr();
printf("Enter an integer number: ");
scanf("%d", &n);
num=n;
while (n!=0)
{
rem = n % 10;
sum = sum + rem;
rev = rev * 10 + rem;
n = n / 10;
}
printf("\nSum of digits = %d",sum);
printf("\nReversed number is = %d", rev);
if(num==rev)
printf("\nThe number is palindrome");
else
printf("\nThe number is not palindrome");
getch();
}

12
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

13
C PROGRAMMING LAB

Exercise: 7
Date:

Write a C program to read numbers from keyboard continuously till the user presses 999 and to
find the sum of only positive numbers.

AIM
To write a C program to read numbers from keyboard continuously till the user presses 999 and
to find the sum of only positive numbers.

ALOGRITHM
Step 1: Start the process
Step 2: sum=0
Step 3: Input the number
Step 4: Check if it is equal to 999 and then go to Step 7. Otherwise, check if it is positive and
then add that number into sum
Step 5: Repeat the Step 3 thru Step 4
Step 6: Display the result.
Step 7: Stop the process

PROGRAM
// A C Program to read numbers from keyboard continuously till the user presses 999
// and to find the sum of only positive numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
int sum = 0;
clrscr();
printf("Enter the numbers (enter 999 to quit):\n");
scanf("%d",&num);
while(num != 999)
{
if (num>0) sum = sum + num;
scanf("%d",&num);
}
printf("\nThe sum of all positive numbers = %d\n",sum);
getch();
}

14
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

15
C PROGRAMMING LAB

Exercise: 8
Date:

Write a C program to read percentage of marks and to display appropriate message


(Demonstration of else-if ladder).

AIM
To write a C program to read percentage of marks and to display appropriate message
(Demonstration of else-if ladder).

ALOGRITHM
Step 1: Start the process
Step 2: Input the percentage of marks
Step 3: If percentage of marks>75, then display “You have secured FIRST CLASS WITH
DISTINCTION"
Step 4: If percentage of marks<75 && percentage of marks>=60, then display “You have
secured FIRST CLASS"
Step 5: If percentage of marks<60 && percentage of marks>=50, then display “You have
secured SECOND CLASS"
Step 6: If percentage of marks<50, then display “You have secured THIRD CLASS"
Step 7: Stop the process

PROGRAM
// A C Program to read percentage of marks and to display appropriate message
// (Demonstration of else-if ladder)
#include<stdio.h>
#include<conio.h>
void main()
{
float percentage;
clrscr();
printf("Enter your percentage of marks:\n");
scanf("%f",&percentage);
if (percentage>75)
printf("You have secured FIRST CLASS WITH DISTINCTION");
else if (percentage<75 && percentage>=60)
printf("You have secured FIRST CLASS");
else if (percentage<60 && percentage>=50)
printf("You have secured SECOND CLASS");
else
printf("You have secured THIRD CLASS");
getch();
}

16
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

17
C PROGRAMMING LAB

Exercise: 9
Date:

Write a C Program to find the roots of quadratic equation (demonstration of switch Case
statement)

AIM
To write a C program to find the roots of quadratic equation (demonstration of switch Case
statement)

ALOGRITHM
Step 1: Start the process
Step 2: Input a,b,c
Step 3: Calculate discriminant
Step 4: If discriminant>0, then calculate the two distinct and real roots and display the result
Step 5: If discriminant==0, then calculate the two equal and real roots and display the result
Step 6: If discriminant<0, then calculate the two distinct complex roots and display the result
Step 7: Stop the process

PROGRAM
// C program to find all roots of a quadratic equation using switch case
#include <stdio.h>
#include <math.h> // Used for sqrt()
#include <conio.h>
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
clrscr();

printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");


scanf("%f%f%f", &a, &b, &c);

// Calculate discriminant
discriminant = (b * b) - (4 * a * c);

// Compute roots of quadratic equation based on the nature of discriminant


switch(discriminant > 0)
{
case 1:
/* If discriminant is positive */
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);

18
C PROGRAMMING LAB

printf("Two distinct and real roots exists: %.2f and %.2f",


root1, root2);
break;

case 0:
// If discriminant is not positive
switch(discriminant < 0)
{
case 1:
// If discriminant is negative
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);

printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
break;

case 0:
// If discriminant is zero
root1 = root2 = -b / (2 * a);

printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);

break;
}
}
getch();
return 0;
}

19
C PROGRAMMING LAB

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

20
C PROGRAMMING LAB

Exercise: 10
Date:

Write a C Program to read marks scored by n students and find the average of marks
(Demonstration of single dimensional array)

AIM
To write a C Program to read marks scored by n students and find the average of marks
(Demonstration of single dimensional array)

ALOGRITHM
Step 1: Start the process
Step 2: Input the number of students
Step 3: Input three subject marks of each student
Step 4: Compute the average and result of each student
Step 5: Display the results
Step 6: Stop the process

PROGRAM
// A C Program to read marks scored by n students and find the average of marks
// (Demonstration of single dimensional array)
#include<stdio.h>
#include<conio.h>
void main()
{
int n, a[25], i, total[20],m1[20],m2[20],m3[20];
float average[20];
clrscr();
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the marks of 3 subjects of student %d: ",i+1);
scanf("%d %d %d",&m1[i],&m2[i],&m3[i]);
}
for(i=0;i<3;i++)
{
total[i]=m1[i]+m2[i]+m3[i];
average[i]=(float)total[i]/3;
}
for(i=0;i<n;i++)
{
printf("\nAverage/percentage marks of student %d is: %f",i+1,average[i]);
if(average[i]>=35)
printf("\t PASS\n");

21
C PROGRAMMING LAB

else
printf("\t FAIL\n");
}
getch();
}

INPUT/OUTPUT

RESULT
Thus the above program has been executed and its output verified successfully using Turbo C
IDE and Command prompt.

22

You might also like