2024
7th Homework
CSE115L
Farhan Mahtab
CSE115.3
2422232642
Question 1: Write a C program to display a given number in words starting
from its leftmost digit.
Code:
#include <stdio.h>
void Word(int digit)
switch (digit)
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
default:
printf("Invalid ");
int main()
int num, temp, divisor = 1;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp >= 10)
divisor *= 10;
temp /= 10;
while (divisor > 0)
int digit = num / divisor;
Word(digit);
num %= divisor;
divisor /= 10;
printf("\n");
return 0;
Result:
Question 2: Write a C program to convert a given Binary number to its
Decimal equivalent.
Code:
#include <stdio.h>
#include <math.h>
int Decimal(long long binary)
int decimal = 0, base = 1, remainder;
while (binary > 0)
remainder = binary % 10;
decimal += remainder * base;
binary /= 10;
base *= 2;
return decimal;
int main()
long long binary;
printf("Enter a binary number: ");
scanf("%lld", &binary);
int decimal = Decimal(binary);
printf("The decimal equivalent is: %d\n", decimal);
return 0;
Result:
Question 3: Write a C program to convert a given decimal number to its
binary equivalent.
Code:
#include <stdio.h>
void decimalToBinary(int decimal)
int binary[32];
int index = 0;
if (decimal == 0)
printf("0");
return;
while (decimal > 0)
binary[index] = decimal % 2;
decimal /= 2;
index++;
for (int i = index - 1; i >= 0; i--)
printf("%d", binary[i]);
}
}
int main()
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("The binary equivalent is: ");
decimalToBinary(decimal);
printf("\n");
return 0;
Result:
Question 4: Write a C program to compute the sum of the series: 1/1! + 1/2!
+ 1/3! + ... + 1/n! where n is an input.
Code:
#include <stdio.h>
double factorial(int num)
double fact = 1;
for (int i = 1; i <= num; i++)
fact *= i;
return fact;
int main()
int n;
double sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += 1 / factorial(i);
}
printf("The sum of the series is: %.6f\n", sum);
return 0;
Result:
Question 5: Write a C program that prints all even numbers between m and
n (m,n are user inputs) except the ones which are divisible by 3.
Code:
#include <stdio.h>
int main()
int m, n;
printf("Enter m: ");
scanf("%d", &m);
printf("Enter n: ");
scanf("%d", &n);
printf("All even numbers between %d and %d except those divisible by 3 are: ", m, n);
for (int i = m; i <= n; i++)
if (i % 2 == 0)
if (i % 3 != 0)
printf("%d, ", i);
}
printf("\n");
return 0;
Result:
Question 6: Write a C program that asks a shopper to enter amount (in kg)
and total price of sugar he bought from different places. If the shopper mistakenly
enters a negative number as amount/price, it prints an error message “Invalid input,
enter a positive number” and prompts the shopper to give another input. When the
shopper enters 0 as an amount then the program terminates and shows the shopper
total amount, price and average price of sugar per kg.
Code:
#include <stdio.h>
int main()
double amount, price, totalAmount = 0, totalPrice = 0;
while (1)
printf("Enter amount of sugar (in kg): ");
scanf("%lf", &amount);
if (amount == 0)
break;
if (amount < 0)
printf("Invalid input, enter a positive number.\n");
continue;
}
printf("Enter total price for the %.2f kg of sugar: ", amount);
scanf("%lf", &price);
if (price < 0)
printf("Invalid input, enter a positive number.\n");
continue;
totalAmount += amount;
totalPrice += price;
if (totalAmount > 0)
double averagePrice = totalPrice / totalAmount;
printf("\nTotal amount of sugar: %.2f kg\n", totalAmount);
printf("Total price: %.2f\n", totalPrice);
printf("Average price per kg: %.2f\n", averagePrice);
else
printf("No valid data entered.\n");
return 0;
}
Result: