C Language Projects
Create a Calculator
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf(“Enter an operator (+, -, *, /): “);
scanf(“%c”, &operator);
printf(“Enter two operands: “);
scanf(“%lf %lf”, &num1, &num2);
switch(operator) {
case ‘+’:
result = num1 + num2;
break;
case ‘-‘:
result = num1 – num2;
break;
case ‘*’:
result = num1 * num2;
break;
case ‘/’:
result = num1 / num2;
break;
default:
printf(“Error! Invalid operator.”);
return 1;
}
printf(“%.2lf %c %.2lf = %.2lf”, num1, operator, num2, result);
return 0;
}
Create a Table of 2
#include <stdio.h>
int main() {
int i, result;
printf(“Table of 2:\n”);
for(i = 1; i <= 10; i++) {
result = 2 * i;
printf(“2 x %d = %d\n”, i, result);
}
return 0;
}
Calculate the Age of a person
#include <stdio.h>
#include <time.h>
int main() {
int birth_year, current_year, age;
printf(“Enter your birth year: “);
scanf(“%d”, &birth_year);
time_t t = time(NULL);
struct tm current_time = *localtime(&t);
current_year = current_time.tm_year + 1900;
age = current_year – birth_year;
printf(“Your age is %d years.\n”, age);
return 0;
}
Calculate the Percentage
#include <stdio.h>
int main() {
float total, amount, percentage;
printf(“Enter the total number: “);
scanf(“%f”, &total);
printf(“Enter the amount: “);
scanf(“%f”, &amount);
percentage = (amount / total) * 100;
printf(“The percentage is: %.2f%%\n”, percentage);
return 0;
}
Table of 2
#include <stdio.h>
int main() {
int i;
printf(“Table of 2:\n”);
for (i = 1; i <= 10; i++) {
printf(“2 x %d = %d\n”, i, 2*i);
}
return 0;
}
Number Guessing game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
srand(time(0)); // seed the random number generator
number = rand() % 100 + 1; // generate a random number between 1 and 100
printf(“Guess the number between 1 and 100.\n”);
do {
printf(“Enter your guess: “);
scanf(“%d”, &guess);
attempts++;
if (guess > number) {
printf(“Too high! Try again.\n”);
} else if (guess < number) {
printf(“Too low! Try again.\n”);
} else {
printf(“Congratulations, you guessed the number in %d attempts!\n”, attempts);
}
} while (guess != number);
return 0;
}