0% found this document useful (0 votes)
7 views

Cse Assignment

The document contains a series of programming assignments for a course on Computer Programming and Applications at Rajshahi University of Engineering & Technology. Each problem includes a description and a corresponding C program that addresses various programming concepts such as arithmetic operations, temperature conversion, area calculation, and more. The assignments cover a range of topics including control structures, functions, and data manipulation.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Cse Assignment

The document contains a series of programming assignments for a course on Computer Programming and Applications at Rajshahi University of Engineering & Technology. Each problem includes a description and a corresponding C program that addresses various programming concepts such as arithmetic operations, temperature conversion, area calculation, and more. The assignments cover a range of topics including control structures, functions, and data manipulation.

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

“Heaven’s light is our guide”

Rajshahi University of Engineering & Technology

DEPARTMENT OF CHEMICAL ENGINEERING

Course Code : CSE 2289

Course Name : Computer Programming and Applications

Assignment Name : Problem Solving by Computer Programming

Submitted by Submitted to
Utsha Das
Name : Md. Abdur Rahman
Lecturer
Roll 2111025
Department of Computer Science &
Engineering
Series 21
Rajshahi University of Engineering &
Technology.
Date of Submission: 18-02-25
Problem no : 01
Problem Name: Writing a program that inputs two integer number and show the
summation, difference, multiplication and division of that two numbers.

Source Code
Here is a simple C program that implements the required functionality:

#include <stdio.h>
int main() {
int num1, num2;
int sum, difference, product;
float division; // Division result is stored as a float for decimal precision
// Prompt the user to input two integers
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
// Perform arithmetic
operations sum = num1 +
num2; difference = num1 -
num2; product = num1 *
num2;
// Display results
printf("Summation: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Multiplication: %d\n", product);
// Check for division by zero
if (num2 != 0) {
division = (float)num1 / num2; // Casting to float for precise division
printf("Division: %.2f\n", division);
} else {
printf("Division: undefined (cannot divide by zero)\n");
}
return 0;
}
Problem no : 02
Problem Name: Writing a program that read temperature in Celsius
and display in Fahrenheit.

Source Code

#include <stdio.h>
int main() {
float celsius, fahrenheit;
// Prompt the user to enter the temperature in Celsius
printf("Enter the temperature in Celsius: ");
scanf("%f", &celsius);
// Calculate Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;
// Display the result
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}

Problem no : 03
Problem Name: Writing a program that read radius of a circle and display its
Area.

Source Code

#include <stdio.h>
#define PI 3.14159 // Defining the constant value of π
int main() {
float radius, area;
// Prompt the user to enter the radius
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate the area
area = PI * radius * radius;
// Display the area
printf("The area of the circle is: %.2f\n", area);
return 0;
}
Problem no : 04
Problem name : Write a program that check odd or even using modular
operator and bitwise operator.

Source Code

#include <stdio.h>
void checkOddEvenModular(int number)
{ if (number % 2 == 0) {
printf("%d is Even (using modular operator)\n", number);
} else {
printf("%d is Odd (using modular operator)\n", number);
}
}
void checkOddEvenBitwise(int number)
{ if (number & 1) {
printf("%d is Odd (using bitwise operator)\n", number);
} else {
printf("%d is Even (using bitwise operator)\n", number);
}
}
int main()
{ int number;
printf("Enter an integer: ");
scanf("%d", &number);
checkOddEvenModular(number);
checkOddEvenBitwise(number);
return 0;
}

Problem no : 05
Problem name : Write a program that read any angle and display cos, tan , sec and
cosec.

Source Code

The following C program reads an angle in degrees and calculates its cosine, tangent,
secant, and cosecant:
#include <stdio.h>
#include <math.h>

int main() {

double angle_degrees, angle_radians;


double cosine, tangent, secant, cosecant;
printf("Enter an angle in degrees: ");
scanf("%lf", &angle_degrees);
angle_radians = angle_degrees * (M_PI / 180.0);
cosine = cos(angle_radians);
tangent = tan(angle_radians);
secant = (cosine != 0) ? (1 / cosine) : NAN; // Check for division by zero
cosecant = (sin(angle_radians) != 0) ? (1 / sin(angle_radians)) : NAN; // Check for
division by zero
printf("For angle %.2f degrees:\n", angle_degrees);
printf("Cosine: %.4f\n", cosine);
printf("Tangent: %.4f\n", tangent);
printf("Secant: %.4f\n", secant);
printf("Cosecant: %.4f\n", cosecant);
return 0;
}

Problem no : 06
Problem name : Write a program that generates random number
between 1 to 6.

Source Code

The following C program generates a random number between 1 and 6:

#include<stdio.h>

#include<stdlib.h>
int main()
{
int i,a;
printf("enter number = ");
scanf("%d",&a);
for(i=0;i<a;i++)
printf("\n%d ", 1+ rand()%6);
return 0;
}

Problem no : 07

Problem name : Write a program that read any year and display leap

year or not leap year.

Source Code:
#include <stdio.h>
int main()

int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d is a Leap Year.\n", year);

else

printf("%d is not a Leap Year.\n", year);

}
return 0;}

Problem no : 08

Problem name : Write a program that read three numbers (a,b,c) and determine
the roots of the quadratic equation: ax2 + bx + c=0.

Source Code

#include <stdio.h>

#include <math.h>

int main()
{

float a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b, and c: ");

scanf("%f %f %f", &a, &b, &c);

if (a == 0) {

printf("This is not a quadratic equation. The value of 'a' cannot be zero.\n");

return 0;

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("The equation has two real and distinct roots: %.2f and %.2f\n", root1,
root2);
}

else if (discriminant == 0)

{ root1 = -b / (2 * a);

printf("The equation has one real root: %.2f\n", root1);

else {

float realPart = -b / (2 * a);

float imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("The equation has two complex roots: %.2f + %.2fi and %.2f -
%.2fi\n", realPart, imaginaryPart, realPart, imaginaryPart);

return 0;

Problem no : 09

Problem name : Write a program that read numbers and display median using
conditional operator.

Source Code

#include <stdio.h>

int main()
{

int a, b, c, median;
printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

median = (a > b) ? ((a < c) ? a : (b > c ? b : c)) : ((b < c) ? b : (a > c ? a : c));

printf("The median is: %d\n", median);

return 0;

Problem no : 10

Problem name : Write a program that read a mark and display its grade using
switch statement.

Source Code

#include <stdio.h>

int main()

{ int mark;

char grade;

printf("Enter your mark: ");

scanf("%d", &mark);

switch (mark / 10) {

case 10:

case 9:

grade = 'A';

break;
case 8:

grade = 'B';

break;

case 7:

grade = 'C';

break;

case 6:

grade = 'D';

break;

default:

grade = 'F';

break;

printf("Your grade is: %c\n", grade);

return 0;

Problem no : 11

Problem name : Write a program that read any number and display

equivalent roman number using switch.

Source Code

#include <stdio.h>
int main()

{ int num;

printf("Enter a number between 1 and 10: ");

scanf("%d", &num);

switch(num)

{ case 1:

printf("The Roman numeral for %d is I\n", num);

break;

case 2:

printf("The Roman numeral for %d is II\n", num);

break;

case 3:

printf("The Roman numeral for %d is III\n", num);

break;

case 4:

printf("The Roman numeral for %d is IV\n", num);

break;

case 5:

printf("The Roman numeral for %d is V\n", num);

break;

case 6:

printf("The Roman numeral for %d is VI\n", num);

break;
case 7:

printf("The Roman numeral for %d is VII\n", num);

break;

case 8:

printf("The Roman numeral for %d is VIII\n", num);

break;

case 9:

printf("The Roman numeral for %d is IX\n", num);

break;

case 10:

printf("The Roman numeral for %d is X\n", num);

break;

default:

printf("Invalid input! Please enter a number between 1 and 10.\n");

return 0;

Problem no :12

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

Source Code

#include <stdio.h>
int isPrime(int num) {

if (num <= 1) return 0;

for (int i = 2; i * i <= num; i++)

{ if (num % i == 0) {

return 0;

return 1;

int main() {

int lower, upper;

printf("Enter the lower bound: ");

scanf("%d", &lower);

printf("Enter the upper bound: ");

scanf("%d", &upper);

printf("Prime numbers between %d and %d are:\n", lower, upper);

for (int num = lower; num <= upper; num++) {

if (isPrime(num))

{ printf("%d ", num);

printf("\n");
return 0; }

Problem no :13

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

Source Code

#include <stdio.h>

int isPalindrome(int num)

{ int originalNum = num;

int reversedNum = 0;

int remainder;

while (num != 0) {

remainder = num % 10; // Get the last digit

reversedNum = reversedNum * 10 + remainder;

num /= 10;

return originalNum == reversedNum;

int main()

{ int

number;

printf("Enter an integer: ");

scanf("%d", &number);

if (isPalindrome(number)) {
printf("%d is a palindrome.\n", number);

} else {

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

return 0; }

Problem no :14

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

Source Code

#include <stdio.h>

void printStarPattern(int n)

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

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

{ printf("* ");

printf("\n");

void printNumberPattern(int n)

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

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

{ printf("%d ", j);

printf("\n");
}

int main() {

int choice, n;

printf("Choose the pattern to print:\n");

printf("1. Star Pattern\n");

printf("2. Number Pattern\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

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

scanf("%d", &n);

if (choice == 1)

{ printf("Star Pattern:\

n"); printStarPattern(n);

} else if (choice == 2)

{ printf("Number Pattern:\

n"); printNumberPattern(n);

} else {

printf("Invalid choice.\n");

return 0;

}
Problem no : 15

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


Armstrong or not.

Source Code

#include <stdio.h>

#include <math.h>

int isArmstrong(int num)

{ int originalNum =

num; int sum = 0;

int digits = 0;

while (originalNum != 0) {

originalNum /= 10;

digits++;

originalNum = num;

while (num != 0) {

int remainder = num % 10;

sum += pow(remainder, digits);

num /= 10; // Remove the last digit

return sum == originalNum;

int main() {
int number;

printf("Enter an integer: ");

scanf("%d", &number);

if (isArmstrong(number)) {

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

} else {

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

return 0;

Problem no : 16

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

Source Code

#include <stdio.h>

void printFibonacci(int n) {

int first = 0, second = 1, next;

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;

printf("Enter the number of terms in the Fibonacci series: ");

scanf("%d", &n);

if (n <= 0) {

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

} else {

printFibonacci(n);

return 0;

Problem No : 17

Problem Name: Writing a program that deletes any number from an array.

Source code:
#include <stdio.h>

void deleteNumber(int arr[], int *size, int num) {

int i, pos = -1;

// Search for the element to be deleted

for (i = 0; i < *size; i++) {

if (arr[i] == num)

{ pos = i;

break;

if (pos == -1) {

printf("Number %d not found in the array.\n", num);

return;

// Shift elements to the left

for (i = pos; i < *size - 1; i++) {

arr[i] = arr[i + 1];

// Decrease array size

(*size)--;

printf("Number %d deleted successfully.\n", num);

}
void displayArray(int arr[], int size)

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[100], size, num, i;

// Input array size

printf("Enter the number of elements in the array: ");

scanf("%d", &size);

// Input array elements

printf("Enter %d elements:\n", size);

for (i = 0; i < size; i++) {

scanf("%d", &arr[i]);

// Input the number to be deleted

printf("Enter the number to be deleted: ");

scanf("%d", &num);
// Perform deletion

deleteNumber(arr, &size, num);

// Display the updated array

printf("Updated array:\n");

displayArray(arr, size);

return 0;

Problem No : 18

Problem Name: Write a program that read and sort an array using bubble sort in
ascending or descending order.

Source code:

#include <stdio.h>
void bubbleSort(int arr[], int n, int order)
{ int i, j, temp;
int swapped;

for (i = 0; i < n - 1; i++) {


swapped = 0; // Track if a swap occurred
for (j = 0; j < n - i - 1; j++) {
if ((order == 1 && arr[j] > arr[j + 1]) || (order == 2 && arr[j] < arr[j + 1])) {
// Swap elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}
// If no elements were swapped, break early
if (!swapped)
break;
}
}

int main() {
int n, order, i;

// Input array size


printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

// Input array elements


printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Input sorting order


printf("Choose sorting order:\n1. Ascending\n2. Descending\n");
scanf("%d", &order);

// Sort the array


bubbleSort(arr, n, order);

// Output sorted array


printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
Problem No : 19

Problem Name: Write a program that reads a decimal number and display
equivalent binary number.
Source code:

#include <stdio.h>
void convertToBinary(int decimal) {
int binary[32]; // Array to store binary digits
int index = 0; // Index for the binary array

// Edge case: Handle zero directly


if (decimal == 0) {
printf("Binary: 0\n");
return;
}

// Conversion process
while (decimal > 0) {
binary[index] = decimal % 2; // Store remainder (binary digit)
decimal /= 2; // Update decimal to the quotient
index++;
}

// Display the binary number in reverse order


printf("Binary: ");
for (int i = index - 1; i >= 0; i--)
{ printf("%d", binary[i]);
}
printf("\n");
}

int main()
{ int
decimal;

// Input from the user


printf("Enter a decimal number: ");
scanf("%d", &decimal);

// Function call to perform conversion


convertToBinary(decimal);

return 0;
}

Problem No :20

Problem Name:Write a program that multiplies two matrices.


Source Code
#include <stdio.h>
void multiplyMatrices(int rowsA, int colsA, int rowsB, int colsB, int
A[rowsA][colsA], int B[rowsB][colsB], int C[rowsA][colsB]) {
if (colsA != rowsB) {
printf("Matrix multiplication not possible.\n");
return;}
for (int i = 0; i<rowsA; i++)
{ for (int j = 0; j <colsB; j++)
{
C[i][j] = 0;}}
for (int i = 0; i<rowsA; i++) {
for (int j = 0; j <colsB; j++) {
for (int k = 0; k <colsA; k++) { C[i]
[j] += A[i][k] * B[k][j];}}}}

void displayMatrix(int rows, int cols, int matrix[rows][cols])


{ for (int i = 0; i< rows; i++) {
for (int j = 0; j < cols; j++)
{ printf("%d ", matrix[i][j]);}
printf("\n");}}
int main() {
int rowsA = 2, colsA = 3, rowsB = 3, colsB = 2;
int A[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int B[3][2] = {
{7, 8},
{9, 10},
{11, 12}};
int C[2][2];
printf("Matrix A:\n");
displayMatrix(rowsA, colsA, A); printf("\
nMatrix B:\n"); displayMatrix(rowsB,
colsB, B);
multiplyMatrices(rowsA, colsA, rowsB, colsB, A, B, C);
printf("\nResultant Matrix C (A * B):\n");
displayMatrix(rowsA, colsB, C);
}
Problem no 21

Problem name : Write a program that read a line of text from a file and convert it
upper to lower and lower to upper and calculate the frequency of each character.

Source Code

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#define MAX_SIZE 1000

void toggle_case_and_frequency(FILE *input_file, FILE *output_file)

{ char line[MAX_SIZE];

int freq[256] = {0};

if (fgets(line, sizeof(line), input_file) != NULL)

{ for (int i = 0; line[i] != '\0'; i++) {

char c = line[i];

if (isupper(c)) {

c = tolower(c);

} else if (islower(c)) {

c = toupper(c);

fputc(c, output_file);

freq[(unsigned char)c]++;

}
fprintf(output_file, "\nCharacter frequencies:\n");

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

if (freq[i] > 0) {

fprintf(output_file, "%c: %d\n", i, freq[i]);

int main() {

FILE *input_file = fopen("input.txt", "r");

if (input_file == NULL) {

printf("Error opening input file\n");

return 1;

FILE *output_file = fopen("output.txt", "w");

if (output_file == NULL) {

printf("Error opening output file\n");

return 1;

toggle_case_and_frequency(input_file, output_file);

fclose(input_file);

fclose(output_file);

return 0;

}
Problem no 22

Problem name : Write a program that read all numbers in a file and write another
file in ascending or descending order.

Source Code

#include <stdio.h>

#include <stdlib.h>

void sort_numbers(FILE *input_file, FILE *output_file, int ascending)

{ int numbers[100], count = 0;

while (fscanf(input_file, "%d", &numbers[count]) != EOF) { count+

+;

for (int i = 0; i < count - 1; i++)

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

if ((ascending && numbers[i] > numbers[j]) || (!ascending && numbers[i] <


numbers[j])) {

int temp = numbers[i];

numbers[i] = numbers[j];

numbers[j] = temp;

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


fprintf(output_file, "%d\n", numbers[i]);

int main() {

FILE *input_file = fopen("input_numbers.txt", "r");

if (input_file == NULL) {

printf("Error opening input file\n");

return 1;

FILE *output_file = fopen("sorted_numbers.txt", "w");

if (output_file == NULL) {

printf("Error opening output file\n");

return 1;

int ascending = 1; // Change to 0 for descending order

sort_numbers(input_file, output_file, ascending);

fclose(input_file);

fclose(output_file);

return 0;

}
Problem no 23

Problem name : Write a program that gets the input of structure from a file and
output display in another file.

Source Code

#include <stdio.h>

#include <string.h>

struct Student

{ char name[50];

int age;

float grade;

};

void process_student_data(FILE *input_file, FILE *output_file)

{ struct Student student;

while (fscanf(input_file, "%s %d %f", student.name, &student.age, &student.grade)


!= EOF) {

fprintf(output_file, "Name: %s\n", student.name);

fprintf(output_file, "Age: %d\n", student.age);

fprintf(output_file, "Grade: %.2f\n\n", student.grade);

int main() {

FILE *input_file = fopen("student_data.txt", "r");

if (input_file == NULL) {
printf("Error opening input file\n");

return 1;

FILE *output_file = fopen("output_student_data.txt", "w");

if (output_file == NULL) {

printf("Error opening output file\n");

return 1;

process_student_data(input_file, output_file);

fclose(input_file);

fclose(output_file);

return 0;

You might also like