Write a C program to compute both Simple Interest and Compound Interest based on user
inputs. Break down your explanation to include the role of headers, variables, input/output
statements, and logic blocks in the program.
Simple Interest and Compound Interest//Program to calculate Simple Interest and
Compound Interest
#include <stdio.h>
#include <math.h> // Required for the pow() function
int main() {
// Declare variables
float principal, rate, time;
float simpleInterest, compoundInterest;
// Input from the user
printf("Enter Principal amount: ");
scanf("%f", &principal);
printf("Enter Rate of Interest (in %% per annum): ");
scanf("%f", &rate);
printf("Enter Time (in years): ");
scanf("%f", &time);
// Calculate Simple Interest
simpleInterest = (principal * rate * time) / 100;
// Calculate Compound Interest
compoundInterest = principal * pow((1 + rate / 100), time) - principal;
// Output the results
printf("\nSimple Interest = %.2f\n", simpleInterest);
printf("Compound Interest = %.2f\n", compoundInterest);
return 0;
}
Discuss and demonstrate the differences between formatted and unformatted I/O
operations in C with examples.
Formatted I/O Operations
Formatted I/O functions allow input and output of data with a specific format. These functions
interpret format specifiers and handle various data types like integers, floats, characters, strings,
etc.
Common Formatted I/O Functions:
printf() – outputs formatted data to the screen
scanf() – reads formatted input from the keyboard
%d is a format specifier for an integer.
%f is for floating-point numbers.
Example
#include <stdio.h>
int main() {
int age;
float salary;
// Formatted Input
printf("Enter your age and salary: ");
scanf("%d %f", &age, &salary);
// Formatted Output
printf("You are %d years old and earn $%.2f per month.\n", age, salary);
return 0;
}
Unformatted I/O Operations
Unformatted I/O functions handle data as raw characters or blocks without interpreting format
specifiers. These are simpler and often used for character or string input/output.
Common Unformatted I/O Functions:
getchar() – reads a single character
putchar() – writes a single character
Example
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar(); // Read a single character
printf("You entered: ");
putchar(ch); // Print the same character
putchar('\n'); // Print a newline
return 0;
}
Write a C program to perform all basic arithmetic operations (addition, subtraction,
multiplication, division, and modulus) on two user-input integers. Display the results
clearly.
#include <stdio.h>
int main() {
int a, b;
int sum, diff, product, remainder;
float division;
// Input
printf("Enter two integers (a and b): ");
scanf("%d %d", &a, &b);
// Operations
sum = a + b;
diff = a - b;
product = a * b;
division = (float)a / b; // Type cast to float for accurate result
remainder = a % b;
// Output
printf("\n--- Results ---\n");
printf("Sum: %d + %d = %d\n", a, b, sum);
printf("Difference: %d - %d = %d\n", a, b, diff);
printf("Product: %d * %d = %d\n", a, b, product);
printf("Division: %d / %d = %.2f\n", a, b, division);
printf("Remainder: %d %% %d = %d\n", a, b, remainder);
return 0;
}
Classify the different types of operators in C based on their functionality. Write a C
program using logical operators to check if a number lies between 1 and 100.
Classification of Operators in C (Based on Functionality)
Category Description Examples
1. Arithmetic Operators Perform basic arithmetic operations +, -, *, /, %
Compare two values and return true ==, !=, >, <, >=,
2. Relational Operators
or false <=
Combine multiple conditions
3. Logical Operators && (AND), `
(true/false values)
4. Assignment =, +=, -=, *=, /=,
Assign values to variables
Operators %=
5. Increment/Decrement Increase or decrease a value by one ++, --
6. Bitwise Operators Perform bit-level operations &, `
Ternary operator for decision
7. Conditional Operator ?:
making
Algorithm and Flowchart to Check Even or Odd
1. Start
2. Read the number
3. If number % 2 == 0 then
4. Print "Even"
5. Else
Print "Odd"
6. Stop
Differentiate clearly between the following operators in C:
Pre-increment
Post-increment
Pre-decrement
Post-decrement
Use code examples to show their behavior and output.
Pre/Post Increment & Decrement with Examples
#include <stdio.h>
int main() {
int a = 5, b;
// Pre-increment
b = ++a; // a becomes 6, then b = 6
printf("Pre-Increment: a = %d, b = %d\n", a, b);
// Post-increment
a = 5;
b = a++; // b = 5, then a becomes 6
printf("Post-Increment: a = %d, b = %d\n", a, b);
// Pre-decrement
a = 5;
b = --a; // a becomes 4, then b = 4
printf("Pre-Decrement: a = %d, b = %d\n", a, b);
// Post-decrement
a = 5;
b = a--; // b = 5, then a becomes 4
printf("Post-Decrement: a = %d, b = %d\n", a, b);
return 0;
}
Discuss the various decision-making control structures in C. Write the syntax of the
if-else statement and provide a program to check if a number is positive or negative
Decision-Making Control Structures in C
Decision-making control structures allow a program to execute different blocks of
code based on certain conditions.
Types of Decision-Making Structures
Structure Description
if Executes a block of code if the condition is true.
Executes one block if the condition is true,
if...else
another if false.
else if
Tests multiple conditions one after another.
ladder
nested if An if or if-else inside another if or else block.
Selects one block of code from many options
switch
based on variable value.
Write the syntax of the switch-case control structure in C. Then, write a C program to
simulate a simple arithmetic calculator using the switch-case construct based on user input.
Switch-Case Control Structure in C
switch(expression) {
case constant1: // Code to execute if expression == constant1; break;
case constant2: // Code to execute if expression == constant2; break;
...
default:
// Code to execute if no case matches
}
switch compares the expression with each case.
break exits the switch block after a case executes.
default is optional and runs when no case matches
#include <stdio.h>
int main() {
float num1, num2, result;
char op;
// Input from user
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op); // Notice space before %c to consume any leftover newline
printf("Enter second number: ");
scanf("%f", &num2);
// switch-case for operation
switch(op) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
result = num1 / num2;
printf("Result: %.2f\n", result);
break;
default:
printf("Invalid operator!\n");
}
return 0;
}
1. Implement a grading system in C using the else-if ladder, based on the following criteria:
90: A+
81–90: A
71–80: B+
61–70: B
51–60: C+
41–50: C
≤40: F
#include <stdio.h>
int main() {
int marks;
// Input marks
printf("Enter marks (0 to 100): ");
scanf("%d", &marks);
// else-if ladder to assign grades
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 if (marks >= 0) {
printf("Grade: F\n");
} else {
printf("Invalid Marks\n");
}
return 0;
}
Write a program that prints all numbers between 1 and 50 that are divisible by 7
using a for loop.
Program to Print Numbers Divisible by 7 from 1 to 50
#include <stdio.h>
int main() {
printf("Numbers divisible by 7 from 1 to 50:\n");
for (int i = 1; i <= 50; i++) {
if (i % 7 == 0)
printf("%d ", i);
}
return 0;
}
Using the switch-case construct, develop a C program that determines whether an input
character is a vowel or consonant.
Vowel or Consonant using Switch-Case
#include <stdio.h>
int main() {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
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);
}
return 0;
}
Voting Eligibility using Nested if
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 0) {
if (age >= 18)
printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");
} else {
printf("Invalid age entered.\n");
}
return 0;
}
Electricity Bill using else-if Ladder
#include <stdio.h>
int main() {
int units;
float bill;
printf("Enter total units consumed: ");
scanf("%d", &units);
if (units <= 50)
bill = units * 3.0;
else if (units <= 150)
bill = units * 5.0;
else if (units <= 250)
bill = units * 7.0;
else
bill = units * 10.0;
printf("Total Electricity Bill: ₹%.2f\n", bill);
return 0;
}
Monthly Sales Using 1D Array
#include <stdio.h>
int main() {
float sales[12], total = 0;
int i;
// Read sales
for (i = 0; i < 12; i++) {
printf("Enter sales for month %d: ", i + 1);
scanf("%f", &sales[i]);
}
// Display and calculate total
printf("\nMonthly Sales:\n");
for (i = 0; i < 12; i++) {
printf("Month %d: ₹%.2f\n", i + 1, sales[i]);
total += sales[i];
}
printf("Total Sales for the Year: ₹%.2f\n", total);
return 0;
}
Salaries Using Array + Average
#include <stdio.h>
int main() {
float salary[5], sum = 0, avg;
int i;
// Input salaries
for (i = 0; i < 5; i++) {
printf("Enter salary of employee %d: ", i + 1);
scanf("%f", &salary[i]);
sum += salary[i];
}
// Display entered salaries
printf("\nSalaries entered:\n");
for (i = 0; i < 5; i++) {
printf("Employee %d: ₹%.2f\n", i + 1, salary[i]);
}
// Calculate average
avg = sum / 5;
printf("Average Salary: ₹%.2f\n", avg);
return 0;
}