c Programming Lab File 24bec025[1] (1)
c Programming Lab File 24bec025[1] (1)
CODEoutput and
input.
ALGORITHM
1. Start.
2. Declare a variable to represent the data type for which you want
to find the size.
5. End.
*2.
5. End.
3.
1. Start.
2. Read a number from the user.
a) Extract the rightmost digit by taking the number modulo 10 and store it in a
variable
digit.
2. End.
1. Start.
5.
1. Start.
6. End .
1. Start.
4. End.
7.
1. Start.
4. End.
1. Start.
2. Read three numbers from the user and store them in variables
num1, num2, and num3.
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.
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.
6. End.
12.
1. Start.
13.
1. Start.
1. Start.
15.
1. Start.
6. End.
11/19/2024 File by – 24bec025
16.
1. Start.
b) Increment num by 1.
17.
1. Start.
7. End.
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.
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.
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.
5. After the loop, sum will contain the sum of all numbers between m
and n.
7. End.
21.
1. Start.
5. End
23.
1. Start.
2. Read a number from the user.
a) Extract the rightmost digit by taking the number modulo 10 and store it in a
variable
digit.
2. End.
24.
1. Start.
2. Read a number from the user and store it in a variable
number.
4. End.
25.
1. Start.
2. Read the starting year 'm' and the ending year 'n' from
the user.
1. End.
1. Start
.
2. Include the necessary header file for input and output
operations.
27.
1. Start.
6. End
28.
1. Start.
2. Include the necessary header files for input and output operations and
mathematical functions.
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.
6. End
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:
2. Declare function:
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:
4. Input:
o Read the value of n from the user (the upper limit of natural numbers).
5. Check validity:
7. Output:
8. End
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
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
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
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:
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
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
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
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
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
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
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.
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.
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
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.
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.
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.
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:
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.
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:
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.
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.
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.
Made by:-24bec025
11/19/2024 File by – 24bec025
66.
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.
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.