C Program to Find Area of a Circle.

In this C program, we are going to find the area of a circle for which the radius is given. The area of a circle is the measure of the region enclosed by the circle. 

Area of Circle Formula:
Area (A) = π * radius^2

Example: 
Input: r = 5
Output: 78.5 sq units

Explanation: 
Area = 3.14 * 5 * 5
     = 3.14 * 25
     = 78.5 square units

Step-by-step Algorithm:

Step 1: Input the radius (r) of the circle from the user.
Step 2: Calculate the area (A) of the circle using the formula: A = π * r^2.
Step 3: Display the calculated area of the circle.

C program to Print Aread of a Circle.

//C implementation to find Area of a circle
#include <stdio.h>
#define PI 3.14

int main() {
    float radius, area;

    // radius of the circle
    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    // area of the circle
    area = PI * radius * radius;

    printf("Area of the circle: %.2f square units\n", area);

    return 0;
}
Output:
Enter the radius of the circle: 5
Area of the circle: 78.50 square units

C Program to Calculate Compound Interest.

In this C program, we will calculate compound interest using user-provided inputs. Before learning the code part, let's understand compound interest and how to calculate it.


What is Compound Interest?

Compound interest is the interest that is added to the principal amount of an investment or loan at regular intervals. The interest is calculated not only on the initial principal amount but also on the accumulated interest from previous periods. As a result, the amount of interest earned keeps increasing with each compounding period.


Compound Interest Formula:

The formula to calculate compound interest is given by:

Compound Interest (CI) = P * (1 + R/n)^(n*T) - P

Where:
  • CI is the Compound Interest.
  • P is the Principal Amount (initial amount invested or borrowed).
  • R is the Annual Interest Rate in percentage (as a decimal).
  • T is the Time Duration in years.
  • n is the number of times the interest is compounded per year.
Example: 
Let's understand with one example. Suppose a person invests 10,000 rupees in a bank account that offers a 5% annual interest rate, compounded annually.
Given Data:
Principal amount (P) = 10,000 rupees
Annual interest rate (R) = 5% (or 0.05 as a decimal)
Time duration (T) = 3 years
Number of times interest is compounded per year (n) = 1 

Solution: 
CI = 10000 * (1 + 0.05/1)^(1*3) - 10000
   = 10000 * (1.05)^3 - 10000
   = 10000 * 1.157625 - 10000
   = 11576.25 - 10000
   = 1576.25 rupees

C program to find the Compound Interest.

// C program to calculate compound interest  
#include <stdio.h>
#include <math.h>

int main() {
    float principal, rate, time, compoundInterest;
    int n;

    // Input the principal amount
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    // Input the annual interest rate (as a decimal)
    printf("Enter the annual interest rate (as a decimal): ");
    scanf("%f", &rate);

    // Input the time duration in years
    printf("Enter the time duration in years: ");
    scanf("%f", &time);

    // Input the number of times interest is compounded per year
    printf("Enter the number of times interest is compounded per year: ");
    scanf("%d", &n);

    // Calculate the compound interest
    compoundInterest = principal * pow(1 + rate / n, n * time) - principal;

    // Display the calculated compound interest
    printf("Compound Interest: %.2f\n", compoundInterest);

    return 0;
}
Output:
Enter the principal amount: 10000
Enter the annual interest rate (as a decimal): 0.05
Enter the time duration in years: 5
Enter the number of times interest is compounded per year: 1
Compound Interest: 2762.81

Note: In the above program, enter the interest rate in decimal format, like 5 % interest rate is equal to 5/100 = 0.05 to get the correct result.

C Program to Calculate Simple Interest.

In this C programming tutorial, we will learn how to calculate simple interest using user-provided inputs. Simple interest is a fundamental concept in finance and is widely used in various financial transactions. It allows us to determine the interest earned or paid on a principal amount over a specific time period.

Formula to calculate Simple Interest.

Simple Interest (SI) can be calculated using the formula:
SI = (P * R * T) / 100

Where:
  • SI is the Simple Interest.
  • P is the Principal Amount (initial amount invested or borrowed).
  • R is the Rate of Interest per period (usually expressed as a percentage).
  • T is the Time Duration in years.
Example:
Example:
P = 5000
R = 6
T = 3

Explanation:
SI = (P * R * T) / 100
   = (5000 * 6 * 3) / 100
   = (90,000) / 100
   = 900

Step-by-step Algorithm.

Step 1: Input the principal amount (P), which is the initial amount invested or borrowed.
Step 2: Input the rate of interest per period (R), usually expressed as a percentage.
Step 3: Input the time duration in years (T) for which the amount is invested or borrowed.
Step 3: Calculate the simple interest (SI) using the formula: SI = (P * R * T) / 100
Step 4: Display the calculated simple interest.

C code to find Simple Interest.

//C program to find simple interest
#include <stdio.h>

int main() {
    float principal, rate, time, simpleInterest;

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

    printf("Enter the rate of interest per period: ");
    scanf("%f", &rate);

    printf("Enter the time duration in years: ");
    scanf("%f", &time);

    // Calculate the simple interest
    simpleInterest = (principal * rate * time) / 100;

    printf("Simple Interest: %.2f\n", simpleInterest);

    return 0;
}
Output:
Enter the principal amount: 6000
Enter the rate of interest per period: 5
Enter the time duration in years: 6
Simple Interest: 1800.00

Time Complexity: O(1)
Space Complexity: O(1)

C Program to Find Size of int, float, char and double.

In this C program, we will determine the size of various data types such as int, float, char, and double. The size of a data type represents the number of bytes it occupies in memory. We will use the sizeof operator to calculate the size of each data type.

Data Types


Steps to find the size of a data type:

Step 1: Declare variables of each data type (int, float, char, and double).
Step 2: Use the sizeof operator to determine the size of each data type and store the results in variables.
Step 3: Display the size of each data type in bytes.
Note%zu format specifier is used to print the result of the sizeof operator, which gives the size of a data type in bytes. (alert-passed)

C program to find the size of different data types. 

//C program to find size of data types
#include <stdio.h>

int main() {
    int intSize;
    float floatSize;
    char charSize;
    double doubleSize;

    // size of each data type
    printf("Size of int data type: %zu \n", sizeof(intSize));
    printf("Size of float data type: %zu \n", sizeof(floatSize));
    printf("Size of char data type: %zu \n", sizeof(charSize));
    printf("Size of double data type: %zu \n", sizeof(doubleSize));

    return 0;
}
Output:
Size of int data type: 4 
Size of float data type: 4 
Size of char data type: 1 
Size of double data type: 8 

In this program, we aimed to determine the size of different data types in C. The int and float data types occupy 4 bytes each, char occupies 1 byte, and double occupies 8 bytes. The size of data types may vary depending on the system architecture and compiler used.

C program to Add Two Complex Numbers.

In this article, we will write a C program to add two complex numbers. A complex number is represented in the form of a + bi, where a is the real part, b is the imaginary part, and i is the imaginary unit (√(-1)).

Example: 

Input: Z1 = 4 + 2i, Z2 = 2 + 7i
Output: 6 + 9i

Explanation:
Z3 = Z1 + Z2
   = (4 + 2i) + (2 + 7i)
   = (4 + 2) + (2 + 7)i
   = 6 + 9i 

Add two complex numbers

Steps to Add Two Complex Numbers.

Below are the steps that need to follow to add two complex numbers:

Step 1: Input the real and imaginary parts of the first complex number.
Step 2: Input the real and imaginary parts of the second complex number.
Step 3: Add the real parts of both complex numbers.
Step 4: Add the imaginary parts of both complex numbers.
Step 5: Display the sum of the complex numbers.

C program to add two complex numbers.

//C program to add two complex numbers
#include <stdio.h>

struct complex {
    float real;
    float imag;
};

int main() {
    struct complex z1, z2, sum;

    printf("Enter first complex number (a + bi):\n");
    scanf("%f %f", &z1.real, &z1.imag);

    printf("Enter second complex number (a + bi):\n");
    scanf("%f %f", &z2.real, &z2.imag);

    // Add the real parts of both complex numbers
    sum.real = z1.real + z2.real;

    // Add the imaginary parts of both complex numbers
    sum.imag = z1.imag + z2.imag;

    printf("Sum of the complex numbers: %.2f + %.2fi\n", sum.real, sum.imag);

    return 0;
}
Output:
Enter first complex number (a + bi):
2 3
Enter second complex number (a + bi):
4 5
Sum of the complex numbers: 6.00 + 8.00i

In the above example code, the struct part is used to define a user-defined data type called complex. This custom data type allows us to group two floating-point variables (real and imag) together, representing the real and imaginary parts of a complex number.

Then we created two complex type variables, z1, and z2, and one sum variable to store the result of addition.

C Program to Calculate Fahrenheit to Celsius and Vice-versa.

Fahrenheit to Celsius is a temperature conversion formula used to convert temperatures measured in degrees Fahrenheit (°F) to degrees Celsius (°C) and vice-versa. In this article, we are going to learn how to write a C programming to perform this conversion.

Fahrenheit to Celsius and Vice-versa.

Fahrenheit to Celsius Conversion:

To convert Fahrenheit to Celsius, subtract 32 from the Fahrenheit temperature and then multiply the result by 5/9. The formula is as follows:

Celsius = (Fahrenheit - 32) * 5/9

For example, if the user inputs 68 degrees Fahrenheit, the conversion would be:

Celsius = (68 - 32) * 5/9 = 20 degrees Celsius.


Celsius to Fahrenheit Conversion:

To convert Celsius to Fahrenheit, multiply the Celsius temperature by 9/5 and then add 32. The formula is as follows:

Fahrenheit = (Celsius * 9/5) + 32

For example, if the user inputs 25 degrees Celsius, the conversion would be:

Fahrenheit = (25 * 9/5) + 32 = 77 degrees Fahrenheit.


C program to convert temperature from Fahrenheit to Celsius.

//C program to calculate celsius from fahrenheit
#include <stdio.h>

int main() {
    float fahrenheit, celsius;

    // Input the temperature in Fahrenheit
    printf("Enter the temperature: ");
    scanf("%f", &fahrenheit);

    // Fahrenheit to Celsius conversion
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("%.2f degrees Fahrenheit is equal to %.2f degrees Celsius.\n",
           fahrenheit, celsius);

    return 0;
}
Output:
Enter the temperature: 34
34.00 degrees Fahrenheit is equal to 1.11 degrees Celsius.

Time Complexity: O(1)
Space Complexity: O(1)

C program to convert temperature from Celsius to Fahrenheit.

//C program to calculate fahrenheit from celsius
#include <stdio.h>

int main() {
    float fahrenheit, celsius;

    // Input the temperature in celsius
    printf("Enter the temperature: ");
    scanf("%f", &celsius);

    // Celsius to Fahrenheit conversion
    fahrenheit = (celsius * 9 / 5) + 32;
    printf("%.2f degrees Celsius is equal to %.2f degrees Fahrenheit.\n",
           celsius, fahrenheit);

    return 0;
}
Output:
Enter the temperature: 32
32.00 degrees Celsius is equal to 89.60 degrees Fahrenheit.

Time Complexity: O(1)
Space Complexity: O(1)

C Program to Find Quotient and Remainder.

To find the quotient and remainder of two numbers in a C program, we can use the division and modulus operators. The division operator (/) calculates the quotient, and the modulus operator (%) calculates the remainder.


Example:

Input: a = 10, b = 3
Output: Quotient = 3, Remainder = 1

Input: a = 25, b = 4
Output: Quotient = 6, Remainder = 1

Here are the steps to find the quotient and remainder of two numbers:

Steps:

Step 1: Input the two numbers from the user (dividend and divisor).
Step 2: Calculate the quotient by dividing the dividend by the divisor using the division operator (/).
Step 3: Calculate the remainder by taking the modulus of the dividend with the divisor using the modulus operator (%).
Step 4: Display the calculated quotient and remainder to the user.

C code to find quotient and remainder.

//C Program to find quotient and remainder
#include <stdio.h>

int main() {
    int dividend, divisor, quotient, remainder;

    // Input the two numbers from the user
    printf("Enter the dividend: ");
    scanf("%d", &dividend);
    printf("Enter the divisor: ");
    scanf("%d", &divisor);

    // Calculate the quotient
    quotient = dividend / divisor;

    // Calculate the remainder
    remainder = dividend % divisor;

    // Display the results
    printf("Quotient: %d\n", quotient);
    printf("Remainder: %d\n", remainder);

    return 0;
}
Output:
Enter the dividend: 15
Enter the divisor: 4
Quotient: 3
Remainder: 3

Dividing 15 by 4 gives a quotient of 3 and a remainder of 3. The result can be written as 15 ÷ 4 = 3 with a remainder of 3.

Time Complexity: O(1)
Space Complexity: O(1)

C Program to Find Quadrant of a Point in Cartesian Plan.

In this article, we are going to learn the C program to find the quadrant of a given coordinate point. Before moving to the code section let's understand about Cartesian plan.


What is Cartesian Plan?

The Cartesian plane, also known as the Cartesian coordinate system or Cartesian grid, is a two-dimensional coordinate system used to locate points in a plane using two perpendicular number lines, known as the x-axis and y-axis


In the Cartesian plane, any point can be represented by an ordered pair (x, y), where "x" represents the horizontal distance from the y-axis (positive to the right and negative to the left), and "y" represents the vertical distance from the x-axis (positive upward and negative downward).


The x-axis and y-axis intersect at a point called the origin, represented as (0, 0). The x-axis extends infinitely to the right and left, while the y-axis extends infinitely upwards and downwards.


Quadrant of point in Cartesian Plan

Quadrant in Cartesian Plan.

In a Cartesian plane, there are four quadrants. The x-axis and y-axis divide the plane into four regions, each known as a quadrant.


First Quadrant (Q1): The first quadrant is located in the upper right-hand side of the plane. In this quadrant, both the x and y coordinates are positive. 

Example: (2, 3)


Second Quadrant (Q2): The second quadrant is on the upper left-hand side of the plane. In this quadrant, the x coordinate is negative, and the y coordinate is positive.

Example: (2, -3)


Third Quadrant (Q3): The third quadrant is on the lower left-hand side of the plane. In this quadrant, both the x and y coordinates are negative.

Example: (-2, -3)


Fourth Quadrant (Q4): The fourth quadrant is on the lower right-hand side of the plane. In this quadrant, the x coordinate is positive, and the y coordinate is negative.

Example: (2, -3)


C code to find the quadrant of a given point.

//C program to find quadrant of a point
#include <stdio.h>

int main() {
    int x, y;

    // Input the coordinates of the point
    printf("Enter the x-coordinate: ");
    scanf("%d", &x);
    printf("Enter the y-coordinate: ");
    scanf("%d", &y);

    // Determine the quadrant of the point
    if (x > 0 && y > 0) {
        printf("(%d, %d) lies in the First Quadrant (Q1).\n", x, y);
    } else if (x < 0 && y > 0) {
        printf("(%d, %d) lies in the Second Quadrant (Q2).\n", x, y);
    } else if (x < 0 && y < 0) {
        printf("(%d, %d) lies in the Third Quadrant (Q3).\n", x, y);
    } else if (x > 0 && y < 0) {
        printf("(%d, %d) lies in the Fourth Quadrant (Q4).\n", x, y);
    } else if (x == 0 && y == 0) {
        printf("(%d, %d) is at the origin.\n", x, y);
    } else if (x == 0) {
        printf("(%d, %d) lies on the y-axis.\n", x, y);
    } else {
        printf("(%d, %d) lies on the x-axis.\n", x, y);
    }

    return 0;
}
Output:
Enter the x-coordinate: 2
Enter the y-coordinate: -3
(2, -3) lies in the Fourth Quadrant (Q4).

Time Complexity: O(1) 
Space Complexity: O(1)

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson