C Programs: Area, Circumference, Interest, Swapping

Calculate Area and Circumference of a Circle

#include <stdio.h>

void main() {
  float radius, area, cf;

  printf("Enter Radius of Circle\n");
  scanf("%f", &radius);

  // Value of pi is 3.14
  area = 3.14 * radius * radius;
  printf("The area of Circle is %f", area);

  cf = 2 * 3.14 * radius;
  printf("\nThe Circumference of Circle is %f", cf);
}

Calculate Area of Triangle and Square

Calculate Area of a Triangle

#include <stdio.h>

int main() {
  float base, height, area;

  printf("Enter the base of the triangle: ");
  scanf("%f", &base);
  printf("Enter the height of the triangle: ");
  scanf("%f", &height);

  area = (base * height) / 2;
  printf("The area of the triangle is: %.2f\n", area);
  return 0;
}

Calculate Area of a Square

#include <stdio.h>

int main() {
  int side, area;

  printf("\nEnter the Length of Side : ");
  scanf("%d", &side);

  area = side * side;
  printf("\nArea of Square : %d", area);
  return (0);
}

Calculate Area of a Rectangle

#include <stdio.h>

int main() {
  int width = 5;
  int height = 10;
  int area = width * height;

  printf("Area of the rectangle = %d", area);
}

Program for Swapping Two Numbers

#include <stdio.h>
#include <conio.h>

int main() {
  int first_number, second_number, temp;

  printf("Enter first number: ");  // Allow user to add first number
  scanf("%d", &first_number);
  printf("Enter second number: "); // Allow user to add second number
  scanf("%d", &second_number);

  printf("Before swapping \n");
  printf("First number: %d \n", first_number);
  printf("Second number: %d \n", second_number);

  temp = first_number;          // First number is assigned to temp
  first_number = second_number;  // Second number is assigned to first number
  second_number = temp;          // First number is assigned to second number

  printf("After swapping \n");
  printf("First number: %d \n", first_number);
  printf("Second number: %d \n", second_number);
  return 0;
}

Calculate Simple Interest

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  // Simple interest program
  int principal, rate, time, interest;

  printf("Enter the principal: ");
  scanf("%d", &principal);

  printf("Enter the rate: ");
  scanf("%d", &rate);

  printf("Enter the time: ");
  scanf("%d", &time);

  interest = principal * rate * time / 100;
  printf("The Simple interest is %d", interest);

  return 0;
}

Calculate Average of 5 Subject Marks

#include <stdio.h>

int main() {
  float marks, tot_marks = 0;
  int i = 0, subject = 0;

  // Prompt user for input
  printf("Input five subject marks(0-100):\n");

  // Loop to read marks for five subjects
  while (subject != 5) {
    // Read a float value 'marks' from user
    scanf("%f", &marks);

    // Check if 'marks' is within valid range (0-100)
    if (marks < 0 || marks > 100) {
      printf("Not a valid marks\n"); // Print an error message
    } else {
      tot_marks += marks; // Accumulate valid marks
      subject++; // Increment subject count
    }
  }

  // Calculate and print the average marks
  printf("Average marks = %.2f\n", tot_marks / 5);

  return 0; // End of program
}