0% found this document useful (0 votes)
6 views45 pages

C Program-New Lab Manual

The document is a C programming lab manual containing various programs that demonstrate fundamental programming concepts. It includes programs for calculating the area and circumference of a circle, finding the largest of three numbers, checking for prime numbers, reversing a number, and more. Each program is accompanied by example inputs and outputs to illustrate functionality.

Uploaded by

jerushdavid01
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)
6 views45 pages

C Program-New Lab Manual

The document is a C programming lab manual containing various programs that demonstrate fundamental programming concepts. It includes programs for calculating the area and circumference of a circle, finding the largest of three numbers, checking for prime numbers, reversing a number, and more. Each program is accompanied by example inputs and outputs to illustrate functionality.

Uploaded by

jerushdavid01
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

C PROGRAMMING LAB MANUAL

1. Program to read radius and find area and circumference of circle .

#include<stdio.h>

int main()

int r;

float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");

scanf("%d", &r);

area = PI * r * r;

ci = 2 * PI * r;

printf("\nArea of circle : %f ", area);

printf("\nCircumference : %f ", ci);

return (0);

Input:

Enter radius of a circle

Output:

Area of Circle : 78.5

Circumfrance :31.4
2. Program to read three numbers and find the biggest of three.

#include <stdio.h>

int main()

int num1, num2, num3;

printf(" Enter the number1 = ");

scanf("%d", &num1);

printf("\n Enter the number2 = ");

scanf("%d", &num2);

printf("\n Enter the number3 = ");

scanf("%d", &num3);

if (num1 > num2)

if (num1 > num3)

printf("\n Largest number = %d \n",num1);

else

printf("\n Largest number = %d \n",num3);

}
else if (num2 > num3)

printf("\n Largest number = %d \n",num2);

else

printf("\n Largest number = %d \n",num3);

return 0;

Input:

Enter the number1 = 10

Enter the number2 = 5

Enter the number3 = 2

Output:

Largest number = 10
3. Program to check whether the number is prime or not

#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {

// condition for non-prime

if (n % i == 0) {

flag = 1;

break;

if (n == 1) {

printf("1 is neither prime nor composite.");

else {

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

}
return 0;

Input:

Enter a positive integer: 19

Output:

19 is a prime number

4 . Program to read a number, find the sum of the digits, reverse the number
and check it for palindrome.

#include <stdio.h>

void main()

int num, temp, remainder, reverse = 0;

printf("Enter an integer \n");

scanf("%d", &num);

while(num>0)

remainder =num%10 ;

Temp = temp+rem ;

num=num/10 #return integer part of the result

print("Sum is :",temp)
/* original number is stored at temp */

temp = num;

while (num > 0)

remainder = num % 10;

reverse = reverse * 10 + remainder;

num /= 10;

printf("Given number is = %d\n", temp);

printf("Its reverse is = %d\n", reverse);

if (temp == reverse)

printf(" %d is a palindrome \n",reverse);

else

printf("%d is not a palindrome \n",reverse);

Input:

Enter an integer 1221

Output:

Given number is : 1221

Its revers is : 1221

1221 is a palindrome
5. 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>

int main()

int i, num, count_p=0 ;

int arr[100];

//size of array

printf("Enter Number of elements: ");

scanf("%d", &num);

//take input from user for "num" numbers

Printf(“enter elements\n”);

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

scanf("%d", &arr[i]);

//count the numbers

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

{
//check for positive numbers

if(arr[i]>0)

count_p++;

else

printf("Wrong Entry");

break;

printf("Positive Numbers: %d\n", count_p);

Input:

Enter Number of elements : 5

Enter elements

16

Output: Positive Numbers: 5


6 . Program to read percentage of marks and to display appropriate message
(Demonstration of else-if ladder)

#include<stdio.h>

#include<conio.h>

void main()

int s1,s2,s3,s4,s5,t,p;

clrscr();

printf("\n Enter marks of 5 subjects each out of 100 ");

printf("\n\n ********************************************");

printf("\n\n Sub1 = ");

scanf("%d",&s1);

printf("\n Sub2 = ");

scanf("%d",&s2);

printf("\n Sub3 = ");

scanf("%d",&s3);

printf("\n Sub4 = ");

scanf("%d",&s4);

printf("\n Sub5 = ");

scanf("%d",&s5);

printf("\n ********************************************");

t=s1+s2+s3+s4+s5; //Total
printf("\n Total Marks = %d/500",t);

p=t/5; //Percentage

printf("\n\n Percentage = %d%",p);

printf("\n ********************************************");

//////////// Ladder If Statement ////////////

if(p>=80)

printf("\n\n Your Grade : A+");

else if(p>=75)

printf("\n\n Your Grade : A");

else if(p>=60)

printf("\n\n Your Grade : B");

else if(p>=45)

printf("\n\n Your Grade : C");

else if(p>=35)

printf("\n\n Your grade : D");

else

printf("\n\n You Are Fail");

//////// Ladder If Statement /////////////

getch();

}
Input:

Enter marks of 5 subjects each out of 100:

Sub1: 65

Sub2: 75

Sub3 :85

Sub4: 95

Sub5:85

Output:

Total : 405

Percentage : 81

Your grade : A+
7 . Program To find the roots of quadratic equation.

#include <math.h>

#include <stdio.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imagPart;

printf("Enter coefficients a, b and c: ");

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

discriminant = b * b - 4 * a * c;

// condition for real and different roots

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

// condition for real and equal roots

else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("root1 = root2 = %.2lf;", root1);

// if roots are not real

else {

realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);

printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart,


realPart, imagPart);

return 0;

Output:

Enter coefficients a, b and c: 2.3

5.6

root1 = -0.87+1.30i and root2 = -0.87-1.30i

8 . to read marks scored by n students and find the average of marks


(Demonstration of single dimensional array)

#include <stdio.h>
int main() {

int n, i;

float num[100], sum = 0.0, avg;

printf("Enter the numbers of elements: ");

scanf("%d", &n);

while (n > 100 || n < 1) {

printf("Error! number should in range of (1 to 100).\n");

printf("Enter the number again: ");

scanf("%d", &n);

printf("%d. Enter the elements: ", i + 1);

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

scanf("%f", &num[i]);

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

sum += num[i];

avg = sum / n;

printf("Average = %.2f", avg);

return 0;
}

Input:

Enter number of elements: 5

Enter the elements:

60

70

80

90

100

Output:

Sum = 400

Average= 80.00

9 . Program to remove Duplicate Element in a single dimensional Array

#include <stdio.h>
int remove_duplicate(int arr[], int n)

if (n == 0 || n == 1)

return n;

int temp[n];

int j = 0;

int i;

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

if (arr[i] != arr[i + 1])

temp[j++] = arr[i];

temp[j++] = arr[n - 1];

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

arr[i] = temp[i];

return j;

int main()

int i,n;

int arr[n];

printf(“enter number of elements\n”)

scanf("%d", &n);
for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("\nArray Before Removing Duplicates: ");

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

printf("%d ", arr[i]);

n = remove_duplicate(arr, n);

printf("\nArray After Removing Duplicates: ");

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

printf("%d ", arr[i]);

return 0;

Input:

Enter the number of elements:

5
Enter array elements

16

04

76

04

45

Array Before Removing Duplicates:

16 04 76 04 45

Array After Removing Duplicates:

16 04 76 45

10 . Program to perform addition and subtraction of Matrices

#include<stdio.h>

int main()
{

printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

int n, m, i,j, first[10][10], second[10][10], sum[10][10], diff[10][10];

printf("\nEnter the number of rows and columns of the first matrix \n\n");

scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);

for(i = 0; i < m; i++) // to iterate the rows

for(j = 0; j < n; j++) // to iterate the columns

scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);

for(i = 0; i < m; i++) // to iterate the rows

for(j = 0; j < n; j++) // to iterate the columns

scanf("%d", &second[c][d]);

/*

printing the first matrix

*/

printf("\n\nThe first matrix is: \n\n");

for(i = 0; i < m; i++) // to iterate the rows

for(j = 0; j < n; j++) // to iterate the columns

printf("%d\t", first[c][d]);
}

printf("\n");

/*

printing the second matrix

*/

printf("\n\nThe second matrix is: \n\n");

for(i = 0; i < m; i++) // to iterate the rows

for(j = 0; j < n; j++) // to iterate the columns

printf("%d\t", second[c][d]);

printf("\n");

/*

finding the SUM of the two matrices

and storing in another matrix sum of the same size

*/

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

for(j = 0; j < n; j++) sum[c][d] = first[c][d] + second[c][d];

// printing the elements of the sum matrix


printf("\n\nThe sum of the two entered matrices is: \n\n");

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

for(j = 0; j < n; j++) {

printf("%d\t", sum[c][d]);

printf("\n");

/*

finding the DIFFERENCE of the two matrices

and storing in another matrix difference of the same size

*/

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

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

diff[c][d] = first[c][d] - second[c][d];

// printing the elements of the diff matrix

printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");

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


{

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

printf("%d\t", diff[c][d]);

printf("\n");

return 0;

Input:

Enter the order of the matrix:

3
3

Enter First Matrix elements:

123456789

Enter Second Matrix elements:

123456789

The sum of the two entered matrices is:

2 4 6

8 10 12

14 16 18

The difference(subtraction) of the two entered matrices is:

000

000

000

11 . Program to find Factorial of the given number

#include<stdio.h>

int main(){
int i,num;

long f=1;

printf("Enter a number: ");

scanf("%d",&num);

for(i=1;i<=num;i++)

f=f*i;

printf("Factorial of %d is: %d",num,f);

return 0;

Input:

Enter a number:

Factorial of 6 is : 720

12 . Program To generate Fibonacci series

#include <stdio.h>

int main() {
int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1,t3;

printf(“Enter n value\n”)

scan(%d”,&n)

printf(“%d\t”,t1);

while(t2<=n)

printf(“%d\t”,t2);

t3=t1+t2;

t1=t2;

t2=t3;

return 0;

Input:

Enter n value: 10
Output:

0 1 1 2 3 5 8

14 . Program to find the length of a string without using built in function

#include <stdio.h>

void main()
{

char string[100];

int i, length = 0;

printf("Enter a string \n");

Scanf( "%s ", string);

/* keep going through each character of the string till its end */

for (i = 0; string[i] != '\0'; i++)

length++;

printf(" the length of the string %s is(Number of Characters Present in the string)
= %d\n", string, length);

Input: Enter a string

Sreenivas

Output:

the length of the string sreenivas is(Number of Characters Present in the string)

15 . Program To demonstrate string functions.

#include<stdio.h>

#include<conio.h>
Void main(){

Char string1[25],string2[25];

int l;

Clrscr();

Printf(“***** performing string length ******\n”);

Printf(“enter only one string \n”);

Scanf(“%s”,string1);

l = strlen(string1);

printf(“the string length is %d\n\n”,l);

printf(“**** performing string concatenation ****\n”);

printf(“enter two strings\n”);

scanf(“%s%s”,string1,string2);

printf(“the concatenated string is %s\n\n”,strcat(string1,string2));

printf(“***** performing string compare *****\n”);

printf(“enter two strings \n”);

scanf(“%s%s”,string1,string2);

if(strcmp(string1,string2) = = 0)

printf(“strings are equal\n”);

else

printf(“strings are not equal\n”);

printf(“*** performing string copy ****\n”);


printf(“enter a strings\n”);

scanf(“%s”,string1);

printf(“the Original string is %s \n”,string1);

strcpy(string1,string2);

printf(“the Copied string is %s\n”,string2);

getch();

Output:

enter only one string

Sreeni

the string length is: 6

**** performing string concatenation ****

Enter two strings

Sree

nivas

the concatenated string is : Sreenivas

***** performing string compare *****

Enter two strings

Sree

SREE

Strings are not equal


*** performing string copy ****

Enter a string

Apple

The Original String is

Apple

Copied String is:

Apple

16 . Program to read, display and add two m x n matrices using functions

#include<stdio.h>

#include<conio.h>
void read_arr(int a[10][10],int row,int col)

int i,j;

printf(“enter elements of the matrix\n”);

for(i=1;i<=row;i++)

for(j=1;j<=col;j++)

scanf(\"%d\",&a[i][j]);

void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col)

int i,j;

for(i=1;i<=row;i++)

for(j=1;j<=col;j++)

m3[i][j] = (m1[i][j] + m2[i][j]);


}

void print_arr(int m[10][10],int row,int col)

int i,j;

for(i=1;i<=row;i++)

for(j=1;j<=col;j++)

printf(\"%d \",m[i][j]);

printf(\"\\n\");

main()

int m1[10][10],m2[10][10],m3[10][10],m4[10][10],row,col;

clrscr();

printf(\"Enter number of rows :\");


scanf(\"%d\",&row);

printf(\"Enter number of columns :\");

scanf(\"%d\",&col);

read_arr(m1,row,col);

read_arr(m2,row,col);

add_arr(m1,m2,m3,row,col);

diff_arr(m1,m2,m4,row,col);

print(“sum matrix is…\n”)

print_arr(m3,row,col);

print(“sum difference matrix is…\n”)

print_arr(m4,row,col);

getch();

Output:

Enter number of rows:

Enter number of columns

Enter matrix elements:

1
2

Enter matrix 2 elemetns

Sum matrix is

2 4

6 8

Difference matrix is

00

00

17 . Program to read a string and to find the number of alphabets, digits,


vowels, consonants, spaces and special characters.

#include<stdio.h>
void main()

char str[200];

int i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0;

printf("Enter a string\n");

gets(str);

for(i=0;str[i]!='\0';i++)

if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' || str[i]=='u' || str[i]=='A'


||str[i]=='E' || str[i]=='I' || str[i]=='O' ||str[i]=='U')

vowels++;

else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))

consonants++;

else if(str[i]>='0' && str[i]<='9')

digits++;

else if (str[i]==' ')


{

spaces++;

else

specialCharacters++;

printf("\nVowels = %d",vowels);

printf("\nConsonants = %d",consonants);

printf("\nDigits = %d",digits);

printf("\nWhite spaces = %d",spaces);

printf("\nSpecial characters = %d",specialCharacters);

Output:

Enter a string

“India is a country with 130 Crore Population!!!”


Vowels: 14

Consonants:19

Digits: 3

White spaces : 7

Special Characters : 3

18 . Program to swap two number using pointer

# include < stdio.h >

int main( )
{

int a, b, temp ;

int *p1, *p2 ;

printf(" Enter the first number : ") ;

scanf("%d ",& a) ;

printf("\n Enter the second number : ") ;

scanf("%d ",& b) ;

printf("\n Two Number before swapping :%d, %d ",*p1, *p2) ;

temp = *p1 ;

*p1 = *p2 ;

*p2 = temp ;

printf("\n Two Number after swapping :%d, %d ",*p1, *p2) ;

return ( 0 );

Output:
Enter First Number:

10

Enter the Second Number:


20

Two numbers before swapping:

10

20

Two numbers after swapping:

20

10

19 . Program to demonstrate student structure to read & display records of n


students

#include <stdio.h>

struct student {
char firstName[50];

int roll;

float marks;

} s[5];

int main() {

int i;

printf("Enter information of students:\n");

// storing information

for (i = 0; i < 5; ++i) {

s[i].roll = i + 1;

printf("\nFor roll number%d,\n", s[i].roll);

printf("Enter first name: ");

scanf("%s", s[i].firstName);

printf("Enter marks: ");

scanf("%f", &s[i].marks);

printf("Displaying Information:\n\n");

// displaying information

for (i = 0; i < 5; ++i) {

printf("\nRoll number: %d\n", i + 1);

printf("First name: ");

puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);

printf("\n");

return 0;

Output:
Enter student Information:

111

Sree

76.76

112

Bindu

96.96

113

Manju

92.92

114

Maggi

87.87

115

Kowshik

94.94
Output:

Roll NO: 111

FirstName: sree

Marks: 76.76

Roll NO: 112

FirstName: Bindu

Marks: 96.96

Roll NO: 113

FirstName: Manju

Marks: 92.92

Roll NO: 114

FirstName: Maggi

Marks: 87.87

Roll NO: 115

FirstName: Kowshik

Marks: 94.94
20 . Program to find difference between Structure and Union

#include <stdio.h>

struct Employee

{
int age;

char Name[50];

char Department[20];

float Salary;

};

union Person

int ag;

char Nam[50];

char Departent[20];

float Salary;

};

int main()

struct Employee emp1;

union Person Person1;

printf(" The Size of Employee Structure = %d\n", sizeof (emp1) );

printf(" The Size of Person Union = %d\n", sizeof (Person1));

return 0;

Outout:

The Size of Employee Structure = 124


The Size of Person Union = 100

In union the longest memory occupied by any data type only that memory is
occupied.

You might also like