0% found this document useful (0 votes)
1 views8 pages

Tasks for Practice

The document contains seven C programming tasks demonstrating various control structures and functionalities. Tasks include basic if-else statements, grade classification, nested if for discount calculation, leap year checking, a menu-driven calculator, a BMI calculator, and a password strength checker. Each task is presented with code snippets and prompts for user input.

Uploaded by

nareshnns2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

Tasks for Practice

The document contains seven C programming tasks demonstrating various control structures and functionalities. Tasks include basic if-else statements, grade classification, nested if for discount calculation, leap year checking, a menu-driven calculator, a BMI calculator, and a password strength checker. Each task is presented with code snippets and prompts for user input.

Uploaded by

nareshnns2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Task 1: Basic If-Else Implementation

Copy code

#include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if (num > 0) {

printf("The number is positive.\n");

} else if (num < 0) {

printf("The number is negative.\n");

} else {

printf("The number is zero.\n");

return 0;

Task 2: Grade Classification

Copy code

#include <stdio.h>

int main() {

int score;
printf("Enter your score (0-100): ");

scanf("%d", &score);

if (score >= 90) {

printf("Grade: A\n");

} else if (score >= 80) {

printf("Grade: B\n");

} else if (score >= 70) {

printf("Grade: C\n");

} else if (score >= 60) {

printf("Grade: D\n");

} else {

printf("Grade: F\n");

return 0;

Task 3: Nested If for Discount Calculation

Copy code

#include <stdio.h>

#include <string.h>

int main() {

char membership[10];

float purchaseAmount, discount = 0;

printf("Enter your membership level (Gold, Silver, Regular): ");

scanf("%s", membership);
printf("Enter the purchase amount: ");

scanf("%f", &purchaseAmount);

if (strcmp(membership, "Gold") == 0) {

if (purchaseAmount > 1000) {

discount = 0.20;

} else if (strcmp(membership, "Silver") == 0) {

if (purchaseAmount > 500) {

discount = 0.10;

} else {

discount = 0.05;

float finalPrice = purchaseAmount - (purchaseAmount * discount);

printf("Final price after discount: %.2f\n", finalPrice);

return 0;

Task 4: Leap Year Checker

Copy code

#include <stdio.h>

int main() {

int year;
printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

printf("%d is a leap year.\n", year);

} else {

printf("%d is not a leap year.\n", year);

} else {

printf("%d is a leap year.\n", year);

} else {

printf("%d is not a leap year.\n", year);

return 0;

Task 5: Menu-Driven Calculator

Copy code

#include <stdio.h>

int main() {

int choice;

float num1, num2, result;

printf("Simple Calculator\n");
printf("1. Addition\n");

printf("2. Subtraction\n");

printf("3. Multiplication\n");

printf("4. Division\n");

printf("Enter your choice (1-4): ");

scanf("%d", &choice);

printf("Enter two numbers: ");

scanf("%f %f", &num1, &num2);

if (choice == 1) {

result = num1 + num2;

printf("Result: %.2f\n", result);

} else if (choice == 2) {

result = num1 - num2;

printf("Result: %.2f\n", result);

} else if (choice == 3) {

result = num1 * num2;

printf("Result: %.2f\n", result);

} else if (choice == 4) {

if (num2 != 0) {

result = num1 / num2;

printf("Result: %.2f\n", result);

} else {

printf("Error: Division by zero is not allowed.\n");

} else {

printf("Invalid choice!\n");

}
return 0;

Task 6: BMI Calculator

Copy code

#include <stdio.h>

int main() {

float weight, height, bmi;

printf("Enter your weight in kg: ");

scanf("%f", &weight);

printf("Enter your height in meters: ");

scanf("%f", &height);

bmi = weight / (height * height);

printf("Your BMI is: %.2f\n", bmi);

if (bmi < 18.5) {

printf("Category: Underweight\n");

} else if (bmi >= 18.5 && bmi <= 24.9) {

printf("Category: Normal weight\n");

} else if (bmi >= 25 && bmi <= 29.9) {

printf("Category: Overweight\n");

} else {

printf("Category: Obese\n");
}

return 0;

Task 7: Password Strength Checker

Copy code

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int main() {

char password[100];

int hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0, length = 0;

printf("Enter a password: ");

scanf("%s", password);

length = strlen(password);

if (length >= 8) {

for (int i = 0; i < length; i++) {

if (isupper(password[i])) {

hasUpper = 1;

if (islower(password[i])) {

hasLower = 1;

if (isdigit(password[i])) {
hasDigit = 1;

if (ispunct(password[i])) {

hasSpecial = 1;

if (hasUpper && hasLower && hasDigit && hasSpecial) {

printf("Password Strength: Strong\n");

} else if ((hasUpper && hasLower && hasDigit) ||

(hasUpper && hasLower && hasSpecial) ||

(hasLower && hasDigit && hasSpecial) ||

(hasUpper && hasDigit && hasSpecial)) {

printf("Password Strength: Medium\n");

} else {

printf("Password Strength: Weak\n");

} else {

printf("Password is too short, it should be at least 8 characters.\n");

return 0;

You might also like