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

LabProgramsC

The document contains a series of C programs aimed at solving various programming problems, including finding the largest of two numbers, checking if a number is even or odd, calculating the sum of digits, generating Fibonacci series, converting temperatures, reversing numbers, checking for Armstrong numbers, finding roots of quadratic equations, sorting numbers, and performing matrix addition and multiplication. Each section includes an algorithm, the corresponding C code, and sample output for clarity. The programs are designed to demonstrate fundamental programming concepts and logic.

Uploaded by

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

LabProgramsC

The document contains a series of C programs aimed at solving various programming problems, including finding the largest of two numbers, checking if a number is even or odd, calculating the sum of digits, generating Fibonacci series, converting temperatures, reversing numbers, checking for Armstrong numbers, finding roots of quadratic equations, sorting numbers, and performing matrix addition and multiplication. Each section includes an algorithm, the corresponding C code, and sample output for clarity. The programs are designed to demonstrate fundamental programming concepts and logic.

Uploaded by

Smitha Rajesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

1.

C Program to Find Largest of Two numbers

Aim: To Find Largest of Two numbers

Algorithm:

Step 1: Start
Step 2: Read a, b .
Step 3: If a>b then
Display “a is the largest number”.
Otherwise
Display “b is the largest number”.
Step 4: Stop.
Program
#include <stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter values for a and b\n");
scanf("%d %d", &a, &b);
if(a > b)
{
printf("%d is the Largest number\n", a);
}
else
{
printf("%d is the Largest number\n", b);
}
getch():
}
Output:
Enter values for a and b
30 20
30 is largest number

2. Program to Check Even or Odd

Aim: To check the given number is even or odd

Algorithm:
Step1: start
Step 2: READ number
Step 3: remainder=number%2
Step 4: IF remainder==0
print "Even Number"
ELSE
print "Odd Number"
Step 5: stop

Program
#include <stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even number.", num);
else
printf("%d is odd. number", num);
getch();
}

Output
Enter an integer: 7
7is odd number.

3.sum of digits of a number

Aim: To find the sum of the digits of the given number


Algorithm
Step 1: Get number by user
Step 2: Get the modulus/remainder of the number
Step 3: sum the remainder of the number
Step 4: Divide the number by 10
Step 5: Repeat the step 2 while number is greater than 0.

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,m;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
getch();
}
Output:
Enter a number:654
Sum is=15
Enter a number:123
Sum is=6

4. Generation of Fibonacci series

Aim : To generate Fibonacci series with the given input.

Algorithm
Step 1: Start
Step 2: Declare variable n1, n2, n3, i
Step 3: Initialize variable n1=1, n2=1, i=2
Step 4: Read n from user
Step 5: Print n1 and n2
Step 6: Repeat until i<= n
1n3=n1+n2
print n3
n1=n2;
n2=n3;
i=i+1
Stop 7: Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n3,i,number;
clrscr()
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=0;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
getch();
}

Output:
Enter the number of elements: 4
0112
5. Temperature conversion

Aim : To convert Celsius to Fahrenheit

Algorithm
1. Define temperature in Celsius unit.
2. Apply in the formula,
Fahrenheit = (celsius*9)/5)+32;
3. Print the temperature in Fahrenheit.

Program
#include<stdio.h>
#include<conio.h>
void main()
{
float fahrenheit, celsius;
clrscr();
scanf(“enter the Celsius reading”, &celsius);
fahrenheit =( (celsius*9)/5)+32;
printf("Temperature in fahrenheit is: %f",fahrenheit);
getch();
}
Output:
Temperature in Fahrenheit is: 75.1999997
6. Reversing a given number

Aim: To find the reverse of a given number

Algorithm
Step 1: start
Step 2: Read n
Step3: initialize reverse=0
Step 4: Read n
Step 5: Check whether n>0
Step 6: rem=n%10;
Step 7: reverse=reverse*10+rem;
Step 8: n/=10;
Step 9: print reverse
Step 10: stop

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, reverse=0, rem;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
getch();;
}

Output:
Enter a number: 123
Reversed Number: 321

7 Checking whether a number is Armstrong or not

Aim: To check whether the given number is Armstrong or not

Algorithm
Step 1: Start
Step 2: Declare Variable sum, temp, n
Step 3: Read n from User
Step 4: Initialize Variable sum=0 and temp=n
Step 5: Repeat Until n>=0
5.1 sum=sum + cube of last digit i.e [(n%10)*(n%10)*(n%10)]
5.2 n=n/10
Step 6: IF sum==temp
Print "Armstrong Number"
ELSE
Print "Not Armstrong Number"
Step 7: Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
clrscr();
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");
getch();
}
Output:
enter the number=153
Armstrong number
153 = (1*1*1) + (5*5*5)+(3*3*3)

enter the number=5


not Armstrong number
8. Roots of a quadratic equation

Aim: To find the roots of a quadratic equation

Algorithm
Step 1: Input the value of a, b, c.
Step 2: Calculate discriminant = b*b - 4*a*c
Step 3 :. If (discriminant < 0)
Display "Roots are Imaginary,
Calculate root1 = (-b +i sqrt(k))/ 2a and root2 =(b + isqrt(k))/ 2a.
else if (discriminant = 0)
Display "Roots are Equal" and calculate root1 = root2 = (-b / 2*a)
else
Display "Roots are real and calculate
root1 = -b +-d / 2*a androot2 = -b – Sqrt(d / 2*a)
Step 4: Print r1 and r2.
Step 5: stop

Program
#include <math.h>
#include <stdio.h>
#include<conio.h>
void main()
{
double a, b, c, discriminant, root1, root2, realPart, imagPart;
clrscr();
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart,
realPart, imagPart);
}
getch();
}

Output
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
9. Sorting numbers in ascending order

Aim: To sort the given list of numbers in ascending order

Algorithm:
Step 1: start
Step 2: initialize arr[] ={5, 2, 8, 7, 1 }
Step 3: set temp =0
Step 4: length= sizeof(arr)/sizeof(arr[0])
Step 5: print "elements of original array"
Step 6: set i=0. Repeat step 7 and step 8 until i<length
Step 7: print arr[i]
Step 8: i=i+1.
Step 9: set i=0. Repeat step 10 to step until i<n
Step 10: set j=i+1. Repeat step 11 until j<length
Step 11: if(arr[i]>arr[j]) then
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp
Step 12: j=j+1.
Step 13: i=i+1.
Step 14: print new line
Step 15: print "elements of array sorted in ascending order"
Step 16: set i=0. Repeat step 17 and step 18 until i<length
Step 17: print arr[i]
Step 18: i=i+1.
Step19: Stop.

Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
clrscr();
int length = sizeof(arr)/sizeof(arr[0]);
printf("Elements of original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}}
printf("\n");
printf("Elements of array sorted in ascending order: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
getch();
}

Output:
Elements of original array:
52871
Elements of array sorted in ascending order:
12578
10. Sorting in descending order
Aim: To sort the given list of numbers in descending order

Algorithm:
Step 1: start
Step 2: initialize arr[] ={5, 2, 8, 7, 1 }.
Step 3: set temp =0
Step 4: length= sizeof(arr)/sizeof(arr[0])
Step 5: print "elements of original array"
Step 6: set i=0. Repeat step 7 and step 8 until i<length
Step 7: print arr[i]
Step 8: i=i+1.
Step 9: set i=0. Repeat step 10 to step 13 until i<n
Step 10: set j=i+1. Repeat step 11 and step 12 until j<length
Step 11: if(arr[i]
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp
Step 12: j=j+1.
Step 13: i=i+1.
Step 14: print new line
Step 15: print "elements of array sorted in ascending order"
Step 16: set i=0. Repeat step 17 and step 18 until i<length
Step 17: print arr[i]
Step 18: i=i+1.
Step 20: stop.

Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
clrscr();
int length = sizeof(arr)/sizeof(arr[0]);
printf("Elements of original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\n");
printf("Elements of array sorted in descending order: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
getch();
}

Output:
Elements of original array:
52871
Elements of array sorted in descending order:
87521
11. Matrix addition

Aim: To find the sum of 2 matrices

Algorithm

Step 1:start

Step 2:Declare first[10][10], second[10][10], , m, n, c, d

Step 3:Calculate the number of rows and columns present in the array a (as
dimensions of both the arrays are same and store it in variables rows and cols
respectively.

Step 4:Declare another array sum[10][10].

Step 5:Loop through the and add the corresponding elements as


sum[c][d] = first[c][d] + second[c][d];
Display the elements of array sum.

Step 6:stop

Program
# include <stdio.h>

#include<conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

clrscr();
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
getch();
}

output:
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
1 2
2 4
Enter the elements of second matrix
5 6
2 1
Sum of enteed matrices
6 8
5 5
12. Matrix multiplication

Aim: To find the product of 2 matrices

Algorithm:

Step 1:Start

Step 2:Declare variables and initialize necessary variables as

a[10][10],b[10][10],mul[10][10],r,c,i,j,k;

Step 3:Enter the element of matrices by row wise using loops

Step 4:Check the number of rows and column of first and second matrices

Step 5:If number of rows of first matrix is equal to the number of columns of
second matrix, go to step 6. Otherwise, print matrix multiplication is not
possible and go to step 3.

Step 6:Multiply the matrices using nested loops as

mul[i][j]+=a[i][k]*b[k][j];

Step 7:Print the product in matrix form as console output.

Step 8:Stop
Program

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clrscr()
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]);
}
}
printf("multiply of the matrix=\n");
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");
}
getch();
}

Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

13. Program to check vowel or consonant


Aim: C program to check vowel or consonant using switch… case statement

Algorithm:
Step 1: Start

Step 2:Input an alphabet from user. Store it in some variable say ch.

Step 3:Switch the value of ch.

Step 4:For ch, there are 10 possibilities for vowel we need to check
i.e. a, e, i, o, u, A, E, I, O and U.

Step 5:Write all 10 possible cases for vowels and print "Vowel" for each case.

Step 6:If alphabet is not vowel then add a default case and print "Consonant".

Step7: Stop

Program
#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
getch();
}

Output:
Enter any alphabet: E
Enter any alphabet
I
Vowel
Enter any alphabet
C
Consonant

14. Factorial using recursion


Aim: To find the factorial of the given number using recursion

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>
#include<conio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
getch();
}

Output:
Enter a number: 6
Factorial of 5 is: 720
15.checking of palindrome for string
Aim: To check whether the given string is palindrome or not

Algorithm:
Step 1: Start

Step 2: Declare variables as char string1[20], int i, length, flag = 0;


Step 3: Find length of str. Let length be n.
Step 4: Initialize low and high indexes as 0 and h respectively.
Step 5: Do following while low index ‘i’ is smaller than high index

Step 6: If str[i] is not same as str[i-1], then return false.


Step 7: Increment i and decrement h, i.e., do i++ and h–.

Step 8: stop

Program

#include <stdio.h>
#include <string.h>
void main(){
char string1[20];
int i, length;
int flag = 0;
clrscr();
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++)
{
if(string1[i] != string1[length-i-1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome", string1);
}
else
{
printf("%s is a palindrome", string1);
}
getch();
}

Output
Enter a string: Malayalam
Malayalam is a palindrome

16. Concatenate two strings

Aim: To concatenate two strings

Algorithm
Step 1: Start
Step 2: Declare a new Strings char s1[20], s2[20];
Step3: Read two strings
Step 4: Concatenate 2 strings using strcat(s1,s2);
Step 5: Print the concatenated string
Step 6: Stop
Program
#include <stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20];
char s2[20];
clrscr();
printf("Enter the first string : ");
scanf("%s", s1);
printf("\nEnter the second string :");
scanf("%s",s2);
strcat(s1,s2);
printf("The concatenated string is : %s",s1);
getch();
}

17.Swapping using call by reference


Aim To write a C program to swap two numbers using call by reference.
Algorithm:
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(&a,&b)
Step 3a: Start fuction
Step 3b: Assign t ← *x
Step 3c: Assign *x ← *y
Step 3d: Assign *y ← t
Step 3e: End function
Step 4: Print x and y.
Step 5: Stop the program.
Program

#include <stdio.h>

#include<conio.h>

void swap(int*, int*);

void main()

int x, y;

clrscr();

printf("Enter the value of x and y\n");

scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

getch();

void swap(int *a, int *b)

int temp;

temp = *b;

*b = *a;

*a = temp;

}
Output
Enter the value for x and y
20 10

Before Swapping
20 10

After Swapping
10 20

18. program to find the largest of the three numbers.

Aim: To write a program to find the largest of the three numbers.

ALGORITHM:
Step-1 Start the program
Step-2 Enter the three numbers
Step-3 Assign large to first number
step-4 Check the next number is greater then the large. If greater then
assign large to next number
Step-5 Compare the next number with large
Step-6 Do the step-4
Step-7 Print the larger value of the three number
Step-8 Stop

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
printf(“\n enter the three numbers”);
scanf(“%f %f %f “,&a,&b,&c);
printf(“\n largest value is”);
if(a>b);
{
if(a>c)
printf(“\t%f”,a);
else
printf(“\n\t%f”,c);
}
else
{
if (c>b)
printf(“\n\t%f”,c);
else
printf(“\n\t %f”,b);
}
}
OUTPUT:
Enter the three numbers 93 43 23
The biggest of three number is 93

19. Program to find the length of a string.

Aim: To write a program to find the length of a string.

ALGORITHM:
Step-1 Start the program
Step-2 Enter the string
Step-3 Find the string length using the function strlen()
Step-4 Print the string length of the entered string
Step-5 Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100];
int length;
clrscr();
printf("Enter the string \n");
gets(a);
length = strlen(a);
printf("Length of the string = %d\n", length);
getch();
}

OUTPUT:
Enter the string
welcome
length of the string is 7

20. Employee information using structure.


Aim: To write a Program to store employee information using structure

Algorithm
Step 1: Start
Step 2: Declare variables using Structure
Step 3:Read details of all employees like employee name,
age, phone number and salary.
Step 4:Display all the details of employees
Step 5: Stop

Program
#include<stdio.h>
#include<conio.h>
#include <string.h>
struct employee{
char name[30];
int empId;
float salary;
};

void main()
{
struct employee emp;
printf("\nEnter details :\n");
printf("Name :");
gets(emp.name);
printf("Id :");
scanf("%d",&emp.empId);
printf("Salary :");
scanf("%f",&emp.salary);
printf("\nEntered detail is:");
printf("Name: %s" ,emp.name);
printf("Id: %d" ,emp.empId);
printf("Salary: %f\n",emp.salary);
getch();
}
Output
Enter details :
Name :Mike
ID :1120
Salary :76543

Entered detail is:


Name: Mike
Id: 1120
Salary: 76543.000000

You might also like