Lab File Work
Lab File Work
Syntax:
#include<stdio.h>
int main()
int n, reverse = 0;
scanf("%d",&n);
while (n > 0)
n = n/10;
return 0;
Algorithm:
Enter a number
reverse=reverse*10
reverse=reverse+n%10
n=n/10
output:-
enter a number to reverse
123
321
Syntax:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if(num % 2 == 0)
else
return 0;
Algorithm:-
Enter a number
output:-
enter a number:
4
4 is a even
Syntax:
#include <stdio.h>
int main()
char ch;
else {
return 0;
Algorithm:
Enter an alphabat
If ch matches any of the given condition in if statement then entered alphabet is a an alphabet
A is a vowel
Syntax:
#include <stdio.h>
int main()
int A, B, C;
return 0;
}
Algorithm:-
Start
2. Read the three numbers to be compared, as A, B and C.
3. Check if A is greater than B.
output:
enter the numbers A,B and C : 2 3 4
4 is the largest number
Syntax:-
#include <stdio.h>
#include <math.h>
int main()
float a, b, c;
float discriminant;
if(discriminant > 0)
printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
else if(discriminant == 0)
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
return 0;
}
Algorithm:-
display Roots are realand real roots exists: 0.81 and -0.31
Output:-
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 8-4-2
Syntax:-
#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if (year % 400 == 0) {
else if (year % 4 == 0) {
else {
return 0;
Algorithm:-
Read the program
Enter a year
Output:-
Syntax:-
#include <stdio.h>
main()
{
int n;
printf("enter a number:\n");
scanf("%d",&n);
if (n>0)
printf("number is positive\n");
if (n<0)
printf("number is negative\n");
if (n==0)
Algorithm:-
Enter a number
Syntax:-
#include <stdio.h>
main()
int n;
printf("enter a character:-\n");
scanf("%c",&n);
if (n>='A'&&n<='Z'||n>='a'&&n<='z')
printf("character is an alphabet\n");
else
printf("character is a digit\n");
Algorithm:-
Enter a character
If entered character is within the range between ‘a’ and ‘z’ or ‘A’ and ‘Z’, then it is an
alphabet and if condition will be executed
Syntax:-
#include <stdio.h>
main()
int n,rev,sum=0;
scanf("%d",&n);
while (n>0)
rev=n%10;
sum=sum+rev;
n=n/10;
Algorithm:-
Enter a natural number
When number is entered by user, ‘%’is applied on it and remainder is stored in variable
named rev
The n/10 is performed so to let other numbers be separated and added in sum
Syntax:-
#include <stdio.h>
main()
int copy,n,rev=0,r;
printf("enter a number:\n");
scanf("%d",&n);
copy=n;
while (n>0)
r=n%10;
rev=(rev*10)+r;
n=n/10;
printf("reverse of a number:%d\n",rev);
if (copy==rev)
printf("palindrome\n");
else
printf("not palindrome\n");
}
Algorithm:-
Enter a number
If rev = to entered number then number is palindrome, if not then it is not a palindrome
Syntax:-
#include <stdio.h>
main()
int a,b;
char op=('+','-','*','/');
printf("choose operator=\n");
scanf("%c",&op);
scanf("%d %d",&a,&b);
switch (op)
case '+':a+b;printf("sum=%d\n",a+b);
break;
case '-':a-b;printf("difference=%d\n",a-b);
break;
case '*':a*b;printf("product=%d\n",a*b);
break;
case '/':a/b;printf("division=%d\n",a/b);
break;
Algorithm:-
Choose an operator
When user will choose and enter the operator, switch case will act
Depending upon the operator that is been chosen by user the cases in switch case will act
After this break condition further stops the compiler in performing function and gives output
to the user
Syntax:-
#include <stdio.h>
main()
int num;
printf("Enter an integer number:");
scanf("%d", &num);
Algorithm:-
Enter any integer number
Syntax:-
#include <stdio.h>
int main()
scanf("%d", &num1);
return 0;
Algorithm:-
Enter two values
Then add two numbers and print its value using printf function
Syntax:-
#include <stdio.h>
int main()
scanf("%d", &num2);
Algorithm:-
Enter any two numbers
After entering of numbers compiler will find out product and will print output using printf
command.
Syntax:-
#include <stdio.h>
int main()
int num;
int sum, i;
float average;
average = sum/(float)5;
return 0;
Algorithm:-
Enter any five numbers to find average
Syntax:-
#include <stdio.h>
int main()
char ch;
scanf("%c", &ch);
printf("ASCII value of '%c' is %d\n", ch, ch);
return 0;
Algorithm:-
Enter a single character
Now due to %c specifier in printf function the ascii value of character will be printed as an
output
Output:-
enter a single character: A
Ascii value of A is 65
Syntax:-
#include <stdio.h>
int main()
int q, rem;
scanf("%d", &num1);
printf("Enter second number :");
scanf("%d", &num2);
if(num2==0) {
return 1;
q = num1/num2;
return 0;
algorithm:-
enter two numbers
if entered number is equal to zero it cannot be divided and cant didvide by zero will be
printed as an output
if it is not equal to zero, compiler will print quotient and remainder by performing
q=num1/num2 (for quotienet )and rem=num1%num2(for remainder)
stop
output:-
enter first number: 4
quotient=2
remainder=0
18.Aim:- Write a C Program to print size of int, char, float, double.
Syntax:-
#include <stdio.h>
int main()
return 0;
Output:-
Size of char=1
Size of int=4
Size of float=4
Size of double=8
Algorithm:-
19.Aim:- Write a C Program to swap two numbers.
Syntax:-
#include <stdio.h>
int main()
int tmp;
num1 = 10;
num2 = 20;
tmp = num1;
num1 = num2;
num2 = tmp;
return 0;
}
Algorithm:-
Output:-
num1=10, num2=20
num1=20, num2=10
Syntax:-
#include <stdio.h>
main()
int no,i,fact=1;
printf("enter a number=\n");
scanf("%d",&no);
for (i=no;i>=1;i--)
fact=fact*i;
printf("factorial=%d\n",fact);
}
Algorithm:-
Enter a number
end
output:-
enter a number=
factorial= 6
Syntax:-
int main()
int num;
int i;
printf("Enter a number:");
scanf("%d", &num);
for(i=1; i<=10; i++){
return 0;
algorithm:-
enter a number
end
output:-
enter a number:5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
Syntax:-
#include <stdio.h>
main()
int fib[10];
int n;
int i;
fib[0] = 1;
fib[1] = 1;
printf("How many numbers do you want to print of fibonacci series (should be <=
10)? ");
if(n>10){
printf("I can't handle more than 1000 elements, increment 'fib' array size to do
so\n");
return 1;
Algorithm:-
First two numbers in fibonacci sequence is 1
stop
output:-
how many number do you want to print of Fibonacci series(should be<=)? 5
11235
Syntax:-
#include <stdio.h>
main()
break;
++max;
Algorithm:-
Enter two positive integers
stop
output:-
enter two positive numbers: 4 2
Syntax:-
#include <stdio.h>
main()
{
int n1,n2;
scanf("%d\n%d",&n1,&n2);
while(n1!=n2)
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
Output:-
Enter two numbers:
2 4
Syntax:-
#include <stdio.h>
int main()
int num;
int cnt;
printf("Enter a number:");
scanf("%d", &num);
cnt = 0;
while(num){
cnt++;
num = num/10;
Algorithm:-
Enter a number
Initialize count to 0
Stop
Output:-
Enter a number:2345
Number of digits:4
Syntax:-
#include <stdio.h>
main()
int num;
int np;
int pow;
int i;
scanf("%d", &num);
scanf("%d", &np);
pow = 1;
Algorithm:-
Enter a number
Iterate from 0 till np and multiply by 'num' to find nth power of 'num'
Stop
Output:-
Enter a number:4
Syntax:-
#include <stdio.h>
main()
int num;
int i;
scanf("%d", &num);
for(i=2; i<=num/2; i++)
if(num%i==0) {
return 0;
Algorithm:-
Enter a number
For loop to check whether given number can be divisible by any number other than itself.
Stop
Output:-
Enter a number:2
Syntax:-
#include <stdio.h>
int c=1;
for(int i=1;i<=d;i++)
{
c=c*r;
return c;
main()
int n,temp,digits=0,rem,sum=0;
printf("enter number\n");
scanf("%d",&n);
temp=n;
while(temp!=0)
digits++;
temp=temp/10;
temp=n;
while(temp!=0)
rem=temp%10;
sum=sum+power(rem,digits);
temp=temp/10;
if(n==sum)
{
printf("number is armstrong");
else{
Algorithm;-
enter number
stop
output:-
enter a number
234
Syntax:-
#include <stdio.h>
main()
int number,i;
printf("enter a number\n");
scanf("%d",&number);
for(i=1;i<=number;i++)
{if(number%i==0)
printf("factor:%d\n", i);
Algorithm:-
enter a number
i initialized with 1
stop
output:-
enter a number
Factor:
Syntax:-
#include <stdio.h>
main() {
originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
++count;
originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
if ((int)result == number) {
count = 0;
result = 0;
}
Algorithm:-
Enter two numbers(intervals)
Arrange numbers from (low+1) to (high-1) and also check whether a number is Armstrong or
not
Result contains sum of nth power of numbers. so check whether number si equal to sum of
nth power or not
Stop
Output:-
Enter two numbers(intervals): 200
2000
Armstrong number between 200 and 2000 are: 370 371 407 1634
Syntax:-
#include <stdio.h>
main()
int num[10],i;
for (i=0;i<10;i++)
scanf("%d",&num[i]);
for (i=0;i<10;i++)
{
Output:
Enter any number=
0123456789
Algorithm:
‘enter any number
Use second loop for printing the number that are been entered using printf
Stop
33.Aim: Write a program in C to read n number of values in an array
and display it in reverse order.
Syntax:
#include <stdio.h>
main()
int num[5],i,a,rev=0;
for (i=0;i<5;i++)
scanf("%d",&num[i]);
for (i=4;i>=0;i--)
printf("%d\n",num[i]);
Output:
Enter any 5 number=
12345
1
Algorithm:
Enter any 5 numbers
stop
Syntax:
#include <stdio.h>
main()
int num[5],i,sum=0;
printf("enter 5 numbers=\n");
for (i=0;i<5;i++)
scanf("%d",&num[i]);
sum=sum+num[i];
for (i=0;i<5;i++)
}
Output:
enter 5 numbers=
algorithm:
enter any 5 numbers
the first loop is used for entering numbers in array and finding sum of all entered elements in
an array
the second loop is is used for printing out the entered numbers in an array
stop
Syntax:
#include <stdio.h>
main()
{
int arr1[5],arr2[5],i;
for (i=0;i<5;i++)
scanf("%d",&arr1[i]);
for (i=0;i<5;i++)
for (i=0;i<5;i++)
arr2[i]=arr1[i];
for (i=0;i<5;i++)
printf("numbers in arr2[i]=%d\n",arr2[i]);
Output:
enter any 5 numbers:
4
5
numbers in arr2[i]=2
numbers in arr2[i]=3
numbers in arr2[i]=4
numbers in arr2[i]=5
algorithm:
enter 5 numbers
enter first 5 numbers in first array using scanf in for loop and
then using for loop copy the numbers of first array to another array and
stop
Syntax:
#include <stdio.h>
int main() {
int Arr[10],i,j,count = 0;
count++;
break;
return 0;
Output:
Enter 8 numbers
1234512467
Use if condition in for loop to check whether the entered numbers in both the arrays
(arr[i]=arr[j]) and if the condition is true then count++ ie the number is duplicate
And it will be counted out how much number are duplicate and are printed by printf
command
Stop
Syntax:
#include <stdio.h>
void main()
int i, j, k;
for(i=0;i<4; i++)
arr3[i] = arr1[i];
for(j=0;j<4; j++)
arr3[i] = arr2[j];
i++;
}
for(i=0;i<8; i++)
for(k=0;k<8-1;k++)
if(arr3[k]<=arr3[k+1])
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
printf("\n\n");
Output:
The merged array in decending order is :
8 7 6 5 3 2 1 0
Algorithm:
Enter the numbers in arr1[] and arr2[] while compiling the program
Use for loop to merge both the arrays to arr3[] one by one
After merging sort the elements in arr3[] using for loop in which if cond, is used such as
arr3[k]<=arr3[k+1].
Stop
Syntax:
#include <stdio.h>
main()
int arr[10],num,i,freq=0;
for (i=0;i<10;i++)
scanf("%d",&arr[i]);
scanf("%d",&num);
for (i=0;i<10;i++)
if (arr[i]==num)
freq++;
printf("frequency of selected number=%d",freq);
Output:
enter numbers in array=
algorithm:
enter numbers in an array
using for loop in which if cond. Is used ie- arr[i]==num we can find out number of frequency
stop
39.Aim: Write a program in C to find the maximum and minimum
element in an array.
main()
int arr[5],i,max;
for (i=0;i<5;i++)
scanf("%d",&arr[i]);
max=arr[0];
for (i=0;i<5;i++)
if (arr[i]>max)
max=arr[i];
Output:
enter numbers in an array=
3
4
algorithm:
enter numbers in an array
set a max number ie-max=arr[0] and compare numbers with respect to this number using if
condition ie arr[i]>max
stop
for minimum
#include <stdio.h>
main()
int arr[5],i,min;
for (i=0;i<5;i++)
scanf("%d",&arr[i]);
min=arr[0];
for (i=0;i<5;i++)
if (arr[i]<min)
min=arr[i];
}
printf("min number is=%d\n",min);
Output:
enter numbers in an array=
Syntax:
#include <stdio.h>
main()
int arr1[2][2],arr2[2][2],arr3[2][2],i,j,k;
for (i=0;i<2;i++)
for (j=0;j<2;j++)
scanf("%d",&arr1[i][j]);
for (j=0;j<2;j++)
scanf("%d",&arr2[i][j]);
for (i=0;i<2;i++)
for (j=0;j<2;j++)
arr3[i][j]=0;
for (k=0;k<2;k++)
arr3[i][j]+=arr1[i][k]*arr2[k][j];
for (i=0;i<2;i++)
for (j=0;j<2;j++)
printf("%d\n",arr3[i][j]);
}
}
Output:
enter numbers in first array=
20
13
Algorithm:
Enter numbers in two d array(contains rows and columns)using scanf function
First enter numbers in first array and then in second using scanf
After his multiply the two array using cond. arr3[i][j]+=arr1[i][k]*arr2[k][j] in which arr1[i]
[j]=0
Then after multiplying store the result in array[3] and print out the result using printf
Stop
41.Aim: Write a program in C to delete an element at desired position
from an array
Syntax:
#include <stdio.h>
main()
int arr[5],i,j,key,index;
for (i=0;i<5;i++)
scanf("%d",&arr[i]);
scanf("%d",&key);
index=4;
for (i=0;i<5;i++)
if (arr[i]==key)
for (j=i+1;j<=index;j++)
arr[j-1]=arr[j];
i--;
index--;
}
}
for (i=0;i<index;i++)
printf("%d\n",arr[i]);
Output:
enter numbers in an array=
124
Algorithm:
enter numbers in an array
now compare the selected number by the user with a[i] using if cond.ie- arr[i]==key
Stop
42.Aim: Write a program in C to find transpose of a given matrix
Syntax:
#include <stdio.h>
main()
int arr[3][3],i,j;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
scanf("%d",&arr[i][j]);
printf("matrix:\n");
for (i=0;i<3;i++)
for (j=0;j<3;j++)
printf("element of matrix=%d\n",arr[i][j]);
printf("transpose of matrix=\n");
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
printf("transpose matrix element =%d\n",arr[j][i]);
Output:
enter numbers in matrix=
10
15
20
25
30
35
40
45
matrix:
element of matrix=5
element of matrix=10
element of matrix=15
element of matrix=20
element of matrix=25
element of matrix=30
element of matrix=35
element of matrix=40
element of matrix=45
transpose of matrix=
algorithm:
enter numbers in matrix using scanf and print out the entered numbers in an array using printf
command
then find out the transpose of the matrix using for loop ie- transpose matrix element =%d\
n",arr[j][i]
stop
Syntax:
#include <stdio.h>
main()
int arr[3][3],i,j,sum=0;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
scanf("%d",&arr[i][j]);
}
for (i=0;i<3;i++)
for (j=0;j<3;j++)
if (i==j)
sum=sum+arr[i][j];
printf("sum of diagonals=%d\n",sum);
Output:
enter elements in array=
sum of diagonals=15
Algorithm:
Enter number of elements in an array using scanf
Use if cond. In loop to find the sum of diagonal elements in an array ie- if (i==j)
sum=sum+arr[i][j]
stop
Syntax:
#include <stdio.h>
main()
int a[3][3],i,j,r1,r2,r3,deter;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("DETERMINANT:\n");
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
r1 = a[0][0] * ((a[1][1] * a[2][2])
- (a[2][1] * a[1][2]));
- (a[2][0] * a[1][2]));
- (a[2][0] * a[1][1]));
deter= r1 - r2 + r3;
printf("solved determinant=%d\n",deter);
Output:
enter elements in an array=
9
DETERMINANT:
solved determinant=0
algorithm:
enter elements in an array
- (a[2][1] * a[1][2]));
- (a[2][0] * a[1][2]));
- (a[2][0] * a[1][1]));
deter= r1 - r2 + r3;
stop
main()
{4, 0, 0},
{0, 5, 0},
{0, 0, 6}
};
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;
if(a[i][j] == 0)
count++;
else
Output:
Given matrix is a sparse matrix
Algorithm:
Declare and initialize a two-dimensional array a.
Calculate the number of rows and columns present in the given array and store it in variables
rows and cols respectively.
Loop through the array and count the number of zeroes present in the given array and store in
the variable count.
Calculate the size of the array by multiplying the number of rows with many columns of the
array.
If the count is greater than size/2, given matrix is the sparse matrix. That means, most of the
elements of the array are zeroes.
Stop
Syntax:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
return;
min1Pair = arr1[0];
min2Pair = arr1[1];
minSum = sum;
min1Pair = arr1[i];
min2Pair = arr1[j];
int main()
int arr1[] = {38, 44, 63, -51, -35, 19, 84, -69, 4, -46};
printf("\n");
//------------------------------------------------------
findMinSumPair(arr1, ctr);
return 0;
Output:
The given array is : 38 44 63 -51 -35 19 84 -69 4 -46
[44, -46]
Algorithm:
Syntax:
#include <stdio.h>
#include <stdlib.h>
int compare(const void* one, const void* two)
int ctrTriangle = 0, i, j, k;
k = j +1;
k++;
ctrTriangle += k - j - 1;
return ctrTriangle;
int main()
int i;
printf("\n");
printf("Number of possible triangles can be formed from the array is: %d\n",
CountNumberOfTriangles(arr1, n));
return 0;
Algorithm:
Let a, b and c be three sides. The below condition must hold for a triangle (sum of two sides
is greater than the third side)
i) a + b > c
ii) b + c > a
iii) a + c > b
run a nested loop. The outer loop runs from start to end and the innner loop runs from index
+ 1 of the first loop to the end. Take the loop counter of first loop as i and second loop as j.
Take another variable k = i + 2
there is two pointers i and j, where array[i] and array[j] represents two sides of the triangles.
For a fixed i and j, find the count of third sides which will satisfy the conditions of a triangle.
i.e find the largest value of array[k] such that array[i] + array[j] > array[k]
when we get the largest value, then the count of third side is k – j, add it to the total count.
stop
Syntax:
include <stdio.h>
int i,j,k;
int sum,maxSum = 0;
sum = 0;
if(sum>maxSum)
maxSum = sum;
}
return maxSum;
int main()
int i;
printf("\n");
return 0;
Output:
The given array is : 8 3 8 -5 4 3 -4 3 5
Syntax:
#include <stdio.h>
main()
int a,b,sum;
scanf("%d %d",&a,&b);
sum=a-~b-1;
printf("sum of numbers=%d\n",sum);
Output:
enter two numbers=
sum of numbers=3
algorithm:
enter two numbers
using eq. sum=a-~b-1 wecan find the sum of two numbers without ‘+’
stop
Syntax:
#include <stdio.h>
main()
{
int n;
printf("enter a numbers=\n");
scanf("%d",&n);
n<<1;
printf("multiplication of number=%d\n",n<<1);
Output:
enter a number=
multiplication of number=8
algorithm:
enter a number
stop
Syntax:
#include <stdio.h>
main()
int n;
printf("enter a number=\n");
scanf("%d",&n);
n>>1;
printf("division of number=%d\n",n>>1);
}
Output:
enter a number=
division of number=2
algorithm:
enter a number that is to be divided by 2
stop