Mastering the Basics: 10 Essential C Programs for Beginners 🚀
Hello aspiring coders! Today, we embark on an exciting journey into the world of C programming, exploring 10 fundamental programs that lay the foundation for your coding adventure. 🌐💻
1. Hello World 👋
c#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Our first program introduces you to the tradition of coding with a simple greeting.
2. Sum of Two Numbers ➕
c#include <stdio.h>
int main() {
int num1 = 5, num2 = 7, sum;
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
Learn how to declare variables and perform basic arithmetic operations.
3. Even or Odd? 🧮
c#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Delve into conditional statements with a program that checks if a number is even or odd.
4. Factorial Fun! 🔄
c#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
printf("Factorial: %d\n", factorial);
return 0;
}
Get hands-on with loops and calculate the factorial of a number.
5. Fibonacci Magic 🌈
c#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; ++i) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
Unravel the mysteries of loops and recursion with the Fibonacci series.
6. Prime Checker 🔍
c#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num)) {
printf("%d is prime.\n", num);
} else {
printf("%d is not prime.\n", num);
}
return 0;
}
Discover the magic of functions while checking if a number is prime.
7. Palindrome Checker 🔄
c#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool isPalindrome(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; ++i) {
if (str[i] != str[len - i - 1]) {
return false;
}
}
return true;
}
int main() {
char word[100];
printf("Enter a word: ");
scanf("%s", word);
if (isPalindrome(word)) {
printf("%s is a palindrome.\n", word);
} else {
printf("%s is not a palindrome.\n", word);
}
return 0;
}
String manipulation comes alive with a palindrome-checking program.
8. Square and Cube Generator 🔄
c#include <stdio.h>
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Number\tSquare\tCube\n");
for (int i = 1; i <= n; ++i) {
printf("%d\t%d\t%d\n", i, i * i, i * i * i);
}
return 0;
}
Enhance your loop skills with a program generating squares and cubes.
9. Simple Calculator ➗
c#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
printf("Result: %.2lf\n", num1 + num2);
break;
case '-':
printf("Result: %.2lf\n", num1 - num2);
break;
case '*':
printf("Result: %.2lf\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("Result: %.2lf\n", num1 / num2);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}
return 0;
}
Bring math to life with a simple calculator program using switch statements.
10. Pattern Printer 🌟
c#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Wrap up your C journey with a visual treat – a program that prints beautiful patterns.
Congratulations, young coder! You've taken your first steps into the captivating world of C programming. Each program is a stepping stone, paving the way for your coding odyssey. Keep exploring, keep coding! 🚀💻 #CProgramming #CodingJourney #ProgrammingMagic
Follow us