Lab Report
Lab Report
1. WAP to read 10 numbers from user and find their sum and average.
Objective
The objective of this program is to read 10 numbers from the user, calculate their sum and
average, and display the results.
Algorithm
i. Start.
ii. Declare variables num, sum, and average.
iii. Initialize sum to 0.
iv. Prompt the user to enter 10 numbers one by one and read them using scanf.
v. Add each number to the sum variable.
vi. Calculate the average by dividing the sum by 10.
vii. Display the sum and average using printf.
viii. Stop.
Flowchart
False
True
1
Code
#include <stdio.h>
int main() {
int num, sum = 0;
float average;
printf("Enter 10 numbers:\n");
for (int i = 1; i <= 10; i++) {
printf("Number %d: ", i);
scanf("%d", &num);
sum += num;
}
average = (float) sum / 10;
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", average);
return 0;
}
Output
Enter 10 numbers:
Number 1: 5
Number 2: 8
Number 3: 12
Number 4: 3
Number 5: 6
Number 6: 10
Number 7: 7
Number 8: 9
Number 9: 4
Number 10: 11
Sum: 75
Average: 7.50
2
2. WAP to display the multiplication table of integer given by the user.
Objective
The objective of this program is to take an integer input from the user and display its
multiplication table.
Algorithm
i. Start.
ii. Declare variables num and i.
iii. Prompt the user to enter a number and read it using scanf.
iv. Print the multiplication table heading.
v. Use a loop to iterate from 1 to 10.
vi. Inside the loop, calculate the product of num and i.
vii. Print the multiplication expression and the product using printf.
viii. Stop.
Flowchart
False
True
3
Code
#include <stdio.h>
int main( )
{
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
Output
Enter a number: 5
Multiplication table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
4
3. WAP to input two integer values from the user and print the even
number between the range of integers. Also count the even number and
display the count as well [Hint: if user enters 10 and 100. The program
should print and count even numbers between 10 and 100].
Objective
The objective of this program is to take two integer values from the user and print the even
numbers between the range of integers. Additionally, the program counts the even numbers and
displays the count.
Algorithm
i. Start.
ii. Declare variables min, max, and count to store the minimum value, maximum value, and
count of even numbers, respectively and initialize count to 0.
iii. Prompt the user to enter the minimum and maximum values and store them in respective
variables.
iv. If min is greater than max, swap their values.
v. Print the message indicating the range of numbers.
vi. Initialize a loop with a variable i starting from min + 1 and ending at max - 1.
vii. Within the loop, check if i is divisible by 2 (i.e., an even number).
viii. If i is even, print its value and increment the count variable.
ix. After the loop, check if the count variable is 0.
x. If count is 0, print "none" to indicate that there are no even numbers in the range.
xi. Print the total count of even numbers.
xii. End.
Flowchart
True
False
False
True
False
True
True
False
5
Code
#include <stdio.h>
int main( ) {
int min, max, count = 0;
printf("Enter the minimum and maximum value: ");
scanf("%d%d", &min, &max);
if (min > max) {
int temp = min;
min = max;
max = temp;
}
printf("The even numbers between %d and %d are: ", min, max);
for (int i = min + 1; i < max; i++) {
if (i % 2 == 0) {
printf("%d ", i);
count++;
}
} if (count == 0) {
printf("none ");
}
printf("\nThere are %d even numbers.\n", count);
return 0;
}
Output
Enter the minimum and maximum value: 15 25
The even numbers between 15 and 25 are: 16 18 20 22 24
There are 5 even numbers.
6
4. WAP to display sum of series: 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n
Objective
The objective of this program is to calculate and display the sum of the given series: 1 + 1/2 +
1/3 + 1/4 + 1/5 ... 1/n.
Algorithm
i. Start.
ii. Declare variables n and sum to store the value of n and the sum of the series, respectively
and initialize sum to 0.
iii. Prompt the user to enter the value of n and store it in n.
iv. Use a loop with a variable i starting from 1 and ending at n.
v. Within the loop, add 1 / (float)i to the sum variable. The (float) conversion is used to ensure
floating-point division.
vi. After the loop, print the value of sum as the sum of the series.
vii. End.
Flowchart
False
True
i++
7
Code
#include <stdio.h>
int main()
{
int n;
float sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
sum += 1 / (float)i;
}
printf("The sum of series: 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n is %f", sum);
return 0;
}
Output
Enter the value of n: 5
The sum of the series: 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n is 2.283334
8
5. WAP to display sum of series: 1 + 1/2! + 1/3! + 1/4! + 1/5! ... 1/n!
Objective
The objective of this program is to calculate and display the sum of the series: 1 + 1/2! + 1/3! +
1/4! + 1/5! ... 1/n!.
Algorithm
i. Start.
ii. Declare variables n, factorial, and sum to store the value of n, the factorial value, and the
sum of the series, respectively and initialize the value of factorial to 1 and sum to 0.
iii. Prompt the user to enter the value of n and store it to the respective variable.
iv. Use a loop with a variable i starting from 1 and ending at n.
v. Within the loop, calculate the factorial of i by multiplying it with the current value of
factorial and add 1 / (float)factorial to the sum variable.
vi. After the loop, print the value of sum as the sum of the series.
vii. End.
Flowchart
9
Code
#include <stdio.h>
int main()
{
int n, factorial= 1;
float sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
factorial *= i;
sum += 1 / (float)factorial;
}
printf("The sum of series: 1 + 1/2! + 1/3! + 1/4! + 1/5! ... 1/n! is %f", sum);
return 0;
}
Output
Enter the value of n: 5
The sum of the series: 1 + 1/2! + 1/3! + 1/4! + 1/5! ... 1/n! is 1.716667
10
6. WAP to display sum of series: x + x2/2! + x3/3! + x4/4! + x5/5! ...
xn/n!
Objective
The objective of this program is to calculate and display the sum of the series: x + x^2/2! +
x^3/3! + x^4/4! + x^5/5! ... + x^n/n!.
Algorithm
i. Start.
ii. Declare variables x, n, sum, and factorial and initialize the sum variable to 0 and the
factorial variable to 1.
iii. Prompt the user to enter the value of x and n and store them in the respective variables.
iv. Use a loop with a variable i starting from 1 and ending at n.
v. Within the loop, calculate the factorial of i by multiplying it with the current value of
factorial.
vi. Calculate x raised to the power of i using the pow() function from the math.h library.
vii. Add (x^i / factorial) to the sum variable.
viii. After the loop, print the value of sum as the sum of the series.
ix. End.
Flowchart
False
True
11
Code
#include <stdio.h>
#include <math.h>
int main()
{
float x, sum = 0;
int n, factorial = 1;
return 0;
}
Output
Enter the value of x: 2.5
Enter the value of n: 5
The sum of the series: x + x^2/2! + x^3/3! + x^4/4! + x^5/5! ... + x^n/n! is 24.041666.
12
7. WAP to find the value cos(x) without using cos(x) library function.
Objective:
The objective of this program is to calculate the value of cos(x) without using the cos(x) library
function. It uses the Taylor series expansion of cos(x) to approximate the value with a given
accuracy.
Algorithm
i. Start.
ii. Declare variables n, term, factorial, x, and cosx and initialize term to 1, cosx to 1, and n to 1.
iii. Prompt the user to enter the value of x in degrees and store it into respective variables.
iv. Convert the value of x from degrees to radians by multiplying it with (π/180).
v. Use a do-while loop to calculate the terms of the Taylor series expansion.
vi. Inside the loop, calculate the factorial of (2 * n) * (2 * n - 1).
vii. Calculate the next term of the series using the previous term, x, and factorial.
viii. Add the term to the cosx variable.
ix. Increment n by 1.
x. Repeat steps 8-11 until n reaches a sufficiently large value (e.g., 999).
xi. Print the calculated value of cos(x)
xii. End.
Flowchart
13
Code
#include <stdio.h>
int main()
{
int n = 1;
float term = 1, factorial, x, cosx = 1;
printf("Enter the value of x (in degrees): ");
scanf("%f", &x);
float x_in_deg = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
do
{
factorial = 2 * n * (2 * n - 1);
term = -term * x * x / factorial;
cosx = cosx + term;
n++;
} while (n < 999);
printf("Cos(%.2fdeg): %.4f\n", x_in_deg, cos);
return 0;
}
Output
Enter the value of x (in degrees): 60
Cos(60.00deg): 0.50
It prompts the user to enter the value of x in degrees, converts it to radians, and then calculates
the series terms and the approximate value of cos(x). The accuracy of the approximation depends
on the number of terms used in the series. The program prints the calculated value of cos(x)
along with the entered value of x in degrees. The implementation is in the C programming
language.
14
8. WAP to display weather a number is Armstrong or not.
Objective
The objective of this program is to determine whether a given number is an Armstrong number or
not. An Armstrong number is a number that is equal to the sum of its own digits raised to the power
of the number of digits.
Algorithm
i. Start.
ii. Declare variables num, rem, armstrong, and count and initialize armstrong and count to 0.
iii. Prompt the user to enter a number and store it in the respective variable.
iv. Initialize a variable org_num with the value of num.
v. Calculate the number of digits in num by repeatedly dividing org_num by 10 and incrementing
count until org_num becomes 0.
vi. Reset org_num to its original value.
vii. Use a while loop to calculate the sum of individual digits raised to power count of org_num.
viii. Check if num is equal to armstrong.
ix. If they are equal, print that num is an Armstrong number.
x. If they are not equal, print that num is not an Armstrong number.
xi. End.
Flowchart
False
True
False
True
15
Code
#include <stdio.h>
#include <math.h>
int main()
{
int num, rem, armstrong = 0, count = 0;
printf("Enter a number: ");
scanf("%d", &num);
int org_num = num;
while (org_num != 0)
{
org_num /= 10;
count++;
}
org_num = num;
while (org_num != 0)
{
rem = org_num % 10;
org_num /= 10;
armstrong += pow(rem, count);
}
if (num == armstrong)
{
printf("%d is an Armstrong number.", num);
}
else
{
printf("%d is not an Armstrong number.", org_num);
}
return 0;
}
Output
Enter a number: 153
153 is an Armstrong number.
16
9. WAP to display the first n terms of Fibonacci series.
Objective
The objective of this program is to display the first n terms of the Fibonacci series.
Algorithm
i. Start.
ii. Declare variables n, fibonacci_term = 0, n_1, and n_2.
iii. Prompt the user to enter a number and store it in respective variable.
iv. Use a for loop to iterate from 1 to n.
v. Initialize fibonacci_term as 0 for the first term.
vi. If i is equal to 2, set n_1 as fibonacci_term and update fibonacci_term to 1.
vii. For subsequent terms (i > 2), update n_2 as n_1, n_1 as fibonacci_term, and calculate
fibonacci_term as the sum of n_2 and n_1.
viii. Print the value of fibonacci_term.
ix. End.
Flowchart
17
Code
#include <stdio.h>
int main()
{
// n_1 is n-1th term and n_2 is n-2th term of fibonacci series
int n, fibonacci_term = 0, n_1, n_2;
printf("Enter a number: ");
scanf("%d", &n);
printf("Fibonacci Series to %dth term: \n", n);
for (int i = 1; i <= n; i++)
{
if (i == 1)
{
fibonacci_term = 0;
}
else if (i == 2)
{
n_1 = fibonacci_term;
fibonacci_term = 1;
}
else
{
n_2 = n_1;
n_1 = fibonacci_term;
fibonacci_term = n_2 + n_1;
}
printf("%d ", fibonacci_term);
}
return 0;
}
Output
Enter a number: 10
Fibonacci Series to 10th term:
0 1 1 2 3 5 8 13 21 34
18
10. WAP to display the number in reverse order.
Objective
The objective of this program is to display a number in reverse order.
Algorithm
i. Start.
ii. Declare variables num and reversed = 0.
iii. Prompt the user to enter a number and store the input in respective variable.
iv. Print the number before swapping.
v. Use a for loop to reverse the number.
vi. Repeat the loop until num becomes 0.
vii. In each iteration, calculate the reversed number by multiplying it by 10 and adding the
last digit of num using the modulus operator.
viii. Divide num by 10 to remove the last digit.
ix. Print the reversed number
x. End.
Flowchart
19
Code
#include <stdio.h>
int main()
{
int num, reversed = 0;
printf("Enter a number: ");
scanf("%d", &num);
printf("Before Swapping: %d\n", num);
for (int i = 0; num != 0; i++)
{
reversed = ((reversed * 10) + (num % 10));
num /= 10;
}
printf("After Swapping: %d\n", reversed);
return 0;
}
Output
Enter a number: 12345
Before Swapping: 12345
After Swapping: 54321
20
11. WAP to check whether a number is a palindrome or not.
Objective
The objective of this program is to check whether a number is a palindrome or not.
Algorithm
i. Start the program.
ii. Declare variables num, reversed = 0, and org_num.
iii. Prompt the user to enter a number.
iv. Read the value of num from the user.
v. Set org_num equal to num.
vi. Use a for loop to reverse the number.
vii. Repeat the loop until num becomes 0.
viii. In each iteration, calculate the reversed number by multiplying it by 10 and adding the
last digit of num using the modulus operator.
ix. Divide num by 10 to remove the last digit.
x. Check if org_num is equal to reversed.
xi. If they are equal, print the message "num is a palindrome number."
xii. If they are not equal, print the message "num is not a palindrome number."
xiii. End.
Flowchart
21
Code
#include <stdio.h>
int main()
{
int num, reversed = 0, org_num;
printf("Enter a number: ");
scanf("%d", &num);
org_num = num;
for (int i = 0; num != 0; i++)
{
reversed = ((reversed * 10) + (num % 10));
num /= 10;
}
if (org_num == reversed){
printf("%d is palindrome number.", org_num);
}
else{
printf("%d is not a palindrome number.", org_num);
}
return 0;
}
Output
Enter a number: 12321
12321 is a palindrome number.
22
12. WAP to find HCF and LCM of two numbers.
Objective
The objective of this program is to find the highest common factor (HCF) and least common
multiple (LCM) of two numbers.
Algorithm
i. Start.
ii. Declare variables num1, num2, larger_num, smaller_num, hcf, and lcm.
iii. Prompt the user to enter two numbers.
iv. Read the values of num1 and num2 from the user.
v. Determine the larger and smaller numbers by comparing num1 and num2.
vi. Use a for loop to find the HCF.
vii. Start the loop from 1 and iterate up to the value of the smaller number.
viii. Check if both the larger and smaller numbers are divisible by the current iteration value.
ix. If they are divisible, update the value of hcf to the current iteration value.
x. Use an infinite for loop to find the LCM.
xi. Start the loop from the larger number and increment by the value of the larger number in
each iteration.
xii. Check if both the current iteration value and the smaller number are divisible by the
larger number.
xiii. If they are divisible, update the value of lcm to the current iteration value and break the
loop.
xiv. Print the values of hcf and lcm.
xv. End.
Flowchart
23
Code
#include <stdio.h>
int main()
{
int num1, num2, larger_num, smaller_num, hcf, lcm;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
larger_num = num1 > num2 ? num1 : num2;
smaller_num = num2 > num1 ? num1 : num2;
for (int i = 1; i <= smaller_num; i++)
{
if (larger_num % i == 0 && smaller_num % i == 0)
{
hcf = i;
}
}
for (int i = larger_num;; i++)
{
if (i % larger_num == 0 && i % smaller_num == 0)
{
lcm = i;
break;
}
}
printf("HCF of %d and %d is: %d\n", num1, num2, hcf);
printf("LCM of %d and %d is: %d\n", num1, num2, lcm);
return 0;
}
Output
Enter two numbers: 12 18
HCF of 12 and 18 is: 6
LCM of 12 and 18 is: 36
24
13. WAP to print the following patterns:
Objective
The objective of this program is to print various patterns using loops and nested loops.
Algorithm
a) main(): iii. Inside the row loop, use a loop to print
i. Start. spaces based on the row number (5 -
ii. Print a newline and the string "Pattern current row).
1: ". iv. Use another loop to print stars based on
iii. Call the pattern1 function. the row number (2 * current row - 1).
iv. Print a newline and the string "Pattern v. Move to the next line after printing the
2: ". spaces and stars.
v. Call the pattern2 function. vi. End.
vi. Print a newline and the string "Pattern e) pattern4():
3: ". i. Start the pattern4 function.
vii. Call the pattern3 function. ii. Initialize a variable num to 1.
viii. Print a newline and the string "Pattern iii. Use a loop to iterate over the rows from 1
4: ". to 5.
ix. Call the pattern4 function. iv. Inside the row loop, use another loop to
x. Print a newline and the string "Pattern iterate over the columns from 1 to the
5: ". current row number.
xi. Call the pattern5 function. v. Print the current value of num and
xii. Print a newline and the string "Pattern increment it by 1 for each iteration.
6: ". vi. Move to the next line after printing the
xiii. Call the pattern6 function. columns.
xiv. End. vii. End the function.
b) pattern1(): f) patter5():
i. Start. i. Start.
ii. Use a loop to iterate over the rows from ii. Use a loop to iterate over the rows from 1
1 to 5. to 5.
iii. Inside the row loop, use another loop to iii. Inside the row loop, use a loop to print
iterate over the columns from 1 to the spaces based on the row number.
current row number. iv. Use another loop to print numbers in an
iv. Print the column number for each increasing order from 1 to (6 - row).
iteration. v. Use a third loop to print numbers in a
v. Move to the next line after printing the decreasing order from (5 - row) to 1.
columns. vi. Move to the next line after printing the
vi. End. spaces and numbers.
c) pattern2(): vii. End.
i. Start. g) pattern6():
ii. Use a loop to iterate over the rows from i. Start.
5 to 1 in a descending order. ii. Use a loop to iterate over the rows from 5
iii. Inside the row loop, use another loop to to 1 in a descending order.
iterate over the columns from 1 to the iii. Inside the row loop, use another loop to
current row number. iterate over the columns from 5 to (6 -
iv. Print the column number for each row).
iteration. iv. Print the column number for each
v. Move to the next line after printing the iteration.
columns. v. Move to the next line after printing the
vi. End. columns.
d) pattern3(): vi. End.
i. Start.
ii. Use a loop to iterate over the rows from 1
to 5.
25
Flowchart
26
27
Code }
28
Output
Pattern 1:
1
12
123
1234
12345
Pattern 2:
12345
1234
123
12
1
Pattern 3:
*
***
*****
*******
*********
Pattern 4:
1
23
456
7 8 9 10
11 12 13 14 15
Pattern 5:
123454321
1234321
12321
121
1
Pattern 6:
54321
5432
543
54
5
Discussion and Conclusion
This program demonstrates the use of loops and nested loops to print various patterns. Each
pattern is implemented as a separate function, making the code modular and easy to understand.
The patterns range from simple number patterns to more complex patterns involving spaces and
stars. The implementation is in the C programming language.
29