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/ 50
{Questions for Lab 4}
1. Write a C program to check whether a given number is positive, negative, or zero.
Solution:
#include<stdio.h>
int main()
int num;
printf("enter a number:");
scanf("%d",&num);
if(num>0){
printf("\n %d is positive number",num);
else if(num<0){
printf("\n %d is negative number",num);
else{
printf("\n number is zero");
return 0;
Output: 2. Write a C program that checks whether a given integer is odd or even using an if-else condition.
Solution:
#include<stdio.h>
int main()
int num;
printf("enter a numbers:");
scanf("%d",&num);
if(num%2==0){
printf("\n%d is even",num);
else{
printf("\n%d is odd",num);
return 0;
Output: 3. Write a C program to find the largest of three numbers entered by the user using nested if- else.
Solution:
#include <stdio.h>
int main()
int num1,num2,num3;
printf("enter three numbers:");
scanf("%d%d%d",&num1,&num2,&num3);
if((num1>num2)&&(num1>num3)){
printf("%d is largest",num1);
else if(num2>num3){
printf("%d is largest",num2);
else{
printf("%d is largest",num3);
return 0;
Output: 4. Write a C program to create a simple calculator that performs addition, subtraction, multiplication, and division using the switch-case construct.
Solution:
#include <stdio.h>
int main()
int num1,num2,c;
printf("enter two numbers:");
scanf("%d%d",&num1,&num2);
printf("\nfirst number:%d",num1);
printf("\nsecond number:%d",num2);
printf("\nenter the case:");
scanf("%d",&c);
switch(c){
case 1:
printf("\nSum of %d and %d is:%d",num1,num2,num1+num2);
break;
case 2:
if(num1>num2){
printf("\ndiff of %d and %d is:%d",num1,num2,num1-num2);
else{
printf("\ndiff of %d and %d is:%d",num1,num2,num2-num1);
break;
case 3:
printf("\nproduct of %d and %d is:%d",num1,num2,num1*num2);
break;
case 4:
if(num1>num2){ printf("\ndivision of %d and %d is:%d",num1,num2,num1/num2);
else{
printf("\ndiff of %d and %d is:%d",num2,num1,num2/num1);
break;
default:
printf("\nplease enter a valid case");
break;
return 0;
Output: 5. Write a C program to check whether a given character is a vowel or consonant using the switch case.
Solution: #include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if((ch >= 'A' )&&(ch <= 'Z')) {
ch+=32;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
case 'b':
case 'c':
case 'd':
case 'f':
case 'g':
case 'h':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n': case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
printf("%c is a consonant.\n", ch);
break;
default:
printf("Invalid input. Please enter an alphabetic character.\n");
return 0;
Output: 6. Write a C program to check if a given year is a leap year or not using if-else.
Solution:
#include<stdio.h>
int main()
int year;
printf("enter the year:");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0)){
printf("\n %d is Leap year",year);
else{
printf("\n %d is not a Leap year",year);
return 0;
Output: 7. Write a C program that takes a student's marks as input and prints the corresponding grade based on a given grading system using if-else.
Solution:
#include <stdio.h>
int main() {
float marks;
printf("Enter the student's marks (0-100): ");
scanf("%f", &marks);
if (marks < 0 || marks > 100) {
printf("Invalid marks. Please enter a value between 0 and 100.\n");
} else {
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 80) {
printf("Grade: B\n");
} else if (marks >= 70) {
printf("Grade: C\n");
} else if (marks >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
return 0;
} Output: 8. Write a C program to calculate the sum of digits of a given number using a while loop.
Solution:
#include <stdio.h>
int main(){
int num,sum=0,rem,n;
printf("\nEnter a number:");
scanf("%d",&num);
n=num;
while(num!=0){
rem=num%10;
sum+=rem;
num=num/10;
printf("Sum of digits of %d is:%d",n,sum);
return 0;
Output: 9. Write a C program to find the factorial of a given number using a for loop.
Solution:
#include <stdio.h>
int main(){
int num;
long int fact=1;
printf("\nEnter a number:");
scanf("%d",&num);
for(int i=1;i<=num;i++){
fact*=i;
printf("factorial of %d is: %ld",num,fact);
return 0;
Output: 10. Write a C program to reverse the digits of a given number using a while loop.
Solution:
#include <stdio.h>
int main(){
int n,rev=0,rem,num;
printf("\nEnter a number:");
scanf("%d",&n);
num=n;
while(n!=0){
rem=n%10;
rev=rev*10+rem;
n=n/10;
printf("reverse of %d is : %d",num,rev);
return 0;
Output: 11. Write a C program to check whether a given number is prime or not using a for loop.
Solution:
#include<stdio.h>
int main()
int num,isprime=1;
printf("enter a number:");
scanf("%d",&num);
if(num<1)
isprime=0;
for(int i=2;i*i<=num;i++){
if(num%i==0){
isprime=0;
if(isprime==0){
printf("\nnumber is not prime");
else{
printf("number is prime");
return 0;
Output: 12. Write a C program to generate the first n numbers in the Fibonacci series using a while loop.
Solution:
#include <stdio.h>
int main(){
int num,pre=0,cur=1,next=0;
printf("enter the no of terms");
scanf("%d",&num);
for(int i=0;i<num;i++){
if(i==0){
printf("%d\n",cur);
next=cur+pre;
pre=cur;
cur=next;
printf("%d\n",next);
return 0;
Output: 13. Write a C program to find the sum of the first n natural numbers using a for loop.
Solution:
#include <stdio.h>
int main()
int num,sum=0;
printf("enter a number");
scanf("%d",&num);
for(int i=0;i<num;i++){
sum+=(i+1);
printf("sum of first %d natural numbers is:%d",num,sum);
return 0;
Output: 14. Write a C program to generate and display the multiplication table of a given number using a for loop.
Solution:
#include <stdio.h>
int main(){
int i , n ;
printf("\nEnter a number:");
scanf("%d",&n);
printf("\nTable of %d is:",n);
for(i=1;i<=10;i++){
int table=n*i;
printf("\n%d",table);
return 0;
Output: 15. Write a C program to check if a given number is a palindrome using a while loop.
Solution:
#include <stdio.h>
int main() {
int num, n, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &num);
n = num;
while (num != 0) {
rem = num % 10;
rev = rev* 10 + rem;
num /= 10;
if (n== rev) {
printf("%d is a palindrome.\n", n);
} else {
printf("%d is not a palindrome.\n", n);
return 0;
Output: 16. Write a C program to find the GCD of two numbers using a while loop and the Euclidean algorithm.
Solution:
#include <stdio.h>
int main() {
int num1, num2, temp;
printf("Enter two numbers to find their GCD: ");
scanf("%d %d", &num1, &num2);
if (num1 < 0) num1 = -num1;
if (num2 < 0) num2 = -num2;
while (num2 != 0) {
temp = num2;
num2 = num1 % num2;
num1 = temp;
printf("GCD is: %d\n", num1);
return 0;
Output: 17. Write a C program to print the following pattern using a nested for loop:
**
***
****
Solution: to get the above pattern enter n=4
#include <stdio.h>
int main(){
int i , n ;
printf("Enter the no of lines;");
scanf("%d",&n);
for(i=0;i<=n;i++){
for(int j=0;j<i;j++){
printf("*");
printf("\n");
return 0;
Output: 18. Write a C program to find the sum of all odd and even numbers separately up to a given number using a for loop.
Solution:
#include <stdio.h>
int main()
int num,evensum=0,oddsum=0;
printf("enter a number");
scanf("%d",&num);
for(int i=1;i<=num;i++){
if(i%2){
oddsum+=i;
else{
evensum+=i;
}}
printf("\nsum of even numbers is:%d",evensum);
printf("\nsum of odd numbers is:%d",oddsum);
return 0;
Output: 19. Write a C program to check whether a given number is an Armstrong number or not using a while loop.
Solution:
#include <stdio.h>
#include <math.h>
int main() {
int num, n, rem, count = 0, sum = 0;
printf("Enter an integer: ");
scanf("%d", &num);
n= num;
while (n!= 0) {
n /= 10;
count++;
n = num;
while (num!= 0) {
rem= num % 10;
sum += pow(rem, count);
num/= 10;
if (sum == n) {
printf("%d is an Armstrong number.\n", n);
} else {
printf("%d is not an Armstrong number.\n", n);
return 0;
Output: 20. Write a C program to generate a Celsius-to-Fahrenheit conversion table for values from 0 to 100 using a for loop.
Solution:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Celsius\tFahrenheit\n");
printf("---------------------\n");
for (celsius = 0; celsius <= 100; celsius++) {
fahrenheit = (celsius * 9.0 / 5.0) + 32;
printf("%.1f\t%.1f\n", celsius, fahrenheit);
return 0;
Output: 21. Write a C program to find the roots of a quadratic equation. Use an if-else ladder to handle different cases (real and equal roots, real and unequal roots, complex roots).
Solution:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
if (a == 0) {
printf("Not a quadratic equation (a should not be 0).\n");
printf("Invalid input! Income cannot be negative.\n");
return 1;}
totalTax = calculateIncomeTax(income);
printf("Total income tax: ₹%.2f\n", totalTax);
return 0;
} Output: 27. Write a C program to input a student's marks in five subjects and calculate the percentage. Based on the percentage, display the grade using an if-else ladder:
90% and above: A+
80%-89%: A
70%-79%: B
60%-69%: C
Below 60%: Fail
Solution:
#include <stdio.h>
void grade(int a,int b,int c,int d,int e){
float per=(a+b+c+d+e)/5;
if(per>90)
printf("Grade=A+");
else if(per>89)
printf("Grade=A");
else if(per>79)
printf("Grade=B");
else if(per>69)
printf("Grade=C");
else
printf("Fail");
int main(){
int mark1,mark2,mark3,mark4,mark5;
printf("enter the marks of 1st subject:");
scanf("%d",&mark1);
printf("enter the marks of 2nd subject:");
scanf("%d",&mark2);
printf("enter the marks of 3rd subject:");
scanf("%d",&mark3);
printf("enter the marks of 4th subject:");
scanf("%d",&mark4);
printf("enter the marks of 5th subject:");
scanf("%d",&mark5);
grade(mark1,mark2,mark3,mark4,mark5);
return 0;
Output: 28. Write a C program to simulate a simple ATM system with the following options:
1: Balance Inquiry
2: Cash Withdrawal
3: Cash Deposit Use a switch-case to handle the different options, ensuring that sufficient balance is checked for withdrawal.
Solution:
#include <stdio.h>
int main() {
int option;
float balance = 10000.0;
float amount;
printf("\nATM Menu:\n");
printf("1: Balance Inquiry\n");
printf("2: Cash Withdrawal\n");
printf("3: Cash Deposit\n");
printf("4: Exit\n");
printf("Select an option: ");
scanf("%d", &option);
switch (option) {
case 1:
printf("Your current balance is: ₹%.2f\n", balance);
break;
case 2:
printf("Enter amount to withdraw: ₹");
scanf("%f", &amount);
if (amount > balance) {
printf("Insufficient balance for withdrawal.\n");
} else {
balance -= amount; printf("Withdrawal successful! New balance: ₹%.2f\n", balance);
break;
case 3:
printf("Enter amount to deposit: ₹");
scanf("%f", &amount);
balance += amount;
printf("Deposit successful! New balance: ₹%.2f\n", balance);
break;
case 4:
printf("Thank you for using the ATM! Goodbye.\n");
return 0;
default:
printf("Invalid option. Please try again.\n");
return 0;
Output: 29. Write a C program to calculate the parking fee in a mall:
First 2 hours: ₹30
Each additional hour: ₹10 Input the total time in hours and calculate the parking fee using a for loop.
Solution:
#include <stdio.h>
int calculateParkingFee(int hours) {
int fee;
if (hours <= 2) {
fee = 30;
} else {
fee = 30 + (hours - 2) * 10;
return fee;
int main() {
int hours;
printf("Enter total parking time in hours: ");
scanf("%d", &hours);
if (hours < 0) {
printf("Invalid input! Parking time cannot be negative.\n");
return 1;
int totalFee = calculateParkingFee(hours);
printf("Total parking fee: ₹%d\n", totalFee);
return 0;
Output: 30. Write a C program to calculate the monthly instalment (EMI) for a loan based on principal, interest rate, and tenure. Use if-else conditions to check valid input.
Output: 31. Write a C program to calculate BMI based on weight and height. Use if-else conditions to classify the BMI into categories:
Underweight: BMI < 18.5
Normal weight: 18.5 ≤ BMI < 24.9
Overweight: 25 ≤ BMI < 29.9
Obesity: BMI ≥ 30
Solution:
#include <stdio.h>
float BMI(float w,float h){
return w/(h*h);
int main(){
float weight,height,bmi;
printf("enter Height in m:");
scanf("%f",&height);
printf("enter weight in kg:");
scanf("%f",&weight);
bmi=BMI(weight,height);
if(bmi<18.5){
printf("Underweight");
else if(bmi<24.9){
printf("normal weight");
else if(bmi<29.9){
printf("overweight");
else{
printf("Obesity"); }
return 0;} Output: 32. Write a C program to calculate the monthly water bill based on usage:
Up to 1000 liters: ₹5 per 100 liters
1001 to 5000 liters: ₹7 per 100 liters
Above 5000 liters: ₹10 per 100 liters Use if-else conditions to calculate the bill.
Solution:
#include <stdio.h>
float Bill(float w){
if(w<1000){
return (w/100)*5;
else if(w<5000){
return 10*5+((w-1000)/100)*7;
else{
return 10*5+40*7+((w-5000)/100)*10;
int main(){
float water,bill;
printf("enter your monthly water usage:");
scanf("%f",&water);
bill=Bill(water);
printf("your monthly water bill is %0.2f",bill);
return 0;
Output: 33. Write a C program to manage a simple shopping cart. Allow the user to add items with prices, calculate the total cost, and apply a discount if the total exceeds ₹5000. Use loops for multiple entries and if-else for the discount.
Solution:
#include <stdio.h>
int main() {
int item = 1;
float price, totalCost ;
char addMore;
printf("Welcome to the Shopping Cart!\n");
do {
printf("Enter the price of item %d: ₹", item);
scanf("%f", &price);
totalCost += price;
item++;
printf("Do you want to add another item? (y/n): ");
scanf(" %c", &addMore);
} while (addMore == 'y' || addMore == 'Y');
printf("\nTotal cost of items: ₹%.2f\n", totalCost);