0% found this document useful (0 votes)
23 views16 pages

Lab 4 Rahul

Uploaded by

bivaman1617
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)
23 views16 pages

Lab 4 Rahul

Uploaded by

bivaman1617
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/ 16

“Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code : CSE 2290


Course Name : Computer Programming Sessional
Experiment No : 12
Experiment Name : : Writing a program that will print the prime numbers in a given
range.

Submitted by Submitted to
Name : Rahul Das Uthsha Das
Lecturer
Roll : 2111022
Department of Computer Science &
Series : 21 Engineering
Rajshahi University of Engineering
& Technology.

Date of Submission:30/11/2024
Experiment no :12
Experiment name :Write a program that will print the prime numbers in a
given range
Theory:
A prime number is a natural number greater than 1 that has no positive divisors
other than 1 and itself. For example, numbers such as 2, 3, 5, and 7 are prime,
while 4, 6, and 9 are not.
Key Properties of Prime Numbers:
1. A prime number is divisible only by 1 and itself.
2. The number 2 is the only even prime number.
Algorithm to Check for a Prime Number:
To determine whether a number n is prime:
1. Check if n≤1. If true, n is not prime.
2. Check divisors from 2 to n\sqrt{n}n to see if n is divisible. If a divisor is
found, n is not prime.
Source code:
Output:

Discussion:

1. Efficiency:
o The program uses n\sqrt{n}n as the upper bound for checking divisors, which
optimizes the process of determining primality.
o For larger ranges, the program's performance can be further improved by
implementing techniques like the Sieve of Eratosthenes.
2. Edge Cases:
o When the start of the range is less than or equal to 1, the program excludes
non-prime numbers like 0 and 1.
o If the range does not contain any primes, the output will be empty.
3. Limitations:
o The program does not handle negative ranges effectively because negative
numbers are not prime.
o User input validation is not included, which might lead to unexpected behavior
if non-numeric input is provided.

Conclusion:

This experiment demonstrated how to use basic C programming concepts, such as loops and
functions, to identify prime numbers within a specified range. The program performed as
expected, efficiently listing prime numbers. Further optimization, such as advanced
algorithms, could make it suitable for larger datasets or ranges.
“Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code : CSE 2290


Course Name : Computer Programming Sessional
Experiment No : 13
Experiment Name : : Writing a program that will check whether a given number is
palindrome or not. Palindrome Numbers

Submitted by Submitted to
Name : Rahul Das Uthsha Das
Lecturer
Roll : 2111022
Department of Computer Science &
Series : 21 Engineering
Rajshahi University of Engineering
& Technology.

Date of Submission:30/11/2024
Experiment no.: 13
Experiment Name: Write a program that will check whether a given number
is palindrome or not. Palindrome Numbers
Theory:
A palindrome number is a number that remains the same when its digits are
reversed. For example:
• Palindrome: 121, 12321, 1331
• Not a Palindrome: 123, 1234, 54321
To determine whether a number is a palindrome:
1. Extract the digits of the number in reverse order.
2. Compare the reversed number with the original number.
3. If they are the same, the number is a palindrome.
Source code ;
Output:

Discussion:

1. Logic Explanation:
o The program uses a while loop to reverse the digits of the number.
By continuously extracting and appending the last digit of the
number, the reversed number is constructed.
o After reversing, the original number is compared to the reversed
number. If they match, the number is a palindrome.
2. Advantages:
o Demonstrates the use of fundamental programming constructs like
loops and conditionals.
o Can handle positive integers of varying lengths.
3. Limitations:
o The program does not handle negative numbers or non-integer
inputs. Enhancements can include support for these cases.
4. Testing:
o The program was tested with various inputs including single-digit
numbers, even-length numbers, odd-length numbers, and non-
palindromes.

Conclusion:

This experiment effectively demonstrates how to determine whether a given


number is a palindrome using C programming. The concepts of arithmetic
operations, loops, and conditionals were applied successfully. The program is
accurate and provides immediate results for valid inputs.
“Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code : CSE 2290


Course Name : Computer Programming Sessional
Experiment No : 14
Experiment Name : : Writing a program to print patterns of numbers or stars.
Submitted by Submitted to
Name : Rahul Das Uthsha Das
Lecturer
Roll : 2111022
Department of Computer Science &
Series : 21 Engineering
Rajshahi University of Engineering
& Technology.

Date of Submission:30/11/2024
Experiment No.: 14
Experiment Name: Write a C program to print patterns of numbers or stars.
Theory :

Patterns in programming involve printing specific sequences of characters or


numbers in a structured format. This is achieved using nested loops, where:

• The outer loop controls the number of lines or rows in the pattern.
• The inner loop controls the elements printed in each row.

Common types of patterns include:

1. Star patterns (e.g., pyramids, diamonds, right-angled triangles).


2. Number patterns (e.g., sequential numbers, mirrored numbers, etc.)

Source Code :

#include <stdio.h>

void printStarPattern(int rows) {

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

printf("* ");

printf("\n");

void printNumberPyramid(int rows) {

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= rows - i; j++) {

printf(" "); // Print spaces for alignment

for (int k = 1; k <= i; k++) {


printf("%d ", k);

for (int l = i - 1; l >= 1; l--) {

printf("%d ", l);

printf("\n");

int main() {

int rows;

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

scanf("%d", &rows);

printf("\nStar Pattern:\n");

printStarPattern(rows);

printf("\nNumber Pyramid:\n");

printNumberPyramid(rows);

return 0;

}
Output :

Discussion

1. Challenges:
o Managing the alignment of the patterns required careful calculation
of spaces in the Number Pyramid.
o Understanding nested loops and their interactions for larger patterns
was critical.
2. Optimization:
o The use of separate functions (printStarPattern and
printNumberPyramid) enhances code readability and
modularity.
o Conditional checks in loops can be added for more complex patterns.
3. Learning Outcomes:
o Gained insight into the use of nested loops in generating patterns.
o Understood the importance of formatting in pattern creation.

Conclusion

The lab demonstrated the use of loops and control structures in C programming
to generate visually appealing patterns. By varying the logic in loops, numerous
types of patterns can be created, showcasing the versatility of C programming in
solving such problems. This exercise also emphasized the importance of modular
programming and user input handling
“Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code : CSE 2290


Course Name : Computer Programming Sessional
Experiment No : 15
Experiment Name : : Writing a c program to check whether a given number is
Armstrong or not.

Submitted by Submitted to
Name : Rahul Das Uthsha Das
Lecturer
Roll : 2111022
Department of Computer Science &
Series : 21 Engineering
Rajshahi University of Engineering
& Technology.

Date of Submission:30/11/2024
Exeriment NO. :15
Experiment Name: Write a c program to check whether a given number is
Armstrong or not.
Theory:
An Armstrong number (also known as a narcissistic number, pluperfect digital
invariant, or PPDI) is a number that is equal to the sum of its own digits each raised to
the power of the number of digits.
To determine if a number n is an Armstrong number:

1. Count the number of digits, d.


2. Extract each digit, d, and compute d^d.
3. Sum up all these values and check if the result equals n

Source Code :
Output:

Discussion:

1. Logic Implementation:
o The program uses loops to calculate the number of digits and the
sum of powered digits.
o The pow() function from math.h was utilized to compute the
power of digits efficiently.
2. Performance Considerations:
o The program works efficiently for small to moderate-sized integers.
However, for larger numbers, the time complexity increases because
of repeated computations of the power function.
3. Limitations:
o The program currently handles only positive integers. Modifications
could be made to validate input or extend functionality for different
number systems.
4. Possible Improvements:
o Implementing a custom power function to avoid overhead from the
math.h library.
o Adding input validation to handle non-integer or negative values
gracefull

Conclusion

The C program was successfully developed to identify Armstrong numbers. By


breaking the problem into smaller components such as digit extraction and power
summation, the solution aligns with the theoretical definition. This exercise
demonstrates the power of programming in solving mathematical problems
efficiently.
“Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code : CSE 2290


Course Name : Computer Programming Sessional
Experiment No : 16
Experiment Name : : Writing a program that will print the first n number sequence of
Fibonacci series.

Submitted by Submitted to
Name : Rahul Das Uthsha Das
Lecturer
Roll : 2111022
Department of Computer Science &
Series : 21 Engineering
Rajshahi University of Engineering
& Technology.

Date of Submission:30/11/2024
Experiment No.:16
Experiment Name: Write a program that will print the first n number sequence of
Fibonacci series.

Theory:

The Fibonacci series is a sequence of numbers where each number is the sum of the two
preceding ones. The series starts with 0 and 1, and subsequent numbers are computed as
follows:
F(n)=F(n−1)+F(n-2)
for n≥2n

The sequence begins as: 0, 1, 1, 2, 3, 5, 8, 13, ...

The Fibonacci series is significant in mathematics and computer science because of its
recurrence relation and applications in algorithm design, nature, and mathematical patterns.

Source Code:
Output:

Discussion:
1. Logic Implementation:
The Fibonacci series is implemented using a loop. For the first two terms, the numbers
are directly assigned as 0 and 1. For the subsequent terms, the series is computed
iteratively using the formula F(n)=F(n−1)+F(n−2)
2. Boundary Cases:
o When n≤0 the program prompts the user to input a positive integer.
o The first two terms (0 and 1) are hardcoded as they do not follow the recurrence
relation.
3. Efficiency:
The program uses an iterative approach, which is more memory efficient than recursion
because it does not involve repeated function calls or additional stack space.
4. Extensions:
o The program can be extended to compute Fibonacci numbers using recursion or
matrix exponentiation for larger n
o Adding error handling for non-integer inputs or extremely large numbers can
improve robustness.

Conclusion:
The program effectively demonstrates the generation of the Fibonacci series using a simple
iterative approach. It highlights the use of loops, basic arithmetic operations, and user input in
C programming. This exercise aids in understanding the computational application of
mathematical sequences and recursion-free approaches.

You might also like