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

Lab File Work

The document contains 14 code snippets with C programs to perform basic mathematical and logical operations like number reversal, checking even-odd, vowel-consonant, largest of 3 numbers, roots of quadratic equation, leap year checking, checking sign of a number, checking alphabet, summation of natural numbers, palindrome checking, basic calculator, taking integer input, addition of 2 numbers, and multiplication of 2 numbers. For each code snippet, it provides the code, inputs/outputs and algorithm to explain the logic.

Uploaded by

Shruti Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Lab File Work

The document contains 14 code snippets with C programs to perform basic mathematical and logical operations like number reversal, checking even-odd, vowel-consonant, largest of 3 numbers, roots of quadratic equation, leap year checking, checking sign of a number, checking alphabet, summation of natural numbers, palindrome checking, basic calculator, taking integer input, addition of 2 numbers, and multiplication of 2 numbers. For each code snippet, it provides the code, inputs/outputs and algorithm to explain the logic.

Uploaded by

Shruti Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

1.

Aim:-to reverse a number

Syntax:
#include<stdio.h>

int main()

int n, reverse = 0;

printf("Enter a number to reverse\n");

scanf("%d",&n);

while (n > 0)

reverse = reverse * 10;

reverse = reverse + n%10;

n = n/10;

printf("Reverse of entered number is = %d\n", reverse);

return 0;

Algorithm:
Enter a number

If number is less than 0 while loop will work

reverse=reverse*10
reverse=reverse+n%10

n=n/10

print the result

output:-
enter a number to reverse

123

Reverse of entered number is=

321

2. Aim:-check whether a number is even or odd

Syntax:
#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

Algorithm:-
Enter a number

num%2==0,if condition will be printed i.e-num is even

if condition is false else condition will be printed

output:-
enter a number:
4

4 is a even

3. Aim:-check whether a character is a vowel or consonant

Syntax:
#include <stdio.h>

int main()

char ch;

printf(" Enter an alphabet: \n");

scanf(" %c", &ch);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

printf("\n %c is a VOWEL.", ch);

else {

printf("\n %c is a CONSONANT.", ch);

return 0;

Algorithm:
Enter an alphabat

If ch matches any of the given condition in if statement then entered alphabet is a an alphabet

If entered alphabet does not matches the condition then it is a consonant


Output:-
Enter an alphabet:

A is a vowel

4. Aim:-find the largest number among three numbers

Syntax:
#include <stdio.h>

int main()

int A, B, C;

printf("Enter the numbers A, B and C: ");

scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)

printf("%d is the largest number.", A);

if (B >= A && B >= C)

printf("%d is the largest number.", B);

if (C >= A && C >= B)

printf("%d is the largest number.", 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.

3.1 If true, then check if A is greater than C.


If true, print 'A' as the greatest number.
If false, print 'C' as the greatest number.

3.2 If false, then check if B is greater than C.


If true, print 'B' as the greatest number.
If false, print 'C' as the greatest number.

output:
enter the numbers A,B and C : 2 3 4
4 is the largest number

5. Aim:- find all roots of a quadratic equation

Syntax:-
#include <stdio.h>

#include <math.h>

int main()

float a, b, c;

float root1, root2, imaginary;

float discriminant;

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

scanf("%f%f%f", &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("Two distinct and real roots exists: %.2f and %.2f", root1, root2);

else if(discriminant == 0)

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

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

else if(discriminant < 0)

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);

return 0;
}

Algorithm:-

Read the value of a , b , c


d = b*b - 4*a*c
if d < 0 then display The Roots are Imaginary.
           else if d = 0
                     then dispaly Roots are Equal.
                                       r = -b / 2*a
                                       display r

                 else r1 = -b + √d / 2*a


                        r2 = -b - √d / 2*a

                    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

Two distinct and real roots exists: 0.81 and -0.31

6, Aim:-to check whether entered number is a leap year or not

Syntax:-
#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;

Algorithm:-
Read the program

Enter a year

If year%4==0 or year%400==0, then the entered year is a leap year

If year%100==0,then the entered year is not a leap year

Output:-

Enter a year: 1900

1900 is not a leap year.

7.Aim:- to check whether a number is positive , negative or equal to


zero

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)

printf("number is equal to zero\n");

Algorithm:-
Enter a number

If a number is greater than zero(n>0) then number is positive

If a number is less than zero(n<0) then number is negative

If a number is equal to zero(n==0) than number is zero itself


8. Aim:-check whether a character is an alphabet or not

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

If not within the range then a character is not an alphabet

9. Aim:-find the sum of natural numbers

Syntax:-
#include <stdio.h>

main()

int n,rev,sum=0;

printf("enter any natural number=\n");

scanf("%d",&n);

while (n>0)

rev=n%10;

sum=sum+rev;

n=n/10;

printf("sum of natural numbers:%d\n",sum);

Algorithm:-
Enter a natural number

When number is entered by user, ‘%’is applied on it and remainder is stored in variable
named rev

Then sum is been performed to get total sum of entered number

The n/10 is performed so to let other numbers be separated and added in sum

Finally sum is been formed as an output


10.Aim:-check whether a number is palindrome or not

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

Save a number in another variable with name copy(acc to above program)

Apply while cond. And perform ‘%’ function on entered number

Apply rev=(rev*10)+r condition

Then ,apply ‘/’function on entered number

If rev = to entered number then number is palindrome, if not then it is not a palindrome

11.Aim:- make a simple calculator

Syntax:-
#include <stdio.h>

main()

int a,b;

char op=('+','-','*','/');

printf("choose operator=\n");

scanf("%c",&op);

printf("enter two numbers=\n");

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

Enter two numbers

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

12.Aim:- C program to take an integer number from user and print it


on screen

Syntax:-
#include <stdio.h>

main()

int num;
printf("Enter an integer number:");

scanf("%d", &num);

printf("Entered number is %d", num); n

Algorithm:-
Enter any integer number

Using printf command, we can print output

Using scanf function, we can give input to the compiler

13.Aim:- Write a C Program to add two numbers.

Syntax:-

#include <stdio.h>

int main()

int num1, num2, sum;

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

sum = num1 + num2;

printf("Sum of enterred numbers is %d", sum);

return 0;

Algorithm:-
Enter two values

As for output use printf command

For input use scanf command

Then add two numbers and print its value using printf function

14,Aim:- Write a C Program to multiply two numbers

Syntax:-
#include <stdio.h>

int main()

int num1, num2, mul;

printf("Enter first number: ");

scanf("%d", &num2);

printf("Enter second number: ");


scanf("%d", &num2);

mul = num1 * num2;

printf("Multiplication of enterred numbers is %d", mul);

Algorithm:-
Enter any two numbers

Using scanf enter numbers

Use printf command an to print output

After entering of numbers compiler will find out product and will print output using printf
command.

15.Aim:- Write a C Program to calculate average of numbers.

Syntax:-
#include <stdio.h>

int main()

int num;

int sum, i;

float average;

printf("Enter five numbers: ");

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


scanf("%d", &num);

sum = sum + num;

average = sum/(float)5;

printf("Average = %f", average);

return 0;

Algorithm:-
Enter any five numbers to find average

After entering numbers compiler will follow if loop

During if loop average of numbers will be finded out by average=sum/(float)5

After this output will be printed out using printf command

This procedure will go on till i<5

16.Aim:- Write a C Program to print ASCII value of character.

Syntax:-
#include <stdio.h>

int main()

char ch;

printf("Enter single character: ");

scanf("%c", &ch);
printf("ASCII value of '%c' is %d\n", ch, ch);

return 0;

Algorithm:-
Enter a single character

Using scanf we can enter 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

17.Aim:- Write a C Program to print quotient and remainder.

Syntax:-
#include <stdio.h>

int main()

int num1, num2;

int q, rem;

printf("Enter first number :");

scanf("%d", &num1);
printf("Enter second number :");

scanf("%d", &num2);

if(num2==0) {

printf("Can't divide by zero.\n");

return 1;

q = num1/num2;

rem = num1 % num2;

printf("Quotient = %d, Remainder = %d\n", q, rem);

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

enter second number: 2

quotient=2

remainder=0
18.Aim:- Write a C Program to print size of int, char, float, double.

Syntax:-
#include <stdio.h>

int main()

printf("Size of char = %lu\n", sizeof(char));

printf("Size of int = %lu\n", sizeof(int));

printf("Size of float = %lu\n", sizeof(float));

printf("Size of double = %lu\n", sizeof(double));

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 num1, num2;

int tmp;

num1 = 10;

num2 = 20;

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

tmp = num1;

num1 = num2;

num2 = tmp;

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

return 0;
}

Algorithm:-

Output:-
num1=10, num2=20

num1=20, num2=10

21.Aim:- Write a C Program to find factorial of number.

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

Apply for loop with condition

fact=fact*i will take place and output will be printed by printf

end

output:-
enter a number=

factorial= 6

22.Aim:-generate multiplication table

Syntax:-
int main()

int num;

int i;

printf("Enter a number:");

scanf("%d", &num);
for(i=1; i<=10; i++){

printf("%d x %d = %d\n", num, i, num*i);

return 0;

algorithm:-
enter a number

compiler will act as per for condition till i<=10 (i=1)

and result of printf will be printed out as an output

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

23.Aim:- Write a C Program to print fibonacci series.

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)? ");

scanf("%d", &n); // Read number

if(n>10){

printf("I can't handle more than 1000 elements, increment 'fib' array size to do
so\n");

return 1;

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

fib[i] = fib[i-1] + fib[i-2];

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

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


}

Algorithm:-
First two numbers in fibonacci sequence is 1

Display message to get number of elements from users

Enter not more than 10 elements due to fib[10] we declared

Calculate fibonacci sequence based on formula fib[n] = fib[n-1] + fib[n-2

Print fibonacci sequence

stop

output:-
how many number do you want to print of Fibonacci series(should be<=)? 5

11235

24.Aim:- Write a C Program to find LCM of two numbers

Syntax:-
#include <stdio.h>

main()

int n1, n2, max;

printf("Enter two positive integers: ");

scanf("%d %d", &n1, &n2);

max = (n1>n2) ? n1 : n2;


while(1)

if( max%n1==0 && max%n2==0 )

printf("The LCM of two numbers is: %d",max);

break;

++max;

Algorithm:-
Enter two positive integers

Check for the maximum number using conditional operator

check for max will be perfectly divisible or not

print output using printf statement

stop

output:-
enter two positive numbers: 4 2

the Lcm of two number is: 4

25.Aim:- Write a C Program to find HCF of two numbers

Syntax:-
#include <stdio.h>

main()
{

int n1,n2;

printf("enter two numbers:\n");

scanf("%d\n%d",&n1,&n2);

while(n1!=n2)

if(n1>n2)

n1=n1-n2;

else

n2=n2-n1;

printf("HCF of two numbers is: %d",n1);

Output:-
Enter two numbers:

2 4

HCF of two number is:2


26.Aim:- Write a C Program to count number of digits in given
number.

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;

printf("Number of digits : %d\n", cnt);

Algorithm:-
Enter a number

Initialize count to 0

Use while loop till number is not becoming zero

Increment count of digits

Divide by 10 so that number will be having one less digit


Print number of digits

Stop

Output:-
Enter a number:2345

Number of digits:4

27.Aim:-  C Program to calculate power of number

Syntax:-
#include <stdio.h>

main()

int num;

int np;

int pow;

int i;

printf("Enter a number: ");

scanf("%d", &num);

printf("Which power of given number do you want to compute? ");

scanf("%d", &np);

pow = 1;

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

pow = pow * num;


printf("Power of given number is %d\n", pow);

Algorithm:-
Enter a number

enter nth power

Initialize 'pow' with 1

Iterate from 0 till np and multiply by 'num' to find nth power of 'num'

Display nth power of given number

Stop

Output:-
Enter a number:4

Which power of a given number do you want to compute? 2

Power of a given number is16

28.Aim:- C Program to check whether given number if prime number


or not.

Syntax:-
#include <stdio.h>

main()

int num;

int i;

printf("Enter a number: ");

scanf("%d", &num);
for(i=2; i<=num/2; i++)

if(num%i==0) {

printf("Given number isn't prime.\n");

return 0;

printf("Given number is prime.\n");

Algorithm:-
Enter a number

For loop to check whether given number can be divisible by any number other than itself.

If it can be divisible, then its not prime number

If not divisible then it is prime

Stop

Output:-
Enter a number:2

Given number is prime

29.Aim:- C Program to check whether given number is armstrong


number or not

Syntax:-
#include <stdio.h>

int power(int r,int d)

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{

printf("number is not armstrong");

Algorithm;-
enter number

find digits of a number using while loop

checking for armstrong or not using if loop

print output dependend upon the condition using printf

stop

output:-
enter a number

234

number is not Armstrong

30.Aim:-  C Program to display factors of number

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

check for remainder 0

print factors line by line

stop

output:-
enter a number

Factor:

31.Aim:- display Armstrong number between two intervals:-

Syntax:-
#include <stdio.h>
main() {

int low, high, number, originalNumber, rem, count = 0;

double result = 0.0;

printf("Enter two numbers(intervals): ");

scanf("%d %d", &low, &high);

printf("Armstrong numbers between %d and %d are: ", low, high);

for (number = low + 1; number < high; ++number) {

originalNumber = number;

while (originalNumber != 0) {

originalNumber /= 10;

++count;

originalNumber = number;

while (originalNumber != 0) {

rem = originalNumber % 10;

result += pow(rem, count);

originalNumber /= 10;

if ((int)result == number) {

printf("%d ", 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

And the print out the output using printf

Stop

Output:-
Enter two numbers(intervals): 200

2000

Armstrong number between 200 and 2000 are: 370 371 407 1634

32.Aim: Write a program in C to store elements in an array and print


it

Syntax:-

#include <stdio.h>

main()

int num[10],i;

printf("enter any number=\n");

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

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

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

printf("entered number is=%d\n",num[i]);

Output:
Enter any number=

0123456789

Entered number is=0

Entered number is=1

Entered number is=2

Entered number is=3

Entered number is=4

Entered number is=5

Entered number is=6

Entered number is=7

Entered number is=8

Entered number is=9

Algorithm:
‘enter any number

Use 2 for loop and set the condition for eachloop

Use first loop for entering numbers using scanf

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;

printf("enter any 5 number=\n");

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

Use scanf command for reading numbers

Then use for loop for reversing the entered numbers

And print out the output using printf command

stop

34.Aim: write a program for finding sum of all elements of an array

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++)

printf("entered number is=%d\n",num[i]);

printf("sum of entered numbers=%d\n",sum)

}
Output:
enter 5 numbers=

entered number is=1

entered number is=2

entered number is=3

entered number is=4

entered number is=5

sum of entered numbers=15

algorithm:
enter any 5 numbers

use 2 for loops

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

again use printf for printing output(sum)

stop

35.Aim: copying values of one array to other array

Syntax:
#include <stdio.h>

main()

{
int arr1[5],arr2[5],i;

printf("enter any 5 numbers:\n");

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

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

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

printf("entered number is=%d\n",arr1[i]);

printf("copying numbers of arr1[5] to arr2[5]");

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

entered number is=1

entered number is=2

entered number is=3

entered number is=4

entered number is=5

copying numbers of arr1[5] to arr2[5]numbers in arr2[i]=1

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

print out the numbers entered in first array using printf

then using for loop copy the numbers of first array to another array and

print the same numbers of array 1 to array 2 using printf

stop

36.Aim: to count total number of duplicate value in an array

Syntax:
#include <stdio.h>

int main() {

int Arr[10],i,j,count = 0;

printf("Enter Number in Array\n");


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

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

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

for(j = i+1; j <10; j++) {

if(Arr [i]== Arr [j])

count++;

break;

printf("Duplicate Element Count : %d\n", count);

return 0;

Output:

Enter Number of Elements in Array

Enter 8 numbers

1234512467

Duplicate Element Count : 3


Algorithm:
Enter the number of elements that should be entered in an array

Enter the numbers which are entered using scanf

And print it out using printf command

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

37.Aim: to merge two arrays of same size sorted in descending order

Syntax:
#include <stdio.h>

void main()

int arr1[4]={1,2,3,0}, arr2[4]={5,7,6,8}, arr3[8];

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("\nThe merged array in decending order is :\n");

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

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

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

And print out the output using printf

After merging sort the elements in arr3[] using for loop in which if cond, is used such as
arr3[k]<=arr3[k+1].

After this print out the sorted elements using printf

Stop

38.Aim:to check the frequency of each element in array

Syntax:
#include <stdio.h>

main()

int arr[10],num,i,freq=0;

printf("enter numbers in array=\n");

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

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

printf("enter number of which frequency is to be founded=\n");

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=

enter number of which frequency is to be founded=

frequency of selected number=2

algorithm:
enter numbers in an array

choose the number of which you want to find out frequency

using for loop in which if cond. Is used ie- arr[i]==num we can find out number of frequency

and print the frequency using printf

stop
39.Aim: Write a program in C to find the maximum and minimum
element in an array.

Syntax: for maximum


#include <stdio.h>

main()

int arr[5],i,max;

printf("enter numbers in an array=\n");

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];

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

Output:
enter numbers in an array=

3
4

max number is=5

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

print out the max number among all using printf

stop

for minimum
#include <stdio.h>

main()

int arr[5],i,min;

printf("enter numbers in an array=\n");

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=

min number is=1

40 Aim: Write a program in C for multiplication of two square


Matrices

Syntax:
#include <stdio.h>

main()

int arr1[2][2],arr2[2][2],arr3[2][2],i,j,k;

printf("enter numbers in first array=\n");

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

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

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

printf("enter numbers in second array=\n");


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

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

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

printf("multiplication of two square matrices=\n");

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=

enter numbers in second array=

multiplication of two square matrices=

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;

printf("enter numbers in an array=\n");

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

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

printf("choose number that you want to delete=\n");

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--;

}
}

printf("array after removing element=\n");

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

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

Output:
enter numbers in an array=

choose number that you want to delete=

array after removing element=

124

Algorithm:
enter numbers in an array

choose number that you want to delete

select index(index will be the last unit of memory in an array)

now compare the selected number by the user with a[i] using if cond.ie- arr[i]==key

and after checking if cond. Is true this will be executed arr[j-1]=arr[j]

now finally print out the result using printf cond.

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;

printf("enter numbers in matrix=\n");

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=

transpose matrix element =5


transpose matrix element =20

transpose matrix element =35

transpose matrix element =10

transpose matrix element =25

transpose matrix element =40

transpose matrix element =15

transpose matrix element =30

transpose matrix element =45

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

43.Aim: Write a program in C to find sum of right diagonals of a


matrix.

Syntax:
#include <stdio.h>

main()

int arr[3][3],i,j,sum=0;

printf("enter elements in array=\n");

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]

using printf command we can print the output

stop

44.Aim: Write a program in C to calculate determinant of a 3 x 3


matrix.

Syntax:
#include <stdio.h>

main()

int a[3][3],i,j,r1,r2,r3,deter;

printf("enter elements in an array=\n");

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]));

r2 = a[0][1] * ((a[1][0] * a[2][2])

- (a[2][0] * a[1][2]));

r3 = a[0][2] * ((a[1][0] * a[2][1])

- (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

for solving the matrix use equation : r1 = a[0][0] * ((a[1][1] * a[2][2])

- (a[2][1] * a[1][2]));

r2 = a[0][1] * ((a[1][0] * a[2][2])

- (a[2][0] * a[1][2]));

r3 = a[0][2] * ((a[1][0] * a[2][1])

- (a[2][0] * a[1][1]));

deter= r1 - r2 + r3;

and print out the result using print f function

stop

45.Aim: Write a program in C to accept a matrix and determine


whether it is a sparse matrix.(Sparse matrix is a matrix which
contains very few non-zero elements. check for the same and find
more info on its application )
Syntax:
#include <stdio.h>

main()

int rows, cols, size, count = 0;


int a[][3] = {

{4, 0, 0},

{0, 5, 0},

{0, 0, 6}

};

rows = (sizeof(a)/sizeof(a[0]));

cols = (sizeof(a)/sizeof(a[0][0]))/rows;

size = rows * cols;

for(int i = 0; i< rows; i++){

for(int j = 0; j < cols; j++){

if(a[i][j] == 0)

count++;

if(count > (size/2))

printf("Given matrix is a sparse matrix");

else

printf("Given matrix is not a sparse matrix");


}

Output:
Given matrix is a sparse matrix

Algorithm:
Declare and initialize a two-dimensional array a.

Given matrix is a sparse matrix

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.

Else, the matrix is not a sparse matrix.

Stop

46.Aim:. Write a program in C to find two elements whose sum is


closest to zero. (hint The given array is : 38 44 63 -51 -35 19 84 -69 4 -
46 The Pair of elements whose sum is minimum are: [44, -46])

Syntax:
#include <stdio.h>

#include <math.h>

#include <stdlib.h>

void findMinSumPair(int *arr1, int arr_size)

int i, j, sum, minSum, min1Pair, min2Pair;


if(arr1 == NULL || arr_size < 2)

return;

min1Pair = arr1[0];

min2Pair = arr1[1];

minSum = min1Pair + min2Pair;

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

for(j = i+1; j < arr_size; j++)

sum = arr1[i] + arr1[j];

if(abs(sum) < abs(minSum))

minSum = sum;

min1Pair = arr1[i];

min2Pair = arr1[j];

printf("[%d, %d]\n", min1Pair, min2Pair);

int main()

int arr1[] = {38, 44, 63, -51, -35, 19, 84, -69, 4, -46};

int ctr = sizeof(arr1)/sizeof(arr1[0]);


int i;

//------------- print original array ------------------

printf("The given array is : ");

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

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

printf("\n");

//------------------------------------------------------

printf("The Pair of elements whose sum is minimum are: \n");

findMinSumPair(arr1, ctr);

return 0;

Output:
The given array is : 38 44 63 -51 -35 19 84 -69 4 -46

The Pair of elements whose sum is minimum are:

[44, -46]

Algorithm:

47.Aim: Write a program in C to count the number of triangles can be


fromed from a given array.(hint The given array is : 6 18 9 7 10
Number of possible triangles can be formed from the array is: 5)

Syntax:
#include <stdio.h>

#include <stdlib.h>
int compare(const void* one, const void* two)

return *(int*)one > *(int*)two;

int CountNumberOfTriangles (int *arr1, int arr_size)

int ctrTriangle = 0, i, j, k;

qsort(arr1, arr_size, sizeof(int), compare);

for(i = 0; i < arr_size-2; ++i)

for (j = i+1; j < arr_size; ++j)

k = j +1;

while (k < arr_size && (arr1[i] + arr1[j])> arr1[k])

k++;

ctrTriangle += k - j - 1;

return ctrTriangle;

int main()

int arr1[] = {6, 18, 9, 7, 10};


int n = sizeof(arr1)/sizeof(arr1[0]);

int i;

printf("The given array is : ");

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

printf("%d ", arr1[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

Following are steps to count triangle.

the array in ascending order.

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.

sum up for all valid pairs of i and j where i< j

stop

48.Aim: Write a program in C to find the largest sum of contiguous


subarray of an array.

Syntax:
include <stdio.h>

int maxSum(int a[],int n)

int i,j,k;

int sum,maxSum = 0;

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

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

sum = 0;

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

sum = sum + a[k];

if(sum>maxSum)

maxSum = sum;

}
return maxSum;

int main()

int i;

int arr1[] = {8, 3, 8, -5, 4, 3, -4, 3, 5};

int ctr = sizeof(arr1)/sizeof(arr1[0]);

printf("The given array is : ");

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

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

printf("\n");

printf("The largest sum of contiguous subarray is : %d \n", maxSum(arr1, ctr));

return 0;

Output:
The given array is : 8 3 8 -5 4 3 -4 3 5

The largest sum of contiguous subarray is : 21

49.Aim:addition of two numbers without arithmetic operator

Syntax:
#include <stdio.h>
main()

int a,b,sum;

printf("enter two numbers=\n");

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 ‘+’

and printf is used to get output on the screen

stop

50. Aim: Multiply an Integer Number by 2 Without Using


Multiplication Operator

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

using eq. n<<1we can multiply the number with 2

and print the output using printf

stop

51.Aim: Divide an Integer Number by 2 Without Using division


Operator

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

use eq. n>>1 to find the result

and print out the output using printf

stop

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy