Lab 03 Assignment No - 03

You might also like

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

Shanto-Mariam University of

Creative Technology
(SMUCT)

Assignment

Course Title : Structured Programming


Sessional
Course Code : CSE-1102
Assignment No : 03
Task No :
Assignment Name : Lab Assignment 3
Submitted To:
Name: Md. Tousif Hassan Lavlu
Department of: CSE & CSIT
Shanto-Mariam University
of Creative Technology.

Submitted By:
Name: Md Jubaer
ID No: 241071051
Batch: 34th
Section: B
Program: B.sc Eng. in CSE

LAB-03

Task-1
Write a program to enter a 4 digits number from the keyboard. Add 8 to the
number and then divide it by 3. Now, the modulus of that number is taken with 5
and then multiply the resultant value by 5. Display the final result.

#include <stdio.h>
//Md.Jubaer
//ID-241071051

int main() {
int number;
float result;

// Input a 4-digit number from the user


printf("Enter a 4-digit number: ");
scanf("%d", &number);

number += 8;
result = (float)number / 3;

result = (int)result % 5;
result *= 5;

printf("Final result: %.2f\n", result);

return 0;
}

Task-2
Enter two numbers from the keyboard. Write a program to check if the two
numbers are equal.

#include <stdio.h>
#include <stdlib.h>
//Jubaer
//ID-241071051

int main() {
int A, B;

// Input two numbers from the user


printf("Enter the 1st number: ");
scanf("%d", &A);
printf("Enter the 2nd number: ");
scanf("%d", &B);

if (A == B)
{
printf("The two numbers are equal.\n");
}
else
{
printf("The two numbers are not equal.\n");
}

return 0;
}

Task-3
Write a program to enter the values of two variables ‘a’ and ‘b’ from the
keyboard and then
check if both the conditions ‘a<50’ and ‘a<b’ are true.

#include <stdio.h>

int main() {
int a, b;

printf("Enter the value of 'a': ");


scanf("%d", &a);
printf("Enter the value of 'b': ");
scanf("%d", &b);

if (a < 50 && a < b) {


printf("Both conditions 'a < 50' and 'a < b' are true.\n");
} else {
printf("At least one of the conditions is false.\n");
}

return 0;
}

Task-4
Now solve the above question to check if at least one of the conditions
‘a<50’ and ‘a<b’ is true.

#include <stdio.h>

int main() {
int a, b;

printf("Enter the value of 'a': ");


scanf("%d", &a);

printf("Enter the value of 'b': ");


scanf("%d", &b);

if (a < 50 || a < b) {
printf("At least one of the conditions 'a < 50' and 'a < b' is true.\n");
} else {
printf("Both conditions are false.\n");
}

return 0;
}

Task-5
If the marks of Robert in three subjects are 78,45 and 62 respectively (each
out of 100), write a program to calculate his total marks and percentage
marks.

#include <stdio.h>

int main() {
int marks1 = 78, marks2 = 45, marks3 = 62;
int total_marks;
float percentage;

total_marks = marks1 + marks2 + marks3;


percentage = (float)total_marks / 300 * 100;

printf("Total marks: %d\n", total_marks);


printf("Percentage marks: %.2f%%\n", percentage);

return 0;
}

Task-6
Write a program to enter the values of two variables from the keyboard and
then interchange the values of the two variables. E.g

If entered value of x is 5 and y is 10 then


should print 10 and 5.

#include <stdio.h>

int main() {
int x, y, temp;

printf("Enter the value of x: ");


scanf("%d", &x);

printf("Enter the value of y: ");


scanf("%d", &y);

temp = x;
x = y;
y = temp;

printf("After interchange:\n");
printf("Value of x: %d\n", x);
printf("Value of y: %d\n", y);

return 0;
}

Task-7
The total number of students in a class are 45 out of which 25 are boys. If
80% of the total students secured grade ‘A’ out of which 17 are boys, then
write a program to calculate the total number of girls getting grade ‘A’.

#include <stdio.h>

int main() {
int total_students = 45;
int boys_total = 25;
int boys_grade_A = 17;
float grade_A_percentage = 80;

float total_grade_A = total_students * (grade_A_percentage / 100);


float girls_grade_A = total_grade_A - boys_grade_A;

printf("Total number of girls securing grade 'A': %.0f\n", girls_grade_A);

return 0;
}

Task-8
Write a program to calculate the sum of the first and the second last digit of
a 5 digits number.
Sample output:
NUMBER: 12345
OUTPUT: 1+4=5

#include <stdio.h>

int main() {
int number, first_digit, second_last_digit, sum;

printf("Enter a 5-digit number: ");


scanf("%d", &number);
first_digit = number / 10000;

second_last_digit = (number / 10) % 10;

sum = first_digit + second_last_digit;

printf("NUMBER: %d\n", number);


printf("OUTPUT: %d+%d=%d\n", first_digit, second_last_digit, sum);

return 0;
}

Task-9
Take a 4 digits number. Write a program to display a number whose digits
are 2 greater than the corresponding digits of the number TAKEN.
For example, if the number which was taken is 5696, then the displayed
number should be
7818.

#include <stdio.h>

int main() {
int number, displayed_number = 0;
int multiplier = 1;

printf("Enter a 4-digit number: ");


scanf("%d", &number);

while (number > 0) {

int digit = number % 10;

displayed_number += (digit + 2) * multiplier;

multiplier *= 10;

number /= 10;
}

printf("Displayed number: %d\n", displayed_number);

return 0;
}
Task-10
Write a program to calculate the sum of the digits of a 3-digit number.
Sample output:
Number: 132
Output: 6

#include <stdio.h>

int main() {
int number, sum = 0, digit;

printf("Enter a 3-digit number: ");


scanf("%d", &number);

// Extracting digits and summing them


digit = number % 10; // Extract the units digit
sum += digit; // Add units digit to sum
number /= 10; // Remove the units digit

digit = number % 10; // Extract the tens digit


sum += digit; // Add tens digit to sum
number /= 10; // Remove the tens digit

digit = number; // Remaining number is hundreds digit


sum += digit; // Add hundreds digit to sum

printf("Sum of the digits: %d\n", sum);

return 0;
}
Task-11
Write a program to reverse a 3-digit number.
Sample output:
Number: 132
Output: 231

#include <stdio.h>

int main() {
int number, reverse = 0;

printf("Enter a 3-digit number: ");


scanf("%d", &number);

while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}

printf("Number: %d\n", reverse);


return 0;
}

You might also like