1.
Write a c program that prints:
“North South University”
Hello Class of CSE115!! Welcome to NSU.
Code:
#include<stdio.h>
int main()
{
printf("\t \" North South University\"\n");
printf("Hello class of cse115L!! Welcome to NSU. \n");
return 0;
}
2. Write a C program that prompts the user to insert an integer
and a decimal number, then prints the inserted values as output
Code:
#include<stdio.h>
int main()
{
int num;
float deci;
printf("Enter an Integer number: ");
scanf("%d", &num);
printf("The number is %d\n", num);
printf("Enter a decimal number: ");
scanf("%f", &deci);
printf("The number is %.2f\n", deci);
return 0;
}
3.Write a C program that displays the size of different data types.
The program should print the size of the following data types
Code:
#include<stdio.h>
int main()
{
int a;
float b;
double c;
char d;
long int longInt;
signed int no;
printf("Size of int: %zu bytes\n", sizeof(a));
printf("Size of float: %zu bytes\n", sizeof(b));
printf("Size of double: %zu bytes\n", sizeof(c));
printf("Size of char: %zu byte\n", sizeof(d));
printf("Size of Long int: %zu bytes\n", sizeof(longInt));
printf("Size of signed int: %zu bytes\n", sizeof(no));
return 0;
}
4.Write a C program that reads the radius of a circle and prints
its diameter, circumference, and area.
Code:
#include<stdio.h>
int main()
{
float const PI = 3.142;
float radius, area, circumference, diameter;
printf("Enter the radius of a circle: ");
scanf("%f", &radius);
diameter = 2 * radius;
circumference = 2 * PI * radius;
area = PI * radius * radius;
printf("The Diameter is: %.2f \n", diameter);
printf("The Circumference is: %.2f \n", circumference);
printf("The area is: %.2f \n", area);
return 0;
}
5.Write a C program to compute the square root and cube of an
input number.
Code:
#include<stdio.h>
#include<math.h>
int main()
{
float a;
printf("Enter a number: ");
scanf("%f", &a);
printf("Square root:%f, Cube:%f\n", sqrt(a), pow(a, 3));
return 0;
}
6.Write a C program to convert a lowercase letter to uppercase.
Code:
#include<stdio.h>
int main()
{
char a;
printf("Enter a lowercase character: ");
scanf("%c", &a);
printf("Uppercase: %c\n", a - 32);
return 0;
}
7.Write a C program to print all the digits of a 3-digit number.
Code:
#include<stdio.h>
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d", &n);
int last = n % 10;
int others = n / 10;
int second = others % 10;
int first = others / 10;
printf("First digit: %d\n", first);
printf("Second digit: %d\n", second);
printf("Last digit: %d\n", last);
return 0;
}
8.Write a C program to demonstrate the use of bitwise operators
(right shift and left shift) and display the results in both decimal
and hexadecimal formats.
Code:
#include<stdio.h>
int main()
{
int i = 3, num = 48;
printf("Right shift by %d: %d\n", i, num >> i);
printf("\n");
printf("Left shift by %d: %d\n", i, num << i);
// Same as above,except now we’re showing hexadecimal values
printf("Right shift by %x: %x\n", i, num >> i);
printf("\n");
printf("Left shift by %x: %x\n", i, num << i);
return 0;
}
9.Logical Operation
Code:
#include<stdio.h>
int main()
{
int a = 12, b = 39;
printf("AND = %d\n", a & b);
printf("OR = %d\n", a | b);
printf("XOR = %d\n", a ^ b);
printf("1's complement of a = %X\n", ~a); // 1's complm a
printf("2's complement of a = %X\n", -a); // 2's complm a
return 0;
}
10.Write a c program whether a number is Even or Odd.
Code:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}
11.Write a c program to find maximum between two numbers
Code:
#include<stdio.h>
int main()
{
int n1, n2;
printf("Enter any two numbers:\n");
scanf("%d%d", &n1, &n2);
int max = n1;
if (n2 > n1) {
max = n2;
}
printf("Maximum: %d\n", max);
return 0;
}
12.Write a c program to find maximum among three numbers
Code:
#include<stdio.h>
int main()
{
int x, y, z;
printf("Enter three distinct numbers:\n");
scanf("%d%d%d", &x, &y, &z);
if (x >= y && x >= z) // Check if x is the maximum
printf("%d is maximum\n", x);
else if (y >= z) //If x is not max,and check between y and z
printf("%d is maximum\n", y);
else
printf("%d is maximum\n", z);
return 0;
}
13.Write a C program to check if a given year is a leap year
(divisible by 4 but not 100, or divisible by 400)
Code:
#include<stdio.h>
int main()
{
int year;
printf("Enter year: ");
scanf("%d", &year);
/* Check for leap year */
if(((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0))
{
printf("LEAP YEAR\n");
}
else
{
printf("Not Leap Year\n");
}
return 0;
}
14.Write a C program to find the maximum of three numbers using the
ternary operator.
Code:
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Using nested ternary operators to find the maximum
max=(num1>num2)?(num1> num3 ?num1:num3):(num2>num3 ?num2:num3);
printf("The maximum number is: %d\n", max);
return 0;
}
15.Write a C program to check whether the input character is an
alphabet, digit, or special character.
Code:
#include<stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("%c is ALPHABET.\n", ch);
else if (ch >= '0' && ch <= '9')
printf("%c is DIGIT.\n", ch);
else
printf("%c is SPECIAL CHARACTER.\n", ch);
return 0;
}
16.Write a C program to check whether an input number is even or odd
using a switch-case statement.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
switch (num % 2)
{
case 0:
printf("Number is Even\n");
break;
case 1:
printf("Number is Odd\n");
break;
}
return 0;
}
17.Write a C program to calculate the electricity bill based on
the following rates: Tk. 3.50/unit for the first 50 units, Tk.
4.00/unit for the next 100 units, Tk. 5.20/unit for the next 100
units, and Tk. 6.50/unit for units above 250.
Code:
#include<stdio.h>
int main()
{
float bill = 0, units;
printf("Enter the units consumed: ");
scanf("%f", &units);
if (units >= 0 && units <= 50)
{
bill = units * 3.50;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else if (units > 50 && units <= 150)
{
bill = 50 * 3.50 + (units - 50) * 4;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else if (units > 150 && units <= 250)
{
bill = 50 * 3.50 + 100 * 4 + (units - 150) * 5.20;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else if (units > 250)
{
bill = 50 *3.50 +100* 4 +100 *5.20 +(units -250) * 6.50;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else
{
printf("Please enter valid consumed units...\n");
}
return 0;
}
18.Write a C program to check whether an input alphabet is a
vowel or a consonant using a switch-case statement (assume the
input is an English letter).
Code:
#include <stdio.h>
#include <ctype.h> // For tolower() function
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
//Convert the character to lower for case-insensitive comparison
switch (tolower(ch)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
break;
}
return 0;
}
19.Write a C program to calculate the electricity bill based
on the following rates: Tk. 3.50/unit for the first 50 units,
Tk. 4.00/unit for the next 100 units, Tk. 5.20/unit for the
next 100 units, and Tk. 6.50/unit for units above 250.
Code:
#include<stdio.h>
int main()
{
float bill = 0, units;
printf("Enter the units consumed: ");
scanf("%f", &units);
if (units >= 0 && units <= 50)
{
bill = units * 3.50;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else if (units > 50 && units <= 150)
{
bill = 50 * 3.50 + (units - 50) * 4;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else if (units > 150 && units <= 250)
{
bill = 50 * 3.50 + 100 * 4 + (units - 150) * 5.20;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else if (units > 250)
{
bill = 50 *3.50 + 100 * 4 + 100 * 5.20 + (units - 250) *
6.50;
printf("Electricity Bill = %.2f Tk\n", bill);
}
else
{
printf("Please enter valid consumed units...\n");
}
return 0;
}
20. Write a C program that computes the sum of the series 3 + 7 + 11
+ ... up to the nth term, where n is a user input.
Code:
#include <stdio.h>
int main() {
int n, i = 3, sum = 0, count = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
while (count < n) {
sum += i; // Add current term to sum
i += 4; // Increment the term by 4
count++; // Increment the count of terms
}
printf("Sum of the series: %d\n", sum);
return 0;
}
21.Write a C program to print all odd numbers from 1 to n, where n is
a user input.
Code:
#include <stdio.h>
int main() {
int n, i = 1;
// Taking user input for n
printf("Enter a number: ");
scanf("%d", &n);
// Printing all odd numbers from 1 to n
printf("Odd numbers from 1 to %d are: ", n);
while (i <= n) {
if (i % 2 == 1) { // Check if the number is odd
printf("%d ", i); // Print the odd number
}
i++; // Increment i to check the next number
}
return 0;
}
22.Write a C program to read an integer and compute the sum of its
digits.
Code:
#include <stdio.h>
int main() {
int number, sum = 0;
// Taking input from the user
printf("Enter the number: ");
scanf("%d", &number);
printf("The number is: %d\n", number);
// Calculating the sum of digits
while (number != 0) {
sum += number % 10; // Add the last digit to sum
number = number / 10; // Remove the last digit from
number
}
printf("Sum of digits: %d\n", sum);
return 0;
}
23.Write a C program to print all the factors of a number.
Code:
#include <stdio.h>
int main() {
int n, i;
// Taking user input for the number
printf("Enter a positive number: ");
scanf("%d", &n);
// Printing the factors of the number
printf("Factors of %d are: ", n);
for (i = 1; i <= n; i++) {
if (n % i == 0) { // If i is a factor of n
printf("%d ", i); // Print the factor
}
}
return 0;
}
24.Write a C program that takes 10 inputs and prints the sum of all
positive inputs. If the user enters a negative number, it should not be
added to the result.
Code:
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
// Taking 10 inputs and calculating the sum of positive inputs
for (i = 1; i <= 10; ++i) {
printf("Enter number %d: ", i);
scanf("%lf", &number);
if (number < 0.0) {
// Skip negative numbers by continuing to the next
iteration
continue;
}
sum += number; // Add the positive number to the sum
}
// Print the sum of all positive inputs
printf("Sum of positive numbers = %.2lf\n", sum);
return 0;
}
25.Write a C program to check if a number N is a prime number or not,
where N is provided as user input. (Use a for loop for the
implementation.)
Code:
#include <stdio.h>
int main() {
int i, n, isPrime = 1;
// Taking user input
printf("Enter any number to check if it is prime: ");
scanf("%d", &n);
// Checking if the number is prime
if (n <= 1) {
isPrime = 0; // 1 and negative numbers are not prime
} else {
for (i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = 0; // Found a divisor, so it's not prime
break;
}
}
}
// Output result
if (isPrime == 1)
printf("\n%d is a prime number\n", n);
else
printf("\n%d is not a prime number\n", n);
return 0;
}
26.Write a C program to check whether an input number is a perfect
number or not. A perfect number is a positive integer that is equal to
the sum of its proper divisors (excluding itself).
Code:
#include <stdio.h>
int main() {
int i, num, sum = 0;
// Taking user input
printf("Enter any number to check if it is a perfect number: ");
scanf("%d", &num);
// Check for proper divisors and calculate their sum
for (i = 1; i < num; i++) {
if (num % i == 0) {
sum += i; // Add proper divisors to sum
}
}
// Check if the sum of divisors equals the number
if (sum == num && num > 0) {
printf("%d is a PERFECT NUMBER\n", num);
} else {
printf("%d is NOT a PERFECT NUMBER\n", num);
}
return 0;
}
27.Write a C program to input a decimal number from the user and
convert it to the binary number system.
#include <stdio.h>
int main() {
int decimal, temp, binary = 0;
int rem, place = 1;
// Taking user input for decimal number
printf("Enter any decimal number: ");
scanf("%d", &decimal);
// Copy the decimal number to temp for conversion
temp = decimal;
// Converting decimal to binary
while (temp > 0) {
rem = temp % 2; // Get remainder (binary digit)
binary = binary + (rem * place); // Add binary digit to
result
temp /= 2; // Divide the number by 2
place *= 10; // Shift place value (1, 10, 100,
etc.)
}
// Print the decimal number and its binary equivalent
printf("Decimal number = %d\n", decimal);
printf("Binary number = %d\n", binary);
return 0;
}
28.Write a C program that takes numbers as inputs until the user
enters zero and prints the sum of those numbers.
Code:
#include <stdio.h>
int main() {
double number, sum = 0;
// Repeatedly asking for input until 0 is entered
do {
printf("Enter a number: ");
scanf("%lf", &number); // Taking input as a double
sum += number; // Add the entered number to the sum
} while (number != 0.0); // Loop continues until 0 is entered
// Print the final sum
printf("Sum = %.2lf\n", sum);
return 0;
}
29.Write a C program to find the Least Common Multiple (LCM) of two
given numbers using a do-while loop.
Code:
#include <stdio.h>
int main() {
int n1, n2, max, i, lcm;
// Taking input for two numbers
printf("Enter any two numbers to find LCM: ");
scanf("%d %d", &n1, &n2);
// Find the maximum of n1 and n2
max = (n1 > n2) ? n1 : n2;
i = max;
// Using do-while loop to find the LCM
do {
if (i % n1 == 0 && i % n2 == 0) {
lcm = i;
break; // Exit once the LCM is found
}
i += max; // Increment by the maximum number
} while (1);
// Print the result
printf("LCM of %d and %d = %d\n", n1, n2, lcm);
return 0;
}
30.