C Basics

AdhamElshabasy avatarAdhamElshabasy
Public01/13/2025 10:16 PM6 snippets
C-Program-to-Calculate-Factorial.cpp
Event-and-Count-Controlled-Loops-in-C.cpp
Counting-A-and-B-Students-Based-on-Grades.cpp
Simple-Looping-Example-in-C-(While).cpp
Basic-Switch-Statement-Example-in-C.cpp
Aircraft-Classifier-(IF-Condition).cpp
This C program prompts the user to enter a number and calculates its factorial using a while loop. The factorial is computed by multiplying the number by all positive integers less than it until it reaches 1.
C-Program-to-Calculate-Factorial.cpp
#include <stdio.h>

int main() {
	
	int n, factorial;
	
	factorial = 1;
	
	printf("Enter n: ");
	scanf("%d", &n);
This C program demonstrates how to calculate the growth of two investments over time.
Event-and-Count-Controlled-Loops-in-C.cpp
#include <stdio.h>

int main() {
	
	float invest = 1000;
	int num_years = 0;
	
	while(invest < 2000) {
		invest = invest + (invest * 0.05);
		printf("Investment = %0.2f\n", invest);
This C program prompts the user to enter grades for 10 students and counts how many received A (above 85) and B (above 75) grades. It ensures that the grades entered are between 0 and 100, requesting re-entry for invalid inputs. At the end, it displays the number of A and B students.
Counting-A-and-B-Students-Based-on-Grades.cpp
#include <stdio.h>

int main() {
	
	int i;
	int grade;
	int a_students = 0;
	int b_students = 0;
	
	for (i = 0; i < 10; i++) {
This code demonstrates the use of both a while loop and a do-while loop in C programming. The program repeatedly prompts the user to input a value for X until they enter 0, showcasing two different looping constructs.
Simple-Looping-Example-in-C-(While).cpp
#include <stdio.h>

int main() {
	
	int x = 1;
	
	while(x != 0) {
		printf("Enter X (while): ");
		scanf("%d", &x);
	}
This C program prompts the user to enter a number and uses a switch statement to check if it matches 10, 20, or 30. Depending on the input, it prints the corresponding number or a default message if the number is not one of those options.
Basic-Switch-Statement-Example-in-C.cpp
#include <stdio.h>

int main() {
	
	int x;
	
	printf("Please enter X: ");
	scanf("%d", &x);
	
	switch(x) {
This C program classifies an aircraft based on its speed and length. It prompts the user to input the aircraft's speed and length, and then determines if it's a military or civilian aircraft, or if it's an unknown type, based on specified criteria.
Aircraft-Classifier-(IF-Condition).cpp
#include <stdio.h>

int main() {
	
	int speed;
	int length;
	
	printf("Please enter speed: ");
	scanf("%d", &speed);
	printf("Please enter length: ");