0% found this document useful (0 votes)
18 views14 pages

Cse 4

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)
18 views14 pages

Cse 4

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/ 14

“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

Name : Abdul Alim Uthsha Das


Lecturer
Roll : 2111001 Department of Computer Science &
Engineering
Series : 21 Rajshahi University of Engineering
& Technology.

Date of Submission:30-12-2024
Experiment No:12

Experiment name: Write a program that will print the prime numbers in a given
range.

Theory

Source code:

#include <stdio.h>

int main()

int start, end, i, j, isPrime;

printf("Enter the starting number of the range: ");

scanf("%d", &start);

printf("Enter the ending number of the range: ");

scanf("%d", &end);

printf("Prime numbers between %d and %d are: ", start, end);


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

if (i < 2) continue;

isPrime = 1;

for (j = 2; j * j <= i; j++)

if (i % j == 0)

isPrime = 0;

break;

if (isPrime)

printf("%d ", i);

printf("\n");

return 0;

}
Output:

Description

1. The program reads the start and end values of the range from the user.
2. It iterates through each number in the range.
3. For each number, a nested loop checks if it is divisible by any number from 2 to the
square root of the number. If it is divisible, it is not a prime number.
4. If the number passes the divisibility test, it is printed as a prime number.
5. The program skips numbers less than 2, as they are not prime.
Experiment No:13

Experiment name: Write a program that will check whether a given number is
palindrome or not.

Theory

A palindrome is a number (or text) that reads the same forward and backward. For example:

• 121 is a palindrome because reversing the digits still gives 121.


• 123 is not a palindrome because reversing the digits gives 321, which is different.

To check if a number is a palindrome:

1. Reverse the number.


2. Compare the reversed number with the original number.
3. If both are equal, the number is a palindrome.

Source code:

#include <stdio.h>

int main()

int n, rev = 0, rem, original;

printf("Enter a number: ");

scanf("%d", &n);

original = n;

while (n != 0)

rem = n % 10;

rev = rev * 10 + rem;

n = n / 10;

}
if (rev == original)

printf("Palindrome\n");

} else

printf("Not Palindrome\n");

return 0;

Output
Description

1. The program starts by reading the number n from the user.


2. It stores the original number in the variable original.
3. The program enters a loop where it repeatedly extracts the last digit of the number using
the modulus operator (n % 10) and builds the reversed number by multiplying the current
reversed number (rev) by 10 and adding the extracted digit.
4. After reversing the number, it compares the reversed number (rev) with the original
number (original).
5. If they are equal, the number is a palindrome. Otherwise, it is not.
Experiment No:14

Experiment name: Write a C program to print patterns of numbers or stars.

**

***

****

Theory

The pattern to be printed consists of stars (*) arranged in increasing rows, where each
row contains a number of stars equal to the row number. For example, the first row has
one star, the second row has two stars, and so on.

The goal is to print the following pattern:

**

***

****

Source code:

#include <stdio.h>

int main()

int n, i, j;

printf("Enter number of rows: ");

scanf("%d", &n);

for (i = 1; i <= n; i++)

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

printf("* ");

printf("\n");

return 0;

Output:

Descriptions

1. The program first prompts the user to input the number of rows (n).
2. The outer loop runs from 1 to n, representing each row of the pattern.
3. The inner loop runs from 1 to the current row number (i), printing the stars in each row.
4. After printing all the stars in a row, the program moves to the next line with the
printf("\n") statement.
Experiment No:15

Experiment name: Write a c program to check whether a given number is


Armstrong or not.

Theory:

Source code:

#include <stdio.h>

#include <math.h>

int main()

int n, sum = 0, temp, digit, count = 0;

printf("Enter a number: ");

scanf("%d", &n);

temp = n;

while (temp != 0)
{

temp /= 10;

count++;

temp = n;

while (temp != 0)

digit = temp % 10;

sum += pow(digit, count);

temp /= 10;

if (sum == n)

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

} else {

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

return 0;

}
Output:

Description

1. The program first takes an integer as input.


2. It then calculates the number of digits in the given number by dividing the number by 10
repeatedly until it becomes 0.
3. Next, it calculates the sum of each digit raised to the power of the total number of digits.
4. If the sum equals the original number, it is an Armstrong number; otherwise, it is not.
Experiment No:16

Experiment name: Write a program that will print the first n number sequence of
Fibonacci series

Theory:

Source code:

#include <stdio.h>

int main()

int n, a = 0, b = 1, temp, i;

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

scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 1; i <= n; i++)

{
printf("%d ", a);

temp = a;

a = b;

b = temp + b;

return 0;

Output:

Description

1. The program reads the integer n from the user, which represents the number of terms in
the Fibonacci sequence to print.
2. It initializes the first two terms a = 0 and b = 1.
3. A for loop runs from 1 to n, printing the current term a, then updates the terms: the new a
becomes the old b, and the new b becomes the sum of the previous a and b.
4. The process continues until n terms are printed.

You might also like