0% found this document useful (0 votes)
14 views7 pages

C Programming Basics: 18 Examples

The document contains a series of C programming examples demonstrating various concepts such as area calculations, interest computations, quadratic equations, and basic conditional statements. Each example includes code snippets that prompt user input and display results for mathematical operations, logical checks, and classifications. Topics covered range from geometry and finance to character classification and temperature conversion.

Uploaded by

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

C Programming Basics: 18 Examples

The document contains a series of C programming examples demonstrating various concepts such as area calculations, interest computations, quadratic equations, and basic conditional statements. Each example includes code snippets that prompt user input and display results for mathematical operations, logical checks, and classifications. Topics covered range from geometry and finance to character classification and temperature conversion.

Uploaded by

ssvaiad3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Variables, Identifiers, datatypes & operators

1. Area of a Circle
#include <stdio.h>
#define PI 3.1416

int main() {
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);

area = PI * radius * radius;


printf("Area of Circle = %.2f\n", area);
return 0;
}

2. Simple Interest Calculation


#include <stdio.h>

int main() {
float principal, rate, time, interest;
printf("Enter principal, rate (%%), and time (years): ");
scanf("%f %f %f", &principal, &rate, &time);

interest = (principal * rate * time) / 100;


printf("Simple Interest = %.2f\n", interest);
return 0;
}

3. Quadratic Equation Roots


#include <stdio.h>
#include <math.h>

int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, 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("Real and distinct roots: %.2f and %.2f\n", root1, root2);
}
else if (discriminant == 0) {
root1 = -b / (2*a);
printf("Real and equal roots: %.2f and %.2f\n", root1, root1);
}
else {
float realPart = -b / (2*a);
float imagPart = sqrt(-discriminant) / (2*a);
printf("Complex roots: %.2f + %.2fi and %.2f - %.2fi\n",
realPart, imagPart, realPart, imagPart);
}

return 0;
}

4. Speed, Distance, and Time Relationship


#include <stdio.h>

int main() {
float speed, time, distance;
printf("Enter speed (km/h) and time (h): ");
scanf("%f %f", &speed, &time);

distance = speed * time;


printf("Distance = %.2f km\n", distance);
return 0;
}

5. Compound Interest Calculation


#include <stdio.h>
#include <math.h>

int main() {
float principal, rate, time, amount, ci;
printf("Enter principal, rate (%%), and time (years): ");
scanf("%f %f %f", &principal, &rate, &time);

amount = principal * pow((1 + rate / 100), time);


ci = amount - principal;
printf("Compound Interest = %.2f\n", ci);
return 0;
}

6. Perimeter of a Rectangle
#include <stdio.h>

int main() {
float length, width, perimeter;
printf("Enter length and width: ");
scanf("%f %f", &length, &width);

perimeter = 2 * (length + width);


printf("Perimeter = %.2f\n", perimeter);
return 0;
}
7. Percentage Calculation
#include <stdio.h>

int main() {
float totalMarks, obtainedMarks, percentage;
printf("Enter obtained and total marks: ");
scanf("%f %f", &obtainedMarks, &totalMarks);

percentage = (obtainedMarks / totalMarks) * 100;


printf("Percentage = %.2f%%\n", percentage);
return 0;
}

8. Checking Leap Year


#include <stdio.h>

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)


printf("%d is a Leap Year\n", year);
else
printf("%d is Not a Leap Year\n", year);

return 0;
}

9. Checking the Largest of Two Numbers


#include <stdio.h>

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

if (a > b)
printf("%d is larger.\n", a);
else if (b > a)
printf("%d is larger.\n", b);
else
printf("Both numbers are equal.\n");

return 0;
}

10. Grade Calculation


#include <stdio.h>
int main() {
float marks;
printf("Enter marks (0-100): ");
scanf("%f", &marks);

if (marks >= 90)


printf("Grade: A\n");
else if (marks >= 80)
printf("Grade: B\n");
else if (marks >= 70)
printf("Grade: C\n");
else if (marks >= 60)
printf("Grade: D\n");
else if (marks >= 50)
printf("Grade: E\n");
else
printf("Grade: F (Fail)\n");

return 0;
}

11. Multiplication Table


#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Multiplication Table of %d:\n", num);


for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}

return 0;
}

12. Absolute Value of a Number


#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num < 0)
num = -num;

printf("Absolute value = %d\n", num);


return 0;
}
13. Character Classification (Vowel or Consonant)
#include <stdio.h>

int main() {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
switch (ch) {
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case 'u':
printf("Vowel\n");
break;
default:
printf("Consonant\n");
}
} else {
printf("Not an alphabet.\n");
}

return 0;
}

14. Checking Divisibility by 5 and 11


#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 5 == 0 && num % 11 == 0)


printf("%d is divisible by both 5 and 11\n", num);
else
printf("%d is not divisible by both 5 and 11\n", num);

return 0;
}

15. Alphabet, Digit, or Special Character


#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
printf("Alphabet\n");
else if (ch >= '0' && ch <= '9')
printf("Digit\n");
else
printf("Special Character\n");

return 0;
}

16. Temperature Conversion Decision (Celsius <-> Fahrenheit)


#include <stdio.h>

int main() {
float temp;
char choice;
printf("Enter C for Celsius to Fahrenheit or F for Fahrenheit to Celsius:
");
scanf(" %c", &choice);

if (choice == 'C' || choice == 'c') {


printf("Enter temperature in Celsius: ");
scanf("%f", &temp);
printf("Fahrenheit = %.2f\n", (temp * 9 / 5) + 32);
} else if (choice == 'F' || choice == 'f') {
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &temp);
printf("Celsius = %.2f\n", (temp - 32) * 5 / 9);
} else {
printf("Invalid choice.\n");
}

return 0;
}

17. Admission Based on Age and Marks


#include <stdio.h>

int main() {
int age;
float marks;
printf("Enter age and marks: ");
scanf("%d %f", &age, &marks);

if (age >= 18 && marks >= 60)


printf("Admission Granted\n");
else
printf("Admission Denied\n");

return 0;
}

18. Checking Triangle Type Based on Sides


#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);

if (a + b > c && a + c > b && b + c > a) {


if (a == b && b == c)
printf("Equilateral Triangle\n");
else if (a == b || b == c || a == c)
printf("Isosceles Triangle\n");
else
printf("Scalene Triangle\n");
} else {
printf("Not a valid triangle\n");
}

return 0;
}

You might also like