Homework 07
Homework 07
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>
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()
scanf("%d", &num);
temp = num;
divisor *= 10;
temp /= 10;
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>
binary /= 10;
base *= 2;
return decimal;
int main()
scanf("%lld", &binary);
int decimal = Decimal(binary);
return 0;
Result:
Question 3: Write a C program to convert a given decimal number to its
binary equivalent.
Code:
#include <stdio.h>
int binary[32];
int index = 0;
if (decimal == 0)
printf("0");
return;
binary[index] = decimal % 2;
decimal /= 2;
index++;
printf("%d", binary[i]);
}
}
int main()
int decimal;
scanf("%d", &decimal);
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 fact = 1;
fact *= i;
return fact;
int main()
int n;
double sum = 0;
scanf("%d", &n);
sum += 1 / factorial(i);
}
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);
if (i % 2 == 0)
if (i % 3 != 0)
}
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()
while (1)
scanf("%lf", &amount);
if (amount == 0)
break;
if (amount < 0)
continue;
}
scanf("%lf", &price);
if (price < 0)
continue;
totalAmount += amount;
totalPrice += price;
if (totalAmount > 0)
else
return 0;
}
Result: