Rajshahi University of Engineering & Technology: Heaven's Light Is Our Guide"
Rajshahi University of Engineering & Technology: Heaven's Light Is Our Guide"
Rajshahi University of Engineering & Technology: Heaven's Light Is Our Guide"
Submitted by Submitted to
Utsha Das
Name: Shibly Sadik
Lecturer
Roll : 2111008 Department of Computer Science &
Engineering
Series : 21
Rajshahi University of Engineering &
Technology.
Experiment no :12
Experiment name :Write a program that will print the prime numbers in a given range Theory:
and pp.
2. Check if nn has any divisors other than 1 and itself by testing for factors up
to the square root of nn.
Source code:
#include <math.h>
not prime
}
return 1; // Number is prime
&end);
if (isPrime(i)) { printf("%d
", i);
printf("\n");
return 0;
Result:
Discussion:
1. Objective: The objective of this experiment is to develop a program that prints all prime numbers
in a specified range.
2. Input/Output:
o Input: The program takes two integers as input from the user, representing the start and
end of the range.
o Output: It prints all the prime numbers between the given range inclusively.
3. Procedure:
o Step 1: Define a function is_prime(n) that returns True if n is a prime number and False
otherwise.
o Step 2: Define a function print_primes(start, end) that iterates through the given range
and prints the prime numbers using the is_prime function. o Step 3: In the main
function, prompt the user to enter the start and end of the range, and call
print_primes(start, end) to display the prime numbers.
4. Examples:
Conclusion:
• The experiment successfully demonstrates how to determine and print prime numbers within a
specified range using fundamental programming concepts.
• The use of a helper function (is_prime) aids in modularizing the code, making it easier to manage
and understand.
• The program efficiently checks for primality by iterating up to the square root of each number,
thereby reducing the number of required operations.
• This experiment emphasizes the application of loops, conditional statements, and function
definitions in solving a common mathematical problem.
Experiment no :13
Experiment name :Write a program that will check whether a given number is palindrome or not
Theory:
A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward
and backward (ignoring spaces, punctuation, and capitalization). Essentially, it is symmetrical.
Examples of palindromes:
In more fun terms, palindromes can be thought of as linguistic or numeric mirror images! They bring a
touch of symmetry and elegance to the language. If you have any favorite palindromes or want to
explore more, let me know!
Source code:
#include <stdio.h>
scanf("%d", &num);
while (num != 0) {
= reversed * 10 + remainder;
// Check if the original number and reversed number are the same
a palindrome.\n", original);
} else {
return 0;
Result:
Discussion:
The objective of this experiment was to develop a program that checks if a given
number is a palindrome. A palindrome is a number that reads the same forward and
backward. By converting the number to a string, reversing it, and comparing it to the
original, we can determine if it is a palindrome.
Conclusion:
The program successfully determines whether a given number is a palindrome by converting the
number to a string, reversing it, and comparing it with the original string. This straightforward
approach demonstrates fundamental programming concepts such as input handling, string
manipulation, and conditional checks, providing a clear and effective solution to the problem.
Experiment no :14
Experiment name :Write a C program to print patterns of numbers or stars Theory
of the Experiment
The objective of this experiment is to develop a C program that prints patterns of
numbers or stars. Pattern printing is a common exercise in programming that helps
in understanding the use of loops and nested loops. This experiment involves
creating various patterns (like triangles, pyramids, or other shapes) using numbers
or stars to gain a deeper understanding of control structures in C. Concepts
Involved
1. Loops: The main control structure used for pattern printing. for loops or
while loops are commonly used.
2. Nested Loops: One loop inside another is essential for printing multi-line
patterns.
3. Conditional Statements: Used to control the flow of the program and create
different patterns.
Source code:
#include <stdio.h>
int main() {
int rows, i, j;
// Ask the
user for the
number of
rows
printf("Enter
the number of
rows for the
pattern: ");
scanf("%d",
&rows);
return 0;
}
#include <stdio.h>
int main() {
int rows, i, j;
return 0;
}
Result:
Discussion:
Conclusion:
The nested loop approach provides a powerful way to create a wide range of patterns. By
adjusting the loop conditions and print statements, you can design patterns such as triangles,
pyramids, diamonds, or even custom designs. Understanding these basics is crucial for mastering
control flow and logic in programming.
Experiment no :15
Experiment name :Write a c program to check whether a given number is
Armstrong or not.
Theory
An Armstrong number (or narcissistic number) is a number that is equal to the sum of its own
digits raised to the power of the number of digits:
Source code:
#include <stdio.h>
#include <math.h>
countDigits(int num) {
int count = 0;
count++; num
/= 10;
return count;
}
// Function to check if a number is an Armstrong number
int main() {
int num;
if (isArmstrong(num)) { printf("%d is an
} else {
return 0;
}
Result:
Discussion:
The objective of this experiment is to develop a C program that checks whether a given number
is an Armstrong number. An Armstrong number is a number that is equal to the sum of its own
digits, each raised to the power of the number of digits. The program involves breaking down the
number into its individual digits, raising each to the appropriate power, summing the results, and
then comparing this sum to the original number.
Conclusion:
The program effectively determines if a number is an Armstrong number by using loops and
mathematical operations to analyze each digit. This experiment highlights the application of
basic programming constructs, such as loops and conditionals, and demonstrates a practical use
case of mathematical logic in coding. The successful implementation of this program
underscores the importance of understanding fundamental programming concepts and their
practical applications.
Experiment no :16
Experiment name :Write a program that will print the first n number sequence of
Fibonacci series.
The objective of this experiment is to develop a program that prints the first nn
numbers of the Fibonacci series. The Fibonacci series is a sequence of numbers where
each number is the sum of the two preceding ones, usually starting with 0 and 1. The
mathematical definition of the Fibonacci sequence is given by:
This sequence appears in many areas of mathematics and natural phenomena, such as
the branching patterns in trees, the arrangement of leaves on a stem, the flowering of
artichoke, the arrangement of a pine cone's bracts, and the fruit sprouts of a pineapple.
Source code:
#include <stdio.h>
next;
if (n <= 0) {
if (i <= 1) next = i;
else {
= next;
printf("\n");
int main() {
int n;
scanf("%d", &n);
printFibonacci(n);
return 0;
}
Result:
Discussion:
The aim of this experiment is to write a program that prints the first nn numbers in the Fibonacci
sequence. The Fibonacci sequence is a series of numbers where each number is the sum of the
two preceding ones, starting from 0 and 1. This experiment helps in understanding how to use
loops and iterative methods to generate sequences in programming. By calculating and printing
the Fibonacci sequence, we explore basic control structures and arithmetic operations in a
programming environment.
In the provided program, the number of terms (nn) is taken as input from the user. The sequence
is generated using a loop that iterates nn times, starting from the first two terms (0 and 1). Each
subsequent term is calculated by summing the two previous terms, which are updated iteratively.
This method ensures that the sequence is generated efficiently and displayed correctly.
Conclusion:
The program successfully generates and prints the first nn numbers of the Fibonacci sequence
using iterative methods. The use of loops allows for efficient calculation and minimal memory
usage. This experiment highlights the importance of understanding iterative processes and
control structures in programming. It provides a clear and practical application of these concepts,
reinforcing the basics of sequence generation and manipulation in programming. Through this
exercise, learners gain a foundational understanding of how to implement and optimize
algorithms for generating numerical sequences.