0% found this document useful (0 votes)
1 views57 pages

c Programming Lab File 24bec025[1] (1)

Uploaded by

dshouryakiran
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)
1 views57 pages

c Programming Lab File 24bec025[1] (1)

Uploaded by

dshouryakiran
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
You are on page 1/ 57

++ FORMAT OF THE FILE IS :-

 CODEoutput and
input.
 ALGORITHM

:>BY DESH RAJ 24BEC025

11/19/2024 File by – 24bec025


*1.

1. Start.

2. Declare a variable to represent the data type for which you want
to find the size.

3. Use the size of operator to determine the number of bytes


required for the data type.

a) For example, size = size of(data_type);


4. Print the result to the screen to display the number of bytes.

5. End.

*2.

11/19/2024 File by – 24bec025


1. Start.
2. Declare a character variable to store the character for which you want to find the
ASCII code.

3. Read a character from the user and store it in charVar.


4. Use the %d format specifier in a printf statement to display the ASCII code of
charVar.

a) For example, printf("ASCII code of '%c' is: %d\n", charVar, (int)charVar);

5. End.

3.

1. Start.
2. Read a number from the user.

3. Initialize a variable sum to 0.


4. Repeat the following until the number becomes 0:

a) Extract the rightmost digit by taking the number modulo 10 and store it in a
variable
digit.

b) Add digit to sum.


c) Remove the rightmost digit by performing an integer division by 10 on the
number.

1. Print the value of sum, which is the sum of the digits.

2. End.

11/19/2024 File by – 24bec025


4.

1. Start.

2. Read a number from the user.


3. Initialize a variable reversed to 0.

4. Repeat the following until the number becomes 0:


a) Extract the rightmost digit by taking the number modulo 10
and store it in a variable
digit.
b) Add digit to reversed after multiplying reversed by 10.

c) Remove the rightmost digit by performing an integer division


by 10 on the number.

1. Print the value of reversed, which is the reversed number.


2. End.

5.

1. Start.

2. Take digits as 5 and 10

3. First make a as addition of a and


b.

4. After that make b as subtraction


of b from a.

5. After that make a as subtraction


of b from a.

6. End .

11/19/2024 File by – 24bec025


6.

1. Start.

2. Read a number from the user and store it in a


variable number.
3. Check if number is divisible by 2.

a) If it is, print "The number is even."

b) If it's not, print "The number is odd."

4. End.

7.

1. Start.

2. Read the age of a person from the user and store it in


a variable age.
3. Check if age is greater than or equal to the legal
voting age (usually 18).

a) If it is, print "The person is eligible to vote."

b) If it's not, print "The person is not eligible to


vote."

4. End.

11/19/2024 File by – 24bec025


8.

1. Start.

2. Read three numbers from the user and store them in variables
num1, num2, and num3.

3. Initialize a variable max to the value of num1.

4. Compare max with num2 and update max if num2 is greater.

5. Compare max with num3 and update max if num3 is greater.

6. Print the value of max, which is the greatest among the three
numbers.
7. End.

9.

1. Start.
2. Read an integer from the user and store it in a variable
day Number.
3. Create a set of strings or an array of strings to represent the
days of the week.
4. Check if day Number is within the range 1 to 7.
5. If it is, use day Number to index the day name from the
array.
6. If it's not, print an error message indicating that the input
is out of range.
7. Print the name of the day.
8. End.

11/19/2024 File by – 24bec025


10.

1. Start.
2. Read the marks of a student from the user.
3. Calculate the percentage by dividing the marks by the maximum
marks and multiplying by 100.
4. Determine the grade based on the percentage:
5. If percentage is greater than or equal to 90, assign the grade "A+."
6. If it's between 80 and 89, assign the grade "A."
7. If it's between 70 and 79, assign the grade "B."
8. If it's between 60 and 69, assign the grade "C."
9. If it's less than 60, assign the grade "F."
10.Print the percentage and the corresponding grade.
11.End.

11.

1. Start.

2. Read a year from the user.

3. Check if the year is divisible by 4.

a) If yes, continue to the next step.

b) If no, it's not a leap year; print "Not a leap


year" and stop.

4. Check if the year is divisible by 100.

a) If yes, continue to the next step.

b) If no, it's a leap year; print "Leap year" and


stop.
5. Check if the year is divisible by 400.

11/19/2024 File by – 24bec025


a) If yes, it's a leap year; print "Leap year" and
stop.

b) If no, it's not a leap year; print "Not a leap


year" and stop.

6. End.

12.

1. Start.

2. Read two numbers, num1 and num2, from the user.

3. Compare num1 and num2.

a) If num1 is greater than num2, print "num1 is


greater than num2."
b) If num1 is equal to num2, print "num1 is equal to
num2."

c) If num1 is smaller than num2, print "num1 is


smaller than num2."
4. End.

13.

1. Start.

2. Read a lowercase character from the user and store it in


a variable charVar.

3. Subtract 32 from the ASCII value of charVar to convert it


to uppercase.

4. Print the value of charVar, which is the uppercase


character.
5. End.

11/19/2024 File by – 24bec025


14.

1. Start.

2. Read a temperature in Celsius from the user and store it


in a variable celsius.

3. Calculate the equivalent temperature in Fahrenheit


using the formula:

a) fahrenheit = (celsius * 9/5) + 32;

4. Print the value of fahrenheit, which is the temperature in


Fahrenheit.
5. End.

15.

1. Start.

2. Read two numbers, num1 and num2, from the user.

3. Read an operator (+, -, *, /) from the user and store it in a


variable operator.

4. Perform the operation based on the operator:

a) If operator is '+', calculate the sum of num1 and


num2.

b) If operator is '-', calculate the difference between


num1 and num2.
c) If operator is '*', calculate the product of num1 and
num2.

d) If operator is '/', calculate the division of num1 by


num2.
5. Print the result of the operation.

6. End.
11/19/2024 File by – 24bec025
16.

1. Start.

2. Initialize a variable num to 1.

3. Create a loop that iterates from


num to 10.

a) Print the value of num.

b) Increment num by 1.

4. Continue the loop until num


reaches 10.
5. End.

17.

1. Start.

2. Read an integer n from the user.

3. Initialize a variable sum to 0 to store the sum of even


numbers.

4. Create a loop that iterates from 2 to n with a step size of


2.
a) For each iteration, add the current number to
sum.

5. After the loop, sum will contain the sum of even


numbers between 1 and n.
6. Print the value of sum.

7. End.

11/19/2024 File by – 24bec025


18.

1. Start.
2. Read an integer from the user and store it in a variable n, which is
the number for which you want to print the multiplication table.

3. Create a loop that iterates from 1 to 10.

a) For each iteration, calculate the product of n and the loop


variable.

b) Print the multiplication expression

4. Continue the loop until the loop variable reaches 10.

5. End.

19.

1. Start.
2. Read an integer from the user and store it in a variable n, where n is
the number for which you want to calculate the factorial.

3. Initialize a variable factorial to 1.

4. Create a loop that iterates from 1 to n.


5. After the loop, factorial will contain the factorial of n.

6. Print the value of factorial.


7. End.

11/19/2024 File by – 24bec025


20.

1. Start.
2. Read two integers from the user and store them in variables m and n,
where m is the starting number and n is the ending number.

3. Initialize a variable sum to 0 to keep track of the sum.

4. Create a loop that iterates from m to n, inclusive.

5. After the loop, sum will contain the sum of all numbers between m
and n.

6. Print the value of sum.

7. End.

21.

1. Start.

2. Read a number from the user and store it in a variable number.


3. Repeat the following until number becomes 0:

a) Extract the rightmost digit of number and store it in a variable


digit.

b) Print the value of digit.

c) Remove the rightmost digit from number.

4. Continue the loop until number becomes 0.

5. End

11/19/2024 File by – 24bec025


22.

23.

1. Start.
2. Read a number from the user.

3. Initialize a variable reversed to 0.


4. Repeat the following until the number becomes 0:

a) Extract the rightmost digit by taking the number modulo 10 and store it in a
variable
digit.

b) Add digit to reversed after multiplying reversed by 10.

c) Remove the rightmost digit by performing an integer division by 10 on the


number.
1. Print the value of reversed, which is the reversed number.

2. End.

24.

1. Start.
2. Read a number from the user and store it in a variable
number.

11/19/2024 File by – 24bec025


3. Create a copy of the original number and store it in a variable originalNumber.

4. Initialize a variable reversedNumber to 0.

5. Repeat the following until the number becomes 0:

a) Extract the rightmost digit by taking number modulo 10 and store it in a


variable
digit.

b) Add digit to reversedNumber after multiplying reversedNumber by

c) Remove the rightmost digit by performing an integer division by 10 on


number.

3. Compare originalNumber with reversedNumber.

a) If they are equal, print "The number is a palindrome."

b) If they are not equal, print "The number is not a palindrome."

4. End.
25.

1. Start.
2. Read the starting year 'm' and the ending year 'n' from
the user.

3. Print "Leap years between m and n."


4. Initialize a variable year to 'm'.

5. Repeat the following until year is less than or equal to 'n':


a) Check if year is a leap year:

If it is, print year.


b) Increment year by 1.

1. End.

11/19/2024 File by – 24bec025


26.

1. Start
.
2. Include the necessary header file for input and output
operations.

3. Define the main function.

4. Inside the main function:

a) Initialize an integer variable i to 1.

b) Start a for loop with the condition i less than or


equal to 10.
c) End of the for loop.

5. Return 0 to indicate successful program execution.


6. End of the program.

27.

1. Start.

2. Include the necessary header file for input and output


operations.

3. Define the main function.


4. Inside the main function: a. Declare integer variables n (for
the user-specified number) and sum (for the sum of integers).
b. Prompt the user to enter a positive integer and store it in
the n variable. c. Check if n is less than or equal to 0:

11/19/2024 a) d. Initialize sum to 0. e. Start a for loop with the


condition i less than or equal to n: File by – 24bec025
b) Add the value of i to sum in each iteration. f. End of the for loop. g. Print the
sum of integers from 1 to n.

5. Return 0 to indicate successful program execution.

6. End

28.

1. Start.
2. Include the necessary header files for input and output operations and
mathematical functions.

3. Define the main function.


4. Inside the main function: a. Declare a double variable num (for the user-entered
number) and squareRoot (for the square root result). b. Prompt the user to enter a
positive number and store it in the num variable. c. Check if num is less than 0:

a) If true, display an error message and exit the program with an error
code. d. Calculate the square root of num using the sqrt function and
store it in the squareRoot variable. e. Print the square root of the
entered number.

5. Return 0 to indicate successful program execution.

6. End

11/19/2024 File by – 24bec025


29.

ALGORITHM:-
1. Start
2. Declare functions:
o Function 1: isPrime(int num):
 This function checks if a number num is prime:
 If num <= 1, return false (since numbers less than or equal to 1
are not prime).
 For i = 2 to i <= sqrt(num):
 If num % i == 0, return false (since num is divisible by i, it's
not prime).
 Return true (if no divisors found, num is prime).
3. Declare variables:
o Declare a variable n to store the number.
o Declare a variable i for iteration (to check potential prime numbers).
o Declare a boolean found and initialize it to false (to track if a valid pair of
primes is found).
4. Input:
o Read the value of n (the number) from the user.
5. Check if the number can be expressed as the sum of two primes:
o For i = 2 to i <= n / 2:
 If isPrime(i) returns true (i is prime):
 Check if n - i is also prime by calling isPrime(n - i).
 If isPrime(n - i) is true, set found = true and break the loop (since
n = i + (n - i) can be expressed as the sum of two primes).
6. Output:
o If found == true:
 Print "The number can be expressed as the sum of two prime
numbers".
o Else, print "The number cannot be expressed as the sum of two prime
numbers".
7. End
11/19/2024 File by – 24bec025
30.

ALGORITHM:-
1. Start
2. Declare functions:
o Function 1: isPrime(int num)
 This function checks if a number num is prime:
 If num <= 1, return false (since numbers less than or equal to 1
are not prime).
 For i = 2 to i <= sqrt(num):
 If num % i == 0, return false (since num is divisible by i, it's
not prime).
 Return true (if no divisors found, num is prime).
o Function 2: isArmstrong(int num)
 This function checks if a number num is an Armstrong number:
 Store the original value of num in original.
 Find the number of digits numDigits in num.
 Initialize sum = 0.
 For each digit in num:
 Extract the last digit digit = num % 10.
 Add digit^numDigits to sum.
 Remove the last digit: num = num / 10.
 If sum == original, return true (the number is Armstrong).
 Otherwise, return false.
3. Declare variables:
o Declare a variable num to store the user input number.
o Declare a variable choice to allow the user to choose whether to check for
prime or Armstrong number.
4. Input:
o Prompt the user to choose between checking for a Prime number or an
Armstrong number.
o Read the value of choice from the user.
o Read the value of num from the user.
5. Check the number type:

11/19/2024 File by – 24bec025


31.
ALGORITHM:-
1. Start

2. Declare function:

o Define a recursive function sumOfNaturalNumbers(int n):

 Base Case: If n == 1, return 1 (since the sum of the first natural number is 1).
 Recursive Case: Else, return n + sumOfNaturalNumbers(n - 1) (add the current number
n to the sum of the previous numbers).

3. Declare variables:

o Declare a variable n to store the value up to which the sum is to be calculated.

4. Input:

o Read the value of n from the user (the upper limit of natural numbers).

5. Check validity:

o If n <= 0, print an error message: "Please enter a positive integer".

6. Call the recursive function:

o Call the function sumOfNaturalNumbers(n) to calculate the sum.

7. Output:

o Print the result returned from the function.

8. End

11/19/2024 File by – 24bec025 Made by :-24bec025


32.
ALGORITHM:-
1. Start
2. Declare function:
o Define a recursive function factorial(int n):
 Base Case: If n == 0 or n == 1, return 1 (since 0! = 1 and 1! = 1).
 Recursive Case: Else, return n * factorial(n - 1) (multiply the current
number n with the factorial of n - 1).
3. Declare variables:
o Declare a variable n to store the number whose factorial is to be calculated.
4. Input:
o Read the value of n from the user (the number for which factorial is required).
5. Check validity:
o If n < 0, print an error message: "Factorial is not defined for negative
numbers".
6. Call the recursive function:
o Call the function factorial(n) to calculate the factorial of n.
7. Output:
o Print the result returned from the function.
8. End

11/19/2024 File by – 24bec025


33.

ALGORITHM:-
1. Start
2. Declare function:
o Define a recursive function gcd(int a, int b):
 Base Case: If b == 0, return a (since the GCD of a and 0 is a).
 Recursive Case: Else, return gcd(b, a % b) (call the function recursively
with the second number b and the remainder of a divided by b).
3. Declare variables:
o Declare two variables a and b to store the two numbers for which the GCD is
to be calculated.
4. Input:
o Read the values of a and b from the user (the two numbers for whichGCD is
required).
5. Call the recursive function:
o Call the function gcd(a, b) to calculate the GCD of a and b.
6. Output:
o Print the result returned from the function (the GCD of a and b).
7. End

11/19/2024 File by – 24bec025


34.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare a variable binary to store the binary number.
o Declare a variable decimal to store the result in decimal.
o Declare a variable position (to keep track of the power of 2, starting from 0).
3. Input:
o Read the binary number binary from the user (as a string or integer).
4. Convert Binary to Decimal:
o Initialize decimal = 0 and position = 0.
o While binary > 0:
 Get the last digit (least significant bit) of the binary number: digit =
binary % 10.
 Add digit * (2^position) to decimal (i.e., decimal += digit * pow(2,
position)).
 Remove the last digit of binary: binary = binary / 10.
 Increment position by 1.
5. Output:
o Print the decimal value of the binary number.
6. End

11/19/2024 File by – 24bec025


35.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare a variable n to store the number to be checked.
o Declare a variable original to store the original value of n.
o Declare a variable sum and initialize it to 0 to store the sum of the
digits raised to the power of the number of digits.
o Declare a variable numDigits to store the number of digits in n.
o Declare a variable digit to store the individual digits of n.
3. Input:
o Read the value of n from the user.
4. Find the number of digits:
o Set original = n (to preserve the original number).
o Set numDigits = 0.
o While n > 0:
 Increment numDigits by 1.
 Update n = n / 10 (remove the last digit).
5. Calculate the Armstrong sum:
o Set n = original (restore the original value of n).
o While n > 0:
 Extract the last digit: digit = n % 10.
 Add the power of digit raised to numDigits to sum (i.e., sum =
sum + (digit^numDigits)).
 Remove the last digit from n: n = n / 10.
6. Check if the number is an Armstrong number:
o If sum == original, then the number is an Armstrong number.
 Print "The number is an Armstrong number".
o Else, the number is Not an Armstrong number.
11/19/2024 File by – 24bec025
 Print "The number is not an Armstrong number".
36.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare two variables low and high to store the range (the lower and upper limits of
the interval).
o Declare variables numDigits, digit, sum, and original for calculations related to
Armstrong numbers.
o Declare a variable i to iterate through each number in the given interval.
3. Input:
o Read the values of low and high from the user (the interval limits).
4. Adjust the lower bound:
o If low <= 1, set low = 2 (since Armstrong numbers start from 2).
5. Loop through the interval:
o For i = low to i <= high:
 Store the value of i in original.
 Set sum = 0 (to store the sum of the Armstrong digits).
 Set numDigits = 0 (to store the number of digits in i).
 Find the number of digits:
 While i > 0:
 Increment numDigits by 1.
 Update i = i / 10 (remove the last digit).
 Calculate the Armstrong sum:
 Set i = original (restore the original value of i).
 While i > 0:
 Extract the last digit: digit = i % 10.
 Add digit^numDigits to sum (i.e., sum = sum +
(digit^numDigits)).
 Remove the last digit: i = i / 10.
 Check if the number is Armstrong:
 If sum == original, print original (since it is an Armstrong number).
6. End
11/19/2024 File by – 24bec025
37.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare a variable num1 to store the first number.
o Declare a variable num2 to store the second number.
o Declare a variable operation to store the arithmetic operation choice (e.g., +, -,
*, /).
o Declare a variable result to store the result of the calculation.
3. Input:
o Read the values of num1, num2, and operation (the arithmetic operation)
from the user.
4. Use switch...case to perform the operation:
o Use a switch statement to check the value of operation:
 Case +:
 Calculate result = num1 + num2.
 Print the result.
 Case -:
 Calculate result = num1 - num2.
 Print the result.
 Case *:
 Calculate result = num1 * num2.
 Print the result.
 Case /:
 If num2 == 0, print "Error: Division by zero" (handle division by
zero).
 Else, calculate result = num1 / num2.
 Print the result.
 Default:
 Print "Invalid operation" (if the user enters an invalid operation).
5. End
11/19/2024 File by – 24bec025
38.
ALGORITHM:-
1. Start
2. Declare function:
o Declare a function isPrime(int num) that checks if a given number num is
prime:
 If num <= 1, return false (since numbers less than or equal to 1 are not
prime).
 For i = 2 to i <= sqrt(num):
 If num % i == 0, return false (since num is divisible by i, it's not
prime).
 Return true (if no divisors found, num is prime).
3. Declare variables:
o Declare variables low and high to store the lower and upper limits of the
interval.
o Declare a variable i for iteration.
4. Input:
o Read the values of low and high (the interval limits) from the user.
5. Display prime numbers:
o For i = low to i <= high:
 If isPrime(i) returns true, print i (since it is a prime number).
6. End

11/19/2024 File by – 24bec025


39.

ALGORITHM:-
1. Start
2. Declare functions:
o Function 1: isPrime(int num)
 This function checks if a number num is prime:
 If num <= 1, return false (since numbers less than or equal to 1
are not prime).
 For i = 2 to i <= sqrt(num):
 If num % i == 0, return false (since num is divisible by i, it's
not prime).
 Return true (if no divisors found, num is prime).
o Function 2: isArmstrong(int num)
 This function checks if a number num is an Armstrong number:
 Store the original value of num in original.
 Find the number of digits numDigits in num.
 Initialize sum = 0.
 For each digit in num:
 Extract the last digit digit = num % 10.
 Add digit^numDigits to sum.
 Remove the last digit: num = num / 10.
 If sum == original, return true (the number is Armstrong).
 Otherwise, return false.
3. Declare variables:
o Declare a variable num to store the user input number.
o Declare a variable choice to allow the user to choose whether to check for
prime or Armstrong number.
4. Input:
o Prompt the user to choose between checking for a Prime number or an
Armstrong number.
o Read the value of choice from the user.
o Read the value of num from the user.
5. Check the number type:

11/19/2024 File by – 24bec025


40.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare a variable octal to store the octal number.
o Declare a variable decimal to store the result in decimal.
o Declare a variable position to keep track of the power of 8, starting from 0.
3. Input:
o Read the octal number octal from the user.
4. Convert Octal to Decimal:
o Initialize decimal = 0 and position = 0.
o While octal > 0:
 Get the last digit (least significant digit) of the octal number: digit =
octal % 10.
 Add digit * (8^position) to decimal (i.e., decimal += digit * pow(8,
position)).
 Remove the last digit of octal: octal = octal / 10.
 Increment position by 1.
5. Output:
o Print the decimal value of the octal number.
6. End
2. Decimal to Octal Conversion:
Steps:
1. Start
2. Declare variables:
o Declare a variable decimal to store the decimal number.
o Declare a variable octal to store the result in octal.
o Declare a variable position to keep track of the place value for constructing
the octal number.
3. Input:
o Read the decimal number decimal from the user.
4. Convert Decimal to Octal:
11/19/2024 File by – 24bec025
o Initialize octal = 0.
41.

1. Start
2. Declare variables:
o Declare a variable binary to store the binary number.
o Declare a variable octal to store the resulting octal number.
o Declare a variable decimal to store the decimal equivalent of the binary number
(intermediate step).
3. Input:
o Read the binary number binary from the user.
4. Convert Binary to Decimal:
o Initialize decimal = 0 and position = 0.
o While binary > 0:
 Get the last digit (least significant bit) of the binary number: digit = binary % 10.
 Add digit * (2^position) to decimal (i.e., decimal += digit * pow(2, position)).
 Remove the last digit of binary: binary = binary / 10.
 Increment position by 1.
5. Convert Decimal to Octal:
o Initialize octal = 0 and position = 1.
o While decimal > 0:
 Get the remainder when decimal is divided by 8: remainder = decimal % 8.
 Add remainder * position to octal.
 Multiply position by 10 (to shift the digits in the octal number).
 Update decimal = decimal / 8 (integer division).
6. Output:
o Print the octal value.
7. End
2. Octal to Binary Conversion:
Steps:
1. Start
2. Declare variables:
o Declare a variable octal to store the octal number.
o Declare a variable binary to store the resulting binary number.
o Declare a variable decimal to store the decimal equivalent of the octal number
(intermediate step).
3. Input:
o Read the octal number octal from the user.
11/19/2024 File by – 24bec025
42.
ALGORITHM:-
1. Start
2. Declare function:
o Define a recursive function reverseSentence(char *str) that reverses a given
sentence:
 Base Case: If the string is empty (i.e., str[0] == '\0'), return (do nothing, just
end the recursion).
 Recursive Case: If the string is not empty:
 Call the function reverseSentence(str + 1) (to process the rest of the
string).
 After the recursion returns, print the first character str[0].
3. Declare variables:
o Declare a character array sentence to store the sentence input by the user.
4. Input:
o Read the sentence from the user using gets() or fgets() (to allow spaces in the
sentence).
5. Call the recursive function:
o Call reverseSentence(sentence) to reverse the input sentence.
6. Output:
o The sentence will be printed in reverse as the recursion unwinds and prints the
characters.
7. End

11/19/2024 File by – 24bec025


43.

ALGORITHM:-
1. Start
2. Declare function:
o Define a recursive function power(int base, int exponent):
 Base Case:
 If exponent == 0, return 1 (since any number raised to the power of 0 is 1).
 Recursive Case:
 Else, return base * power(base, exponent - 1) (multiply the base with the
result of the power function for exponent - 1).
3. Declare variables:
o Declare variables base and exponent to store the base and exponent values.
4. Input:
o Read the base and exponent from the user.
5. Call the recursive function:
o Call the function power(base, exponent) to calculate the result.
6. Output:
o Print the result returned from the power(base, exponent) function.
7. End

11/19/2024 File by – 24bec025


44.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare an integer array arr[] to store n numbers.
o Declare a variable sum to accumulate the sum of the numbers.
o Declare a variable n to store the number of elements in the array.
o Declare a variable average to store the calculated average.
3. Input:
o Read the value of n (number of elements) from the user.
o Input n elements into the array arr[].
4. Calculate the sum:
o Initialize sum = 0.
o For i = 0 to i < n:
 Add the value of arr[i] to sum (i.e., sum += arr[i]).
5. Calculate the average:
o If n > 0, calculate the average as average = sum / n (since the average is the
sum of the elements divided by the number of elements).
o If n == 0, print an error message stating "Cannot calculate average for zero
elements".
6. Output:
o Print the value of average.
7. End

11/19/2024 File by – 24bec025


45.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare an integer array arr[] to store the elements of the array.
o Declare an integer variable n to store the number of elements in the array.
o Declare an integer variable largest to store the largest element in the array.
3. Input:
o Read the value of n (number of elements in the array).
o Input n elements into the array arr[].
4. Initialize the largest element:
o Initialize largest as the first element of the array: largest = arr[0].
5. Find the largest element:
o For i = 1 to i < n:
 If arr[i] > largest, then update largest = arr[i].
6. Output:
o Print the value of largest (the largest element in the array).
7. End

11/19/2024 File by – 24bec025


46.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare an integer array arr[] to store the elements of the dataset.
o Declare an integer variable n to store the number of elements in the array.
o Declare variables sum, mean, variance, and stdDeviation to hold intermediate results.
3. Input:
o Read the value of n (the number of elements in the dataset).
o Input n elements into the array arr[].
4. Calculate the mean:
o Initialize sum = 0.
o For i = 0 to i < n:
 Add each element of the array to sum (i.e., sum += arr[i]).
o Calculate the mean: mean = sum / n.
5. Calculate the variance:
o Initialize variance = 0.
o For i = 0 to i < n:
 Calculate the squared difference of each element from the mean: variance +=
(arr[i] - mean) * (arr[i] - mean).
o Calculate the variance: variance = variance / n.
6. Calculate the standard deviation:
o Calculate the standard deviation: stdDeviation = sqrt(variance).
7. Output:
o Print the value of stdDeviation (the standard deviation).
8. End

11/19/2024 File by – 24bec025


47.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare two 2D arrays matrix1[][] and matrix2[][] to store the elements of the two matrices.
o Declare a 2D array result[][] to store the sum of the matrices.
o Declare integer variables i, j, rows, and cols for iterating over the elements and to store the
number of rows and columns in the matrices.
3. Input:
o Read the values of rows and cols (dimensions of the matrices).
o Input the elements of matrix1[][] and matrix2[][] from the user.
4. Initialize the result matrix:
o Initialize the result[][] matrix where each element will be the sum of the corresponding
elements in matrix1[][] and matrix2[][].
5. Add the matrices:
o For i = 0 to i < rows:
 For j = 0 to j < cols:
 Add the corresponding elements of matrix1[i][j] and matrix2[i][j] and store
the result in result[i][j] (i.e., result[i][j] = matrix1[i][j] + matrix2[i][j]).
6. Output:
o Print the result[][] matrix (the sum of the two matrices).
7. End

11/19/2024 File by – 24bec025


48.
ALGORITHM:-
1. Start
2. Declare variables:
o Declare two 2D arrays matrix1[][] and matrix2[][] to store the elements of the two
matrices.
o Declare a 2D array result[][] to store the product of the matrices.
o Declare integer variables i, j, and k for iterating over the rows and columns, and rows1,
cols1, rows2, cols2 to store the dimensions of the matrices.
3. Input:
o Read the values of rows1, cols1 (dimensions of the first matrix).
o Read the values of rows2, cols2 (dimensions of the second matrix).
o Ensure that cols1 == rows2, as matrix multiplication is only possible when the number of
columns in the first matrix equals the number of rows in the second matrix.
o Input the elements of matrix1[][] and matrix2[][] from the user.
4. Initialize the result matrix:
o Initialize the result[][] matrix to store the product of the two matrices. Set each element
to 0 initially.
5. Multiply the matrices:
o For i = 0 to i < rows1 (iterate through the rows of matrix1):
 For j = 0 to j < cols2 (iterate through the columns of matrix2):
 Initialize result[i][j] = 0 (reset the element of the result matrix before
calculating).
 For k = 0 to k < cols1 (iterate through the columns of matrix1 and rows of
matrix2):
 Perform the multiplication and accumulate the result:
result[i][j] += matrix1[i][k] * matrix2[k][j].
6. Output:
o Print the result[][] matrix, which contains the product of matrix1 and matrix2.
7. End

11/19/2024 File by – 24bec025


49.

ALGORITHM:-
1. Input the Matrix:
o Let the given matrix be AAA of size M×NM \times NM×N, where MMM is the number of
rows and NNN is the number of columns.
2. Create a New Matrix:
o Create a new matrix BBB of size N×MN \times MN×M, where NNN is the number of
rows and MMM is the number of columns (the transpose will have its rows and columns
swapped).
3. Transpose the Matrix:
o For each element A[i][j]A[i][j]A[i][j] of matrix AAA, place it in the matrix BBB at position
B[j][i]B[j][i]B[j][i].
4. Display the Transposed Matrix:
o After filling the transposed matrix BBB, print matrix BBB.
5. End.

11/19/2024 File by – 24bec025


50.
ALGORITHM:-
1. Input Matrices:
o Let the first matrix be AAA of size M×NM \times NM×N (M rows and N columns).
o Let the second matrix be BBB of size N×PN \times PN×P (N rows and P columns).
o The result of multiplying these two matrices will be stored in a matrix CCC, which will have the
size M×PM \times PM×P.
2. Matrix Multiplication Condition:
o Matrix multiplication is possible if and only if the number of columns in matrix AAA (i.e., NNN) is
equal to the number of rows in matrix BBB (i.e., NNN).
3. Define a Function for Matrix Multiplication:
o Create a function, say multiplyMatrices(A, B, C, M, N, P), which takes the matrices AAA and
BBB as input and outputs the matrix CCC (the result of the multiplication).
o The function will also take the dimensions MMM, NNN, and PPP as parameters.
4. Perform Matrix Multiplication:
o For each element C[i][j]C[i][j]C[i][j] in the result matrix CCC:
 Initialize C[i][j]=0C[i][j] = 0C[i][j]=0.
 For k=0k = 0k=0 to N−1N-1N−1:
 C[i][j]+=A[i][k]×B[k][j]C[i][j] += A[i][k] \times B[k][j]C[i][j]+=A[i][k]×B[k][j].
5. Return the Result Matrix:
o After computing the elements of matrix CCC, return the result matrix.
6. Display the Result:
o After calling the multiplication function, print the matrix CCC, which will be the product of
matrices AAA and BBB.
7. End.

11/19/2024 File by – 24bec025


51.

ALGORITHM:-
1. Declare an Array:
o Declare an array of size NNN, e.g., int arr[N];, where NNN is the number of elements in the
array.
2. Initialize the Array:
o Initialize the array with values, either manually or by input from the user.
3. Declare a Pointer:
o Declare a pointer that will be used to access the elements of the array, e.g., int *ptr;.
4. Assign the Address of the Array to the Pointer:
o Assign the address of the first element of the array to the pointer using the array name: ptr =
arr;.
5. Access Array Elements Using the Pointer:
o To access the array elements using the pointer:
 For the iii-th element of the array, use the pointer as *(ptr + i) or ptr[i].
 Both expressions will refer to the value stored at the iii-th position of the array.
6. Traverse the Array Using the Pointer:
o Use a loop (e.g., a for loop) to iterate over the array elements by incrementing the pointer and
accessing the values:
 For each iteration, print the value: printf("%d ", *(ptr + i)); or printf("%d ", ptr[i]);.
7. End.

11/19/2024 File by – 24bec025


52.

ALGORITHM:-
Algorithm: To Swap Numbers in Cyclic Order Using Call by Reference in C
1. Input Three Numbers:
o Declare three variables to hold the numbers, e.g., int a, b, c;.
o Input values for these three variables from the user.
2. Function Declaration:
o Declare a function swapCyclic(int *x, int *y, int *z) that will swap the numbers in a cyclic order
using call by reference.
3. Call by Reference:
o In C, call by reference is achieved by passing the addresses (pointers) of the variables to the
function. This allows the function to directly modify the values of the original variables.
4. Inside the Swap Function:
o Use a temporary variable to swap the numbers in a cyclic manner:
 Store the value of *x (a) in a temporary variable.
 Set *x = *y (a = b).
 Set *y = *z (b = c).
 Set *z = temp (c = a).
5. Return Control to Main:
o After the swap, return control back to the main function where the swapped values can be
printed.
6. Display the Swapped Numbers:
o In the main function, after the function call, print the values of a, b, and c to display the numbers
after swapping in cyclic order.
End

11/19/2024 File by – 24bec025


53.
ALGORITHM:-
Algorithm: To Find the Largest Number Using Dynamic Memory Allocation in C
1. Input the Size of the Array:
o Ask the user to input the number of elements, say n, that they want to store in the array.
2. Dynamically Allocate Memory for the Array:
o Use malloc or calloc to dynamically allocate memory for the array that will store n elements:
o If memory allocation fails, print an error message and exit the program.
3. Input the Array Elements:
o Use a loop to input n numbers into the dynamically allocated array.
4. Find the Largest Number:
o Initialize a variable, say largest, with the first element of the array:
o Traverse the array using a loop:
 Compare each element with the current largest number:
5. Display the Largest Number:
o After the loop completes, print the value of largest which will hold the largest element in the
array.
6. Free the Dynamically Allocated Memory:
o After finding the largest number, free the memory allocated for the array:
7. End.

11/19/2024 File by – 24bec025


54.

ALGORITHM:-
1. Input the Number of Strings:
o Declare an integer variable n to store the number of strings to be sorted.
o Input the value of n from the user (number of strings to be sorted).
2. Input the Strings:
o Declare an array of strings (2D character array), e.g., char arr[n][MAX]; where
MAX is the maximum length of each string.
o Use a loop to input n strings from the user into the array.
3. Sorting the Strings Lexicographically:
o Use a sorting algorithm like Bubble Sort or Selection Sort to sort the strings in
lexicographical order.
 Bubble Sort Approach:
 Compare each string with the next string (using strcmp() function).
 If the current string is greater than the next string, swap them.
 Repeat this process for all th e strings in the array until the array is sorted.
4. Display the Sorted Strings:
o After sorting, use a loop to print the strings in lexicographical order:
5. End.

11/19/2024 File by – 24bec025


55.

ALGORITHM:-
1. Input the String:
o Declare a character array (string) to store the input string, e.g., char str[MAX]; where
MAX is the maximum size of the string.
o Input the string from the user using scanf or fgets.
2. Initialize Counters:
o Declare and initialize variables to store the count of vowels, consonants, digits, spaces,
and other characters:
3. Traverse the String:
o Use a loop to traverse each character of the string.
o For each character, check its type (vowel, consonant, digit, space, or other):
 Vowels: Check if the character is one of the vowels ('a', 'e', 'i', 'o', 'u', and their
uppercase counterparts).
 Consonants: Check if the character is an alphabet letter (A-Z or a-z) but not a
vowel.
 Digits: Check if the character is a numeric digit ('0' to '9').
 Spaces: Check if the character is a space.
 Other Characters: If the character does not fit into any of the above categories,
count it as "other".
4. Display the Counts:
o After traversing the entire string, print the counts of vowels, consonants, digits, spaces,
and other characters:
5. End.

11/19/2024 File by – 24bec025


56.

ALGORITHM:-
Algorithm: To Add Two Complex Numbers by Passing Structure to a Function in C
1. Define the Structure:
o Define a structure Complex that represents a complex number. The structure will have
two members:
 real to store the real part of the complex number (float or double).
 imag to store the imaginary part of the complex number (float or double).
2. Declare Structure Variables:
o Declare two variables of type struct Complex to represent the two complex numbers,
say complex1 and complex2.
3. Declare a Function to Add Complex Numbers:
o Declare a function addComplexNumbers(struct Complex c1, struct Complex c2) that
takes two complex numbers as parameters (passed by value) and returns the sum of
the two complex numbers as a struct Complex.
4. Input the Complex Numbers:

o Input the real and imaginary parts for both complex numbers:
 Input the real and imaginary parts of the first complex number (complex1).
 Input the real and imaginary parts of the second complex number (complex2).
5. Call the Function to Add Complex Numbers:
o Call the addComplexNumbers() function to add the two complex numbers. Pass
complex1 and complex2 as arguments, and store the result in a struct Complex
variable, say sum.
6. Define the Function to Add Complex Numbers:
o Inside the function addComplexNumbers(), add the real and imaginary parts of the two
complex numbers and return the result:
 Real part of the sum = c1.real + c2.real
 Imaginary part of the sum = c1.imag + c2.imag
7. Display the Result:
o After receiving the result from the addComplexNumbers() function, print the sum of the
two complex numbers:
8. End.

11/19/2024 File by – 24bec025


57.

ALGORITHM:-
Algorithm: To Calculate the Difference Between Two Time Periods in C
1. Define the Structure:
o Define a structure Time to store a time period. The structure will have three members:
 hours to store the hours of the time period (integer).
 minutes to store the minutes of the time period (integer).
 seconds to store the seconds of the time period (integer).
2. Declare Structure Variables:
o Declare two variables of type struct Time to represent the two time periods, say time1
and time2.
3. Input the Two Time Periods:
o Input the hours, minutes, and seconds for both time periods:
 Input the time for time1.
 Input the time for time2.
4. Convert Time to Total Seconds (for easier calculation):
o Convert both time periods into total seconds for easier comparison and subtraction:
 total_seconds1 = time1.hours * 3600 + time1.minutes * 60 + time1.seconds
 total_seconds2 = time2.hours * 3600 + time2.minutes * 60 + time2.seconds
5. Calculate the Difference in Total Seconds:
o Subtract the total seconds of time2 from time1 to get the difference in seconds:
 difference_seconds = total_seconds1 - total_seconds2
If difference_seconds is negative (i.e., if time1 is earlier than time2), take the
absolute value of the difference.
6. Convert the Difference Back to Hours, Minutes, and Seconds:

o Convert the difference in seconds back into hours, minutes, and seconds:
 hours = difference_seconds / 3600
 minutes = (difference_seconds % 3600) / 60
 seconds = difference_seconds % 60
7. Display the Difference:
o After calculating the difference, print the result in the format: X hours, Y minutes, Z
seconds.
8. End.
11/19/2024 File by – 24bec025
58.

ALGORITHM:-
1. Start
2. Declare variables:
o Declare an integer array arr[] to store the elements of the dataset.
o Declare an integer variable n to store the number of elements in the array.
o Declare variables sum, mean, variance, and stdDeviation to hold intermediate results.
3. Input:
o Read the value of n (the number of elements in the dataset).
o Input n elements into the array arr[].
4. Calculate the mean:
o Initialize sum = 0.
o For i = 0 to i < n:
 Add each element of the array to sum (i.e., sum += arr[i]).
o Calculate the mean: mean = sum / n.
5. Calculate the variance:
o Initialize variance = 0.
o For i = 0 to i < n:
 Calculate the squared difference of each element from the mean: variance +=
(arr[i] - mean) * (arr[i] - mean).
o Calculate the variance: variance = variance / n.
6. Calculate the standard deviation:

o Calculate the standard deviation: stdDeviation = sqrt(variance).


7. Output:
o Print the value of stdDeviation (the standard deviation).
8. End

11/19/2024 File by – 24bec025


59.

ALGORITHM:-
Algorithm: To Add Two Distances (in Inch-Feet System) Using Structures in C
1. Define the Structure:
o Define a structure Distance to store the distance in inches and feet. The
structure will contain two fields:
 feet to store the feet part of the distance (integer).
 inches to store the inches part of the distance (integer).
2. Declare Structure Variables:
o Declare two variables of type struct Distance, one for each distance to be
added. For example, distance1 and distance2 for storing the two distances.
3. Input the Distances:
o Input the feet and inches for both distances from the user:
 Input feet and inches for the first distance (distance1).
 Input feet and inches for the second distance (distance2).
4. Add the Distances:
o Add the inches part of both distances. If the sum exceeds 12 inches
(because 12 inches = 1 foot), convert the excess inches into feet.
o Add the feet part of both distances, including any additional feet from the
inches conversion.
o Sum of inches: total_inches = distance1.inches + distance2.inches
o If total_inches >= 12, convert the excess inches into feet and adjust the
inches.
o Sum of feet: total_feet = distance1.feet + distance2.feet + (total_inches /
12)
5. Store and Display the Result:
o Store the final sum of feet and inches in a new struct Distance variable, say
sum.
o Print the total distance in feet and inches.
6. End.
11/19/2024 File by – 24bec025
60.

ALGORITHM:-
Algorithm: To Add Two Complex Numbers by Passing Structure to a Function in C
1. Define the Structure:
o Define a structure Complex that represents a complex number. The structure will have
two members:
 real to store the real part of the complex number (float or double).
 imag to store the imaginary part of the complex number (float or double).
2. Declare Structure Variables:
o Declare two variables of type struct Complex to represent the two complex numbers,
say complex1 and complex2.
3. Declare a Function to Add Complex Numbers:
o Declare a function addComplexNumbers(struct Complex c1, struct Complex c2) that
takes two complex numbers as parameters (passed by value) and returns the sum of
the two complex numbers as a struct Complex.
4. Input the Complex Numbers:

o Input the real and imaginary parts for both complex numbers:
 Input the real and imaginary parts of the first complex number (complex1).
 Input the real and imaginary parts of the second complex number (complex2).
5. Call the Function to Add Complex Numbers:
o Call the addComplexNumbers() function to add the two complex numbers. Pass
complex1 and complex2 as arguments, and store the result in a struct Complex
variable, say sum.
6. Define the Function to Add Complex Numbers:
o Inside the function addComplexNumbers(), add the real and imaginary parts of the two
complex numbers and return the result:
 Real part of the sum = c1.real + c2.real
 Imaginary part of the sum = c1.imag + c2.imag
7. Display the Result:
o After receiving the result from the addComplexNumbers() function, print the sum of the
two complex numbers:
8. End.

11/19/2024 File by – 24bec025


61.

ALGORITHM:-
Algorithm: To Store Information of Students Using Structure in C
1. Define the Structure:
o Define a structure Student to store the information of a student. The structure will have the
following fields:

 name[]: A character array to store the student's name (string).


 roll_no: An integer to store the student's roll number.
 age: An integer to store the student's age.
 marks[]: An array to store the marks of the student in multiple subjects.
2. Declare Structure Variables:
o Declare an array of struct Student to store the information of multiple students. For example,
struct Student students[100]; to store information for up to 100 students.
3. Input Number of Students:
o Input the number of students whose information needs to be stored. Declare an integer variable
n to store the number of students.

4. Input Information for Each Student:


o Use a loop to input the details for each student:
 Input the student's name (use fgets() or scanf() to read the name).
 Input the student's roll number.
 Input the student's age.
 Input the marks for each subject (assume 5 subjects for simplicity).
5. Display Information of All Students:

o After storing the information for all students, use a loop to display the stored data.
o Print the name, roll number, age, and marks for each student.
6. End.

11/19/2024 File by – 24bec025


62.

ALGORITHM:-
1. Right-Angled Triangle (Left-Aligned)
Pattern:
markdown
Copy code
*
**
***
****
*****
Algorithm:
1. Input the number of rows n.
2. Outer Loop: Loop from 1 to n (for rows).
o Inner Loop: Loop from 1 to the current row number (i) and print *.
3. Print a new line after each row.

11/19/2024 File by – 24bec025


63.
2. Right-Angled Triangle (Right-Aligned)
Pattern:
markdown
Copy code
*
**
***
****
*****
Algorithm:
1. Input the number of rows n.
2. Outer Loop: Loop from 1 to n (for rows).
o Print Spaces: Loop from 1 to n-i (to print leading spaces).
o Print Stars: Loop from 1 to i (to print stars).

3. Print a new line after each row.

11/19/2024 File by – 24bec025


64.

3. Equilateral Triangle
Pattern:
markdown
Copy code
*
***
*****
*******
*********
Algorithm:
1. Input the number of rows n.
2. Outer Loop: Loop from 1 to n (for rows).
o Print Spaces: Loop from 1 to n-i (to print leading spaces).
o Print Stars: Loop from 1 to 2*i - 1 (to print stars, which increases with each row).
3. Print a new line after each row.

11/19/2024 File by – 24bec025


65.
Checkerboard Pattern
Pattern:
markdown
Copy code
*****
****
*****
****
Algorithm:
1. Input the number of rows n and columns m.
2. Outer Loop: Loop through rows.
o Inner Loop: Loop through columns.
 Print * if the sum of the current row and column index is even; otherwise, print a
space.
3. Print a new line after each row.

Made by:-24bec025
11/19/2024 File by – 24bec025
66.

9. Reverse Number Pyramid


Pattern:
markdown
Copy code
123454321
1234321
12321
121
1
Algorithm:
1. Input the number of rows n.
2. Outer Loop: Loop from n down to 1 (for rows).
o Print Spaces: Loop from 1 to n -i (to print leading spaces).
o Print Increasing Numbers: Loop from 1 to i and print numbers.
o Print Decreasing Numbers: Loop from i -1 down to 1 and print numbers in reverse.
3. Print a new line after each row.

11/19/2024 File by – 24bec025


67.

8. Number Pyramid
Pattern:
markdown
Copy code
1
121
12321
1234321
123454321
Algorithm:
1. Input the number of rows n.
2. Outer Loop: Loop from 1 to n (for rows).
o Print Spaces: Loop from 1 to n -i (to print leading spaces).
o Print Increasing Numbers: Loop from 1 to i and print numbers.
o Print Decreasing Numbers: Loop from i -1 down to 1 and print numbers in reverse.
3. Print a new line after each row.

11/19/2024 File by – 24bec025


68.

7. Hollow Pyramid
Pattern:
markdown
Copy code
*
**
* *
* *
*********
Algorithm:
1. Input the number of rows n.
2. Outer Loop: Loop from 1 to n (for rows).
o Print Spaces: Loop from 1 to n -i (to print leading spaces).
o Print Stars: Print a star at the start and end of each row. For intermediate rows, print
spaces between the stars.
 Print * on the first and last position of each row.
 Print spaces between the stars for intermediate rows.
3. Print a new line after each row.

11/19/2024 File by – 24bec025


69.
ALGORITHM:-
1. Start
2. Declare variable:
o Declare a variable ch to store the current character.
3. Initialize the loop:
o Set ch = 'A' (the first character of the alphabet).
4. Loop through characters:
o For ch from 'A' to 'Z' (inclusive):
 Print the current value of ch.
 Increment ch to the next character (i.e., ch = ch + 1 or ch++).
5. End

11/19/2024 File by – 24bec025

You might also like