Here’s a collection of beginner-friendly C programs based on the provided suggestions.
Each code is
well-documented for easy understanding.
Basic
1. Area and Perimeter of a Circle
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, perimeter;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
perimeter = 2 * PI * radius;
printf("Area of the circle: %.2f\n", area);
printf("Perimeter of the circle: %.2f\n", perimeter);
return 0;
}
2. Solve Quadratic Equation ax2 + bx + c = 0
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different: %.2f and %.2f\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and same: %.2f\n", root1);
} else {
printf("Roots are imaginary.\n");
}
return 0;
}
Checking Any Number
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/9
1. Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is Even\n", num);
} else {
printf("%d is Odd\n", num);
}
return 0;
}
2. Palindrome Check
#include <stdio.h>
int main() {
int num, reversed = 0, temp, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
temp = num;
while (temp != 0) {
remainder = temp % 10;
reversed = reversed * 10 + remainder;
temp /= 10;
}
if (num == reversed) {
printf("%d is a Palindrome\n", num);
} else {
printf("%d is not a Palindrome\n", num);
}
return 0;
}
Switch Case
1. Grade System
#include <stdio.h>
int main() {
int marks;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/9
printf("Enter your marks: ");
scanf("%d", &marks);
switch (marks / 10) {
case 10:
case 9:
printf("Grade: A\n");
break;
case 8:
printf("Grade: B\n");
break;
case 7:
printf("Grade: C\n");
break;
case 6:
printf("Grade: D\n");
break;
default:
printf("Grade: F\n");
}
return 0;
}
2. Simple Calculator
#include <stdio.h>
int main() {
int num1, num2, choice;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Choose an operation:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Result: %d\n", num1 + num2);
break;
case 2:
printf("Result: %d\n", num1 - num2);
break;
case 3:
printf("Result: %d\n", num1 * num2);
break;
case 4:
if (num2 != 0)
printf("Result: %.2f\n", (float)num1 / num2);
else
printf("Division by zero is not allowed.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Pattern
1. Diamond Pattern
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/9
c
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of rows for the diamond: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = i; j < n; j++)
printf(" ");
for (j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--)
printf(" ");
for (j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
return 0;
}
Series
1. Sum of 1 + 2 + 3 + … + n
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum = %d\n", sum);
return 0;
}
Array
1. Summation and Multiplication of Two Arrays
#include <stdio.h>
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/9
int main() {
int n, i;
printf("Enter the size of arrays: ");
scanf("%d", &n);
int a[n], b[n], sum[n], product[n];
printf("Enter elements of the first array: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter elements of the second array: ");
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
for (i = 0; i < n; i++) {
sum[i] = a[i] + b[i];
product[i] = a[i] * b[i];
}
printf("Sum of arrays: ");
for (i = 0; i < n; i++) {
printf("%d ", sum[i]);
}
printf("\n");
printf("Product of arrays: ");
for (i = 0; i < n; i++) {
printf("%d ", product[i]);
}
printf("\n");
return 0;
}
The remaining sections (Recursion, Strings, Pointers, and Structures) will follow if you'd like. Would you
like me to proceed?
Here’s the continuation of your requested programs:
Recursion
1. Factorial
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/9
return 0;
}
2. Fibonacci Sequence
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
String
1. Reverse String
#include <stdio.h>
#include <string.h>
int main() {
char str[100], reversed[100];
int len, i;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++) {
reversed[i] = str[len - i - 1];
}
reversed[len] = '\0';
printf("Reversed string: %s\n", reversed);
return 0;
}
2. String Length
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/9
c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int length;
printf("Enter a string: ");
scanf("%s", str);
length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}
3. Count Vowels and Consonants
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels = 0, consonants = 0;
printf("Enter a string: ");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
else if (isalpha(ch))
consonants++;
}
printf("Vowels: %d, Consonants: %d\n", vowels, consonants);
return 0;
}
Pointer
1. Swap Two Pointers
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/9
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
2. Add, Subtract, Multiply, Divide Using Pointers
#include <stdio.h>
void arithmeticOperations(int a, int b, int *sum, int *diff, int *prod, float *quotient) {
*sum = a + b;
*diff = a - b;
*prod = a * b;
if (b != 0)
*quotient = (float)a / b;
else
*quotient = 0;
}
int main() {
int a, b, sum, diff, prod;
float quotient;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
arithmeticOperations(a, b, &sum, &diff, &prod, "ient);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
printf("Product: %d\n", prod);
if (b != 0)
printf("Quotient: %.2f\n", quotient);
else
printf("Division by zero is not possible.\n");
return 0;
}
3. Find Maximum and Minimum Numbers
#include <stdio.h>
void findMaxMin(int arr[], int n, int *max, int *min) {
*max = arr[0];
*min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > *max)
*max = arr[i];
if (arr[i] < *min)
*min = arr[i];
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/9
int main() {
int n, max, min;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findMaxMin(arr, n, &max, &min);
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
return 0;
}
Structure
1. Lab Report
#include <stdio.h>
struct LabReport {
char name[50];
int rollNumber;
char experiment[100];
int marks;
};
int main() {
struct LabReport report;
printf("Enter your name: ");
scanf("%s", report.name);
printf("Enter your roll number: ");
scanf("%d", &report.rollNumber);
printf("Enter the experiment name: ");
scanf(" %[^\n]s", report.experiment);
printf("Enter your marks: ");
scanf("%d", &report.marks);
printf("\n--- Lab Report ---\n");
printf("Name: %s\n", report.name);
printf("Roll Number: %d\n", report.rollNumber);
printf("Experiment: %s\n", report.experiment);
printf("Marks: %d\n", report.marks);
return 0;
}
These programs cover all the suggested topics. If you'd like further explanations or enhancements for
any code, let me know! 😊
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/9