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

Lab Report 5

The document is a CSE lab report detailing various C programming exercises focused on mathematical operations and algorithms. It includes programs for calculating sums of sequences, reversing integers, checking for palindromes, identifying Armstrong numbers, and counting digits, along with source code and outputs for each program. The report concludes with lessons learned, mistakes to avoid, and emphasizes the importance of loops and mathematical operations in programming.

Uploaded by

2251081143
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 views13 pages

Lab Report 5

The document is a CSE lab report detailing various C programming exercises focused on mathematical operations and algorithms. It includes programs for calculating sums of sequences, reversing integers, checking for palindromes, identifying Armstrong numbers, and counting digits, along with source code and outputs for each program. The report concludes with lessons learned, mistakes to avoid, and emphasizes the importance of loops and mathematical operations in programming.

Uploaded by

2251081143
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/ 13

CSE Lab Report-5

Submitted by:
Name: Md. Abid Al Azmain Shakil
ID: 2251081143
Batch: 63(D)
Department: CSE

Submitted to:
Name: Noor Easrib Tiba
Submission Date: 06/03/2025
Department of CSE
Uttara University
Introduction

In the world of programming, solving problems involving mathematical operations and


algorithms is a common and fundamental task. C programming provides a variety of features that
allow developers to implement mathematical computations effectively. By learning how to work
with loops, conditional statements, and other basic constructs, programmers can efficiently solve
problems involving arithmetic sequences, reverse operations, number properties, and more. This
set of programs covers several essential topics in C, such as calculating the sum of sequences,
reversing integers, checking for palindrome numbers, identifying Armstrong numbers, and
counting digits. These programs will help develop a deeper understanding of C programming
basics and enhance problem-solving skills.

Description

Program to Calculate the Sum of the Series 1, 2, 3, ..., n: This program calculates the sum of the
first n natural numbers. It iterates through all integers from 1 to n, adding each number to a
cumulative sum. The program demonstrates how to use loops for performing repetitive
arithmetic operations and is a great way to learn about the basic concept of series summation.

Program to Calculate the Sum of Two Series 1, 3, 5, 7,..., n and 2, 4, 6, 8,..., n: This program
calculates the sum of two distinct arithmetic series: one consisting of odd numbers and the other
consisting of even numbers, both up to a given number n. The program loops through the range
and categorizes each number as either odd or even, adding them separately to maintain
separate sums. It helps understand how to manage multiple conditions within loops.

Program to Reverse the Order of an Integer Number: This program takes an integer input and
reverses the digits of the number. Using a loop, it repeatedly extracts the last digit of the number
and builds a new integer by appending each extracted digit. This demonstrates how to
manipulate the individual digits of a number, which is a common task in many real-world
applications like data formatting.

Program to Check Whether an Integer is a Palindrome: This program checks if a given integer is
a palindrome. A palindrome number is one that remains the same when its digits are reversed.
The program first reverses the number and then compares it with the original number. If they
are the same, the number is a palindrome. This helps illustrate how to work with numbers and
compare their properties.
Program to Check Whether an Integer is an Armstrong Number: An Armstrong number (or
narcissistic number) is a number that is equal to the sum of its own digits each raised to the
power of the number of digits. This program calculates the sum of the digits of a number raised
to the power of the number of digits and checks if the sum equals the original number. This task
is often used in number theory problems and provides a deeper understanding of how to
manipulate and analyze numbers in programming.

Program to Count the Number of Digits in an Integer Number: This program counts how many
digits a given integer has. By repeatedly dividing the number by 10, the program keeps track of
the number of times this operation is performed until the number becomes zero. This program
provides insight into number manipulation and helps solidify the understanding of basic
mathematical operations in programming.

Experiment No. 1

Write a C program to calculate the sum of the following series.


1,2,3,4,...........,n.

Source Code

#include<stdio.h>
int main()
{
int i = 1;
int sum = 0;
//Using loop and unary operator
while(i<=100)
{
sum = sum+i;
i++;
}
//Display result
printf("The sum of the following series is : %d", sum);
return 0;
}

Output

Experiment No. 2

Write a C program to calculate the sum of the following series.


1,3,5,7,...........,n
2,4,6,8,...........,n

Source Code

#include<stdio.h>
int main()
{
//Variable initialization
int i = 1;
int sum_odd = 0,sum_even = 0;
//Using loop and unary operator
while(i<=100)
{
if(i%2==0)
{
sum_even = sum_even + i;
}
else
{
sum_odd = sum_odd + i;
}
i++;
}

//Display result
printf("Sum of the odd numbers is : %d\n", sum_odd);
printf("Sum of the even numbers is : %d\n", sum_even);

return 0;
}

Output
Experiment No. 3

Write a C program to evaluate the reverse order of an integer number.

Source Code

#include<stdio.h>
int main()
{
int num, reverse = 0, remainder;

// Input the number from the user


printf("Enter an integer number : ");
scanf("%d", &num);

// Reverse the digits of the number


while (num != 0)
{
remainder = num % 10; // Get the last digit
reverse = reverse * 10 + remainder; // Add the digit to the reverse
num = num / 10; // Remove the last digit
}

// Output of the reversed number


printf("The reversed number is : %d\n", reverse);
return 0;
}

Output

Experiment No. 4

Write a C program to check whether an integer number is palindrome.

Source Code

#include <stdio.h>
int main()
{
int num, originalnum, reversednum = 0, remainder;

// Input the number from the user


printf("Enter an integer number: ");
scanf("%d", &num);

originalnum = num; // Store the original number for comparison


// Reverse the digits of the number
while (num != 0)
{
remainder = num % 10; // Get the last digit
reversednum = reversednum * 10 + remainder; // Add the digit to the reversed number
num = num / 10; // Remove the last digit
}

// Check if the number is a palindrome number or not


if (originalnum == reversednum)
{
printf("%d is a Palindrome number.\n", originalnum);
}
else
{
printf("%d is not a Palindrome number.\n", originalnum);
}

return 0;
}

Output
Experiment No. 5

Write a C program to check whether an integer number is Armstrong number.

Source Code

#include <stdio.h>
#include <math.h>
int main()
{
int num, originalnum, remainder, sum = 0, digits = 0;

// Input the number from the user


printf("Enter an integer number : ");
scanf("%d", &num);

originalnum = num; // Store the original number for comparison

// Calculate the number of digits in the given number


while (originalnum != 0)
{
originalnum = originalnum / 10;
digits++;
}

originalnum = num; // Restore the original number


// Calculate the sum of each digit raised to the power of 'digits'
while (originalnum != 0)
{
remainder = originalnum % 10; // Get the last digit
sum = sum + pow(remainder, digits); // Add the power of the digit to sum
originalnum = originalnum / 10; // Remove the last digit
}

// Check if the number is an Armstrong number


if (sum == num)
{
printf("%d is an Armstrong number.\n", num);
}
else
{
printf("%d is not an Armstrong number.\n", num);
}

return 0;
}

Output
Experiment No. 6

Write a C program to count the number of digits of an integer number.

Source Code

#include <stdio.h>
int main()
{
int num, count = 0;

// Ask the user to input a number


printf("Enter an integer number: ");
scanf("%d", &num);

// If the number is 0, it's a special case with 1 digit


if (num == 0)
{
count = 1;
}
else
{
// Count digits
while (num != 0)
{
num /= 10; // Remove the last digit
count++; // Increment the count for each digit
}
}

// Print the result


printf("The number of digits is: %d\n", count);

return 0;
}

Output

Conclusion

These programs offer a solid foundation in C programming by demonstrating practical uses of


loops, conditional statements, and basic mathematical operations. By working through these
exercises, we have learned how to:

1. Sum of Sequences: We calculated the sum of natural numbers, odd numbers, and even
numbers, which reinforces our understanding of loops and arithmetic operations.
2. Reverse Operations: We learned how to reverse the digits of a number, which is an
essential task in many applications.
3. Palindrome Check: We practiced checking for palindromes, helping us understand how to
compare original and reversed versions of numbers.
4. Armstrong Numbers: We explored Armstrong numbers, which involves extracting and
raising digits to specific powers, and we saw how mathematical concepts can be
implemented in code.
5. Digit Counting: Counting the digits of a number helped us reinforce the use of loops and
integer operations in real-world problems.

What We Learned:

• Looping Constructs: Mastery of using for and while loops to solve problems such as
summing series, reversing digits, and counting digits.
• Mathematical Operations: We applied basic arithmetic and modulus operations to solve
practical problems.
• Number Theory: We explored interesting properties of numbers such as Armstrong
numbers and palindromes, which are widely used in mathematical computations.

Mistakes to Avoid:

1. Ignoring Input Validation: When taking user input, always ensure the input is valid. For
example, negative numbers might need to be handled separately for problems like digit
counting or Armstrong number checking.
2. Incorrect Logic in Palindrome or Armstrong Check: Be careful while comparing the
original number with the reversed or calculated sum in palindrome and Armstrong
checks. Small mistakes in logic can lead to incorrect results.
3. Using Incorrect Data Types: Ensure that you're using the correct data types. For example,
when calculating powers or sums for Armstrong numbers, the result can overflow if the
data type is too small.

The End

You might also like