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)

C Program to Find Factorial of a Number.

In this article, we are going to learn how to find the factorial of a number using C programming. Before moving to the program part, we first need to get a basic understanding of Factorial.

Factorial of a Number Formula

What is Factorial?

Factorial is a mathematical operation used to calculate the product of all positive integers from 1 up to a given number. It is denoted by the symbol !. For example, the factorial of 5 is represented as 5! and calculated as follows:

Factorial of 5 is:
5! = 5 × 4 × 3 × 2 × 1 = 120

Factorial of 4 is:
4! = 4 × 3 × 2 × 1 = 24

Factorials are commonly used in mathematics and various applications, such as combinations and permutations. They grow rapidly as the input number increases, and the factorial of 0 is defined as 1. Factorials are integral in solving problems related to counting arrangements, possibilities, and probabilities.

Find the Factorial of a Number.

Approach 1: Using the Iterative Method.

In this approach, we calculate the factorial of a number using a loop to multiply the numbers from 1 to the given number.

Step-by-step algorithm:

Step 1: Input the number for which you want to find the factorial.
Step 2: Initialize a variable "factorial" to 1.
Step 3: Use a loop to multiply "factorial" by numbers from 1 to the input number.
Step 4: Repeat the multiplication until the loop reaches the input number.
Step 5: The final value of "factorial" will be the factorial of the input number.

C Program to find the Factorial of a number using for loop.
//C program to find factorial using for loop
#include <stdio.h>

unsigned int findFactorialIterative(int num) {
    unsigned int factorial = 1;

    for (int i = 1; i <= num; i++) {
        factorial *= i;
    }

    return factorial;
}

int main() {
    int number;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    if (number < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d is %d.\n", number, findFactorialIterative(number));
    }

    return 0;
}
Output:
Enter a positive integer: 5
Factorial of 5 is 120.

Time Complexity: O(n) where n is the input number.
Space Complexity: O(1) as no extra space is required.

Approach 2: Using Recursive Method.

In this approach, we are using a recursive method to find the factorial of a number that uses a function that calls itself with smaller subproblems until it reaches the base case.

Step-by-step algorithm:

Step 1: Input the number for which you want to find the factorial.
Step 2: Create a recursive function to calculate the factorial.
Step 3: The base case is when the input number is 0 or 1, return 1 (factorial of 0 and 1 is 1).
Step 4: For other numbers, call the recursive function with (num - 1) and multiply it by "num."
Step 5: The final result obtained from the recursive calls will be the factorial of the input number.

C Program to find the Factorial of a num using Recursion.
//C implementation to find factorial of a number using recursion
#include <stdio.h>

unsigned int findFactorialRecursive(int num) {
    //base case
    if (num == 0 || num == 1) {
        return 1;
    } else {
        return num * findFactorialRecursive(num - 1);
    }
}

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

    if (number < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d is %d.\n", number, findFactorialRecursive(number));
    }

    return 0;
}
Output:
Enter a positive integer: 8
Factorial of 8 is 40320.

Time Complexity: The time complexity of the recursive method is also O(n), where "n" is the input number.
Space Complexity: The space complexity is O(n) as stack space is used to perform recursion.

C Program to Swap Two Numbers.

Swapping is a way of exchanging the values of two different variables. In C programming, we have multiple approaches to swap two numbers efficiently. In this article, we will learn each approach one by one.

Swap two numbers

Approach 1: Swap two numbers using a Temporary variable.

In this approach, we take the help of a third temp variable for swapping values.

 

Step-by-step algorithm:

Step 1: Input the two numbers from the user.

Step 2: Create a temporary variable to hold the value of one of the numbers.

Step 3: Assign the value of the first number to the temporary variable.

Step 4: Assign the value of the second number to the first number.

Step 5: Assign the value of the temporary variable to the second number.

Step 6: Print the swapped values of the two numbers.


Below is the C program to swap two numbers using the third temporary variable.

//C Program to swap two numbers usingn third variable
#include <stdio.h>

int main() {
    int num1, num2, temp;

    // Input the two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Create a temporary variable and swap the numbers
    temp = num1;
    num1 = num2;
    num2 = temp;

    // Print the swapped values
    printf("Swapped values: %d %d\n", num1, num2);

    return 0;
}
Output:
Enter two numbers: 20 10
Swapped values: 10 20

Time Complexity: As it is performing fix number of operations regardless of input so time complexity is O(1).
Space Complexity: As no extra space is required except for one variable so space complexity is O(1)


Approach 2: Swap two numbers using Arithmetic Operations.

In this approach, we do not require any third temp variable for swapping, instead, we perform some arithmetic operations to do so.

Step-by-step algorithm:

Step 1: Input the two numbers from the user.
Step 2: Perform the following arithmetic operations:
  • Add the first number to the second number and store the result in the first number.
  • Subtract the second number from the first number and store the result in the second number.
  • Subtract the second number (new value) from the first number (new value) and store the result in the first number.
Step 3: Print the swapped values of the two numbers.

Below is the C program to swap two numbers without using a third variable.

//C program to swap two number without third variable
#include <stdio.h>

int main() {
    int num1, num2;

    // Input the two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Perform arithmetic operations to swap the numbers
    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;

    // Print the swapped values
    printf("Swapped values: %d %d\n", num1, num2);

    return 0;
}
Output:
Enter two numbers: 20 10
Swapped values: 10 20

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

Approach 3: Swap two numbers using Bitwise XOR Operation.

In this approach, we use the bitwise XOR (^) operation to swap two numbers without the need for a temporary variable. The XOR operation between two bits is 1 if the bits are different and 0 if they are the same. By applying XOR between the binary representations of two numbers, we can effectively swap their values. 

Step-by-step algorithm:

Step 1: Input the two numbers from the user.
Step 2: Perform the bitwise XOR operation between the two numbers and store the result in the first number.
Step 3: Perform the bitwise XOR operation between the first number (new value) and the second number and store the result in the second number.
Step 4: Perform the bitwise XOR operation between the first number (new value) and the second number (new value) and store the result in the first number.
Step 5: Print the swapped values of the two numbers.

Below is the C program to swap two numbers using a bitwise operation.
//C program to swap to number using XOR operation
#include <stdio.h>

int main() {
    int num1, num2;

    // Input the two numbers
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    // Perform bitwise XOR operations to swap the numbers
    num1 = num1 ^ num2;
    num2 = num1 ^ num2;
    num1 = num1 ^ num2;

    // Print the swapped values
    printf("Swapped values: %d %d\n", num1, num2);

    return 0;
}
Output:
Enter two numbers: 8 10
Swapped values: 10 8

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

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson