Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Rajshahi University of Engineering & Technology: Heaven's Light Is Our Guide"

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code :CSE 2290

Course Name :Computer Programming Sessional

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:

Theory of the Experiment


The purpose of this experiment is to create a program that prints all the prime
numbers within a given range. A prime number is a natural number greater than 1
that has no positive divisors other than 1 and itself. In other words, a prime
number is only divisible by 1 and itself.
Prime Number Concept:

1. Definition: A prime number pp has exactly two distinct positive divisors: 1

and pp.

2. Examples: 2, 3, 5, 7, 11, etc.

3. Non-Examples: 4 (divisible by 1, 2, and 4), 6 (divisible by 1, 2, 3, and 6).


To determine whether a number nn is prime:

1. Check if nn is less than 2 (if true, it is not a prime number).

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>

// Function to check if a number is prime int isPrime(int

num) { if (num <= 1) return 0; // Numbers <= 1 are

not prime

for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0)

return 0; // Found a divisor, not prime

}
return 1; // Number is prime

// Main function to print primes in a given range

int main() { int start, end;

// Input range from the user

printf("Enter the start of the range: ");

scanf("%d", &start); printf("Enter the

end of the range: "); scanf("%d",

&end);

// Print prime numbers in the range printf("Prime numbers

between %d and %d are:\n", start, end);

for (int i = start; i <= end; i++) {

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:

o If the user enters 10 and 20, the program outputs: 11 13 17 19.

o If the user enters 1 and 10, the program outputs: 2 3 5 7.

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:

• Words: "radar", "level", "rotor"

• Phrases: "A man, a plan, a canal, Panama"

• Numbers: 121, 12321

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>

int main() { int num, original, reversed = 0,


remainder;

// Input the number

printf("Enter an integer: ");

scanf("%d", &num);

// Store the original number


original = num;

// Reverse the number

while (num != 0) {

remainder = num % 10; //


Get the last digit reversed

= reversed * 10 + remainder;

// Build the reversed number

num /= 10; // Remove the last digit

// Check if the original number and reversed number are the same

if (original == reversed) { printf("%d is

a palindrome.\n", original);

} else {

printf("%d is not a palindrome.\n", original);

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.

This experiment demonstrates the practical application of string manipulation and


conditional checks in programming. It provides a clear understanding of how to
implement simple algorithms to solve specific problems efficiently. The approach
used in this experiment is straightforward, making it an excellent exercise for
beginners to grasp fundamental programming concepts.

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);

// Generate the triangular pattern for (i = 1; i


<= rows; i++) { // Outer loop for rows for (j =
1; j <= i; j++) { // Inner loop for columns
printf("%d ",j); // Print star
}
printf("\n"); // New line after each row
}

return 0;
}
#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);

// Generate the triangular pattern


for (i = 1; i <= rows; i++) { // Outer loop for rows
for (j = 1; j <= i; j++) { // Inner loop for columns
printf("* "); // Print star
}
printf("\n"); // New line after each row
}

return 0;
}

Result:
Discussion:

The objective of this experiment is to create a C program that prints patterns of


numbers or stars using loops and nested loops. Pattern printing exercises are crucial
for understanding the use of iterative control structures in programming. By
specifying the number of rows and using loops to manage the alignment and repetition
of characters, this experiment helps build foundational skills in algorithmic thinking
and code organization. Through this process, students gain insights into how to
manipulate output formatting and develop a deeper comprehension of loop constructs.

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>

// Function to calculate the number of digits in a number int

countDigits(int num) {

int count = 0;

while (num > 0) {

count++; num

/= 10;

return count;

}
// Function to check if a number is an Armstrong number

int isArmstrong(int num) { int originalNum = num,

remainder, sum = 0; int digits = countDigits(num);

while (num > 0) { remainder = num % 10; // Extract the

last digit sum += pow(remainder, digits); // Add the power

of the digit num /= 10; // Remove the last digit

return (sum == originalNum);

int main() {

int num;

// Input from the user printf("Enter a number to check if it is

an Armstrong number: "); scanf("%d", &num);

// Check if the number is an Armstrong number

if (isArmstrong(num)) { printf("%d is an

Armstrong number.\n", num);

} else {

printf("%d is not an Armstrong number.\n", num);

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.

Theory of the Experiment:

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:

F(0)=0,F(1)=1F(0) = 0, \quad F(1) = 1

F(n)=F(n−1)+F(n−2)forn≥2F(n) = F(n-1) + F(n-2) \quad \text{for} \quad n \geq 2

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>

// Function to generate Fibonacci series up to n terms

void printFibonacci(int n) { int first = 0, second = 1,

next;

if (n <= 0) {

printf("Please enter a positive number.\n");


return;

printf("Fibonacci Series: ");

for (int i = 0; i < n; i++) {

if (i <= 1) next = i;

else {

next = first + second;

first = second; second

= next;

printf("%d ", next);

printf("\n");

int main() {

int n;

// Input: number of terms

printf("Enter the number of terms: ");

scanf("%d", &n);

// Function call to print Fibonacci series

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.

You might also like