Progr_Solution
Progr_Solution
1. Declare and initialize variables of different data types and print their
values.
#include <stdio.h>
int main() {
// Declare and initialize variables of different data types
int integerVar = 42; // Integer
float floatVar = 3.14f; // Float
double doubleVar = 2.718281828; // Double
char charVar = 'A'; // Character
char stringVar[] = "Hello, World"; // String (character array)
// Print their values
printf("Integer: %d\n", integerVar);
printf("Float: %.2f\n", floatVar); // Print float with 2 decimal places
printf("Double: %.6f\n", doubleVar); // Print double with 6 decimal
places
printf("Character: %c\n", charVar);
printf("String: %s\n", stringVar);
return 0;
}
Data Types:
int: For integer values.
float: For single-precision floating-point values.
double: For double-precision floating-point values.
char: For single characters.
char[]: For strings (array of characters).
Printing:
%d is used for integers.
%.2f is used for floats with 2 decimal places.
%.6f is used for doubles with 6 decimal places.
%c is used for characters.
%s is used for strings.
return 0;
}
return 0;
}
4.Demonstrate Implicit Type Conversion by Assigning a Float Value to an
Integer Variable.
#include <stdio.h>
int main() {
int a = 5; // Integer
float b = 3.2; // Float
void main() {
float float_value = 7.89; // Float value
int int_value = (int) float_value; // Explicitly casting float to int
printf("Float value: %.2f\n", float_value);
printf("Converted to int: %d\n", int_value); // Output will be 7 (fractional
part is discarded)
getch();
}
6.Using Arithmetic Operators (+, -, , /, %) to Perform Basic Calculations
#include <stdio.h>
void main() {
int x = 10;
int y = 3;
// Addition
int addition = x + y;
printf("Addition: %d + %d = %d\n", x, y, addition);
// Subtraction
int subtraction = x - y;
printf("Subtraction: %d - %d = %d\n", x, y, subtraction);
// Multiplication
int multiplication = x * y;
printf("Multiplication: %d * %d = %d\n", x, y, multiplication);
// Division (returns integer result)
int division = x / y; // Integer division
printf("Division: %d / %d = %d\n", x, y, division);
// Modulus (remainder of division)
int modulus = x % y;
printf("Modulus: %d %% %d = %d\n", x, y, modulus);
getch();
}
7.Use Relational Operators (==, !=, >, <, >=, <=) to Compare Two Numbers
and Print the Results.
#include <stdio.h>
void main() {
int num1 = 10;
int num2 = 5;
// Equality check
if (num1 == num2) {
printf("%d is equal to %d\n", num1, num2);
} else {
printf("%d is not equal to %d\n", num1, num2);
}
// Greater than check
if (num1 > num2) {
printf("%d is greater than %d\n", num1, num2);
}
void main() {
int a = 10;
int b = 5;
int c = 20;
// Logical OR (||)
if (a > b || b > c) {
printf("a is greater than b OR b is greater than c\n");
} else {
printf("Neither a is greater than b NOR b is greater than c\n");
}
// Logical NOT (!)
if (!(a == b)) {
printf("a is not equal to b\n");
}
getch();
}
9.Demonstrate the Use of Increment and Decrement Operators (++, --) on
an Integer Variable.
#include <stdio.h>
void main() {
int num = 5;
// Using the increment operator (++)
printf("Initial value of num: %d\n", num);
// Prefix increment
printf("Prefix increment: ++num = %d\n", ++num); // num is incremented
before it's used
// Postfix increment
printf("Postfix increment: num++ = %d\n", num++); // num is used first,
then incremented
printf("Value of num after postfix increment: %d\n", num);
// Using the decrement operator (--)
// Prefix decrement
printf("Prefix decrement: --num = %d\n", --num); // num is decremented
before it's used
// Postfix decrement
printf("Postfix decrement: num-- = %d\n", num--); // num is used first, then
decremented
printf("Value of num after postfix decrement: %d\n", num);
getch();
}
10.Write a Program to Check Whether a Given Number is Even or Odd
Using an if-else Statement.
#include <stdio.h>
void main() {
int num;
// Prompt user for input
printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is even or odd
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
getch();
}
11. Calculate the Final Price of a Meal Based on the Order Value
Problem: Write a C program that calculates the final price of a meal
based on the total amount entered by the user. Apply a discount based on
the following criteria:
10% discount for orders over ₹500.
15% discount for orders over ₹1000.
20% discount for orders over ₹2000.
Additionally, include a service charge of 5% of the final discounted
price.
#include <stdio.h>
int main() {
float totalAmount, finalPrice, discountAmount, serviceCharge;
return 0;
}
12. Calculate the Final Loan Amount Based on Interest Rates
Problem: Write a C program that calculates the final loan amount
including interest based on the principal amount and the loan term. The
interest rates are:
5% for loans of ₹5000 or less.
7% for loans between ₹5001 and ₹10000.
10% for loans above ₹10000.
Validation: Ensure that the principal amount is positive.
#include <stdio.h>
int main() {
float principal, finalAmount, interestAmount;
int loanTerm;
// Ask the user for the principal amount and loan term
printf("Enter the principal amount: ₹");
scanf("%f", &principal);
printf("Enter the loan term in years: ");
scanf("%d", &loanTerm);
return 0;
}
13. Calculate the Final Salary Based on Experience
Problem: Write a C program that calculates the final salary of an
employee based on their basic salary and years of experience:
5% bonus for employees with less than 5 years of experience.
10% bonus for employees with 5-10 years of experience.
15% bonus for employees with more than 10 years of experience.
Validation: Ensure that the basic salary is positive and years of
experience is a non-negative integer.
#include <stdio.h>
int main() {
float basicSalary, finalSalary, bonus;
int experience;
return 0;
}
14. Calculate the Final Rent Based on Lease Duration
Problem: Write a C program that calculates the total rent based on the
number of months for a lease. The rent discount applies as follows:
5% discount for leases of 6-12 months.
10% discount for leases of 13-24 months.
15% discount for leases of 25 months or more.
Validation: Ensure that the number of months entered is positive and the
rent is greater than zero.
#include <stdio.h>
int main() {
float rent, totalRent, discount;
int months;
// Ask the user for the rent amount and lease duration
printf("Enter the monthly rent: ₹");
scanf("%f", &rent);
printf("Enter the lease duration in months: ");
scanf("%d", &months);
return 0;
}