[go: up one dir, main page]

0% found this document useful (0 votes)
11 views13 pages

Progr_Solution

The document outlines various C programming exercises for students in the Integrated M Sc(Information Technology) and Bachelor of Computer Applications programs at SKIPS University. It covers fundamental concepts such as variable declaration, data types, arithmetic operations, constants, type conversion, and conditional statements, along with practical coding examples. Additionally, it includes problems related to calculating discounts, loan amounts, salaries, and rent based on different criteria.

Uploaded by

jeseowens111
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
11 views13 pages

Progr_Solution

The document outlines various C programming exercises for students in the Integrated M Sc(Information Technology) and Bachelor of Computer Applications programs at SKIPS University. It covers fundamental concepts such as variable declaration, data types, arithmetic operations, constants, type conversion, and conditional statements, along with practical coding examples. Additionally, it includes problems related to calculating discounts, loan amounts, salaries, and rent based on different criteria.

Uploaded by

jeseowens111
Copyright
© © All Rights Reserved
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/ 13

SKIPS University – School of Computer Science

Integrated M Sc(Information Technology) - Batch 2024- 29


Bachelor of Computer Applications (Hons.) - Batch 2024- 28
Practical Solution For reference

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.

2. Define and use integer, float, and character constants in a program


#include <stdio.h>
int main() {
// Define constants
const int INT_CONSTANT = 100; // Integer constant
const float FLOAT_CONSTANT = 9.81f; // Float constant (use 'f' to
indicate float)
const char CHAR_CONSTANT = 'Z'; // Character constant

// Use the constants in calculations and printing


int result = INT_CONSTANT * 2;
float gravity = FLOAT_CONSTANT;
char nextChar = CHAR_CONSTANT + 1; // Get the next character

// Print the values


printf("Integer Constant: %d\n", INT_CONSTANT);
printf("Result of Integer Constant multiplied by 2: %d\n", result);
printf("Float Constant: %.2f\n", FLOAT_CONSTANT);
printf("Character Constant: %c\n", CHAR_CONSTANT);
printf("Next Character: %c\n", nextChar);

return 0;
}

3.Use arithmetic operators (+, -, *, /, %) to perform basic calculations


between two numbers.
#include <stdio.h>
int main() {
// Declare two integer variables
int num1, num2;
// Ask the user for input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Perform calculations using arithmetic operators
int sum = num1 + num2; // Addition
int difference = num1 - num2; // Subtraction
int product = num1 * num2; // Multiplication
float quotient = (float)num1 / num2; // Division (cast to float for accurate
result)
int remainder = num1 % num2; // Modulus (remainder of division)

// Print the results


printf("Sum: %d + %d = %d\n", num1, num2, sum);
printf("Difference: %d - %d = %d\n", num1, num2, difference);
printf("Product: %d * %d = %d\n", num1, num2, product);
printf("Quotient: %d / %d = %.2f\n", num1, num2, quotient); // Print float
with 2 decimal places
printf("Remainder: %d %% %d = %d\n", num1, num2, remainder);

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

// Implicit type conversion occurs when an integer is added to a float


float result = a + b; // 'a' is implicitly converted to float
printf("Result of addition: %.2f\n", result); // Output will be 8.20
return 0;
}

5.Implement Explicit Type Casting to Convert a Float Value to an Integer


#include <stdio.h>

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);
}

// Less than check


if (num1 < num2) {
printf("%d is less than %d\n", num1, num2);
}
// Greater than or equal to check
if (num1 >= num2) {
printf("%d is greater than or equal to %d\n", num1, num2);
}
// Less than or equal to check
if (num1 <= num2) {
printf("%d is less than or equal to %d\n", num1, num2);
}
getch();
}
8.Implement a Simple Program Using Logical Operators (&&, ||, !) to
Combine Multiple Conditions.
#include <stdio.h>

void main() {
int a = 10;
int b = 5;
int c = 20;

// Logical AND (&&)


if (a > b && a < c) {
printf("a is greater than b AND less than c\n");
} else {
printf("Either a is not greater than b OR a is not less than c\n");
}

// 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;

// Ask the user for the total order amount


printf("Enter the total order amount: ₹");
scanf("%f", &totalAmount);

// Validate the input to ensure the amount is positive


if (totalAmount <= 0) {
printf("Invalid input! The order amount must be positive.\n");
return 1;
}

// Calculate discount based on the total amount


if (totalAmount > 2000) {
discountAmount = totalAmount * 0.20; // 20% discount
} else if (totalAmount > 1000) {
discountAmount = totalAmount * 0.15; // 15% discount
} else if (totalAmount > 500) {
discountAmount = totalAmount * 0.10; // 10% discount
} else {
discountAmount = 0; // No discount
}

// Calculate the final price after discount


finalPrice = totalAmount - discountAmount;

// Apply service charge of 5% on the discounted price


serviceCharge = finalPrice * 0.05;

// Calculate the final price after adding the service charge


finalPrice += serviceCharge;

// Display the final price


printf("The final price after discount and service charge is: ₹%.2f\n",
finalPrice);

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);

// Validate the input to ensure the principal is positive


if (principal <= 0 || loanTerm <= 0) {
printf("Invalid input! Principal amount and loan term must be
positive.\n");
return 1;
}

// Calculate interest rate based on the principal amount


if (principal > 10000) {
interestAmount = principal * 0.10 * loanTerm; // 10% interest
} else if (principal > 5000) {
interestAmount = principal * 0.07 * loanTerm; // 7% interest
} else {
interestAmount = principal * 0.05 * loanTerm; // 5% interest
}

// Calculate the final loan amount


finalAmount = principal + interestAmount;

// Display the final amount to be repaid


printf("The final loan amount to be repaid is: ₹%.2f\n", finalAmount);

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;

// Ask the user for basic salary and experience


printf("Enter the basic salary: ₹");
scanf("%f", &basicSalary);
printf("Enter years of experience: ");
scanf("%d", &experience);

// Validate the input


if (basicSalary <= 0 || experience < 0) {
printf("Invalid input! Basic salary must be positive and experience
cannot be negative.\n");
return 1;
}

// Calculate bonus based on experience


if (experience > 10) {
bonus = basicSalary * 0.15; // 15% bonus
} else if (experience >= 5) {
bonus = basicSalary * 0.10; // 10% bonus
} else {
bonus = basicSalary * 0.05; // 5% bonus
}

// Calculate the final salary


finalSalary = basicSalary + bonus;

// Display the final salary


printf("The final salary after bonus is: ₹%.2f\n", finalSalary);

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);

// Validate the input


if (rent <= 0 || months <= 0) {
printf("Invalid input! Rent and lease duration must be positive.\n");
return 1;
}

// Calculate the discount based on lease duration


if (months >= 25) {
discount = 0.15; // 15% discount
} else if (months >= 13) {
discount = 0.10; // 10% discount
} else if (months >= 6) {
discount = 0.05; // 5% discount
} else {
discount = 0; // No discount
}

// Calculate the total rent after discount


totalRent = rent * months * (1 - discount);

// Display the total rent


printf("The total rent after discount for %d months is: ₹%.2f\n",
months, totalRent);

return 0;
}

You might also like