Tag: c basic programs

  • Currency Conversion Program in C

    In this tutorial, we will write a C program for currency conversion. Before that, you may go through the following topic in C.

    Explanation: In this program, there is a use of switch statements in C. The program gives the user choice for the conversion. For this example, we have taken four currencies those are Rupee, Dollar, Pound and Euro.

    There are four cases for each currency conversion. You can modify the value of a currency with the actual current value.


    Currency Conversion Program in C

    #include <stdio.h>
    
    int main()
    {
      float amount;
      float rupee, dollar, pound, euro;
      int choice;
    
      printf("Following are the Choices:");
      printf("\nEnter 1: Ruppe");
      printf("\nEnter 2: Dollar");
      printf("\nEnter 3: Pound");
      printf("\nEnter 4: Euro");
    
      printf("\nEnter your choice: ");
      scanf("%d", &choice);
    
      printf("Enter the amount you want to convert?\n");
      scanf("%f", &amount);
    
      switch (choice)
      {
        case 1: // Ruppe Conversion
            dollar = amount / 70;
            printf("%.2f Rupee =  %.2f dollar", amount, dollar);
    
            pound = amount / 88;
            printf("\n%.2f Rupee =  %.2f pound", amount, pound);
    
            euro = amount / 80;
            printf("\n%.2f Rupee =  %.2f euro", amount, euro);
            break;
    
        case 2: // Dollar Conversion
            rupee = amount * 70;
            printf("\n%.2f Dollar =  %.2f rupee", amount, rupee);
    
            pound = amount *0.78;
            printf("\n%.2f Dollar =  %.2f pound", amount, pound);
    
            euro = amount *0.87;
            printf("\n%.2f Dollar =  %.2f euro", amount, euro);
            break;
    
        case 3: // Pound Conversion
            rupee = amount * 88;
            printf("\n%.2f Pound =  %.2f rupee", amount, rupee);
    
            dollar = amount *1.26;
            printf("\n%.2f Pound =  %.2f dollar", amount, dollar);
    
            euro = amount *1.10;
            printf("\n%.2f Pound =  %.2f euro", amount, euro);
            break;
    
        case 4: // Euro Conversion
            rupee = amount * 80;
            printf("\n%.2f Euro =  %.2f rupee", amount, rupee);
    
            dollar = amount *1.14;
            printf("\n%.2f Euro =  %.2f dollar", amount, dollar);
    
            pound = amount *0.90;
            printf("\n.2%f Euro =  %.2f pound", amount, pound);
            break;
    
         //Default case
        default:
            printf("\nInvalid Input");
      }
    
      return 0;
    }

    Output:

    Following are the Choices:
    Enter 1: Ruppe
    Enter 2: Dollar
    Enter 3: Pound
    Enter 4: Euro
    Enter your choice: 2
    Enter the amount you want to convert?
    7

    7.00 Dollar = 490.00 rupee
    7.00 Dollar = 5.46 pound
    7.00 Dollar = 6.09 euro

    There is a use of %.2f” which means only two digits after the decimal value will be displayed.


  • C Program to Find the Area of a Rectangle

    In this tutorial, we will write a C program to find the area of a rectangle. Before that, you may go through the following topic in C.

    Explanation: The program asks the user to enter the value of length and breadth as an input. Then simply using the formula (area = length * breadth), we calculate the area of a rectangle in C.


    C Program to find the area of a Rectangle

    #include <stdio.h>
    
    int main()
    {
      int len, br, area;
    
      printf("Enter the length: ");
      scanf("%d", &len);
      printf("Enter the breadth: ");
      scanf("%d", &br);
    
      area = len * br;
      printf("Area of a rectangle: %d", area);
    
      return 0;
    }

    Output:

    Enter the length: 2
    Enter the breadth: 5
    Area of a rectangle: 10

    You may change the data type int to float or any other as per your requirements.


  • Menu Driven Program using Switch Case in C

    In this tutorial, we will write a menu-driven program in C. We will use the switch case statement present in C to create menus or options. Before that, you may go through the following topic in C programming.

    C Menu-driven Program using switch case:

    This is a menu program in C that displays a menu and takes the input from the user. The choice made by the user is among the option displayed in the menu. And accordingly, the output is displayed on the screen. It is a user-friendly program in C.


    Menu Driven Program using Switch Case in C

    #include <stdio.h>
    
    int main()
    {
      int choice;
    
      printf("To find the areas of following press:");
      printf("\n1 for the Area of a Circle");
      printf("\n2 for the Area of a Square");
      printf("\n3 for the Area of a Right Angled Triangle");
      printf("\n4 for the Area of a Rectangle");
      printf("\n5: Area of Rhombus");
      printf("\nEnter your Choice: ");
      scanf("%d", &choice);
    
      switch (choice)
      {
        case 1:
          printf("Enter radius for the circle:");
          float r, area;
          scanf("%f", &r);
          area = 3.14f *r * r;
          printf("Area: %f", area);
          break;
    
        case 2:
          printf("Enter side for square:");
          int s;
          scanf("%d", &s);
          int ae = s * s;
          printf("Area: %d", ae);
          break;
    
        case 3:
          printf("Enter height and base for traingle:\n");
          float h, bs;
          scanf("%f", &h);
          scanf("%f", &bs);
          float ar = 0.5f *h * bs;
          printf("Area: %f", ar);
          break;
    
        case 4:
          printf("Enter length and breadth for rectangle:\n");
          int l, b;
          scanf("%d", &l);
          scanf("%d", &b);
          int aa = l * b;
          printf("Area: %d", aa);
          break;
    
        case 5:
          printf("Enter the first diagonal of a Rhombus: ");
          float diagonal1, diagonal2;
          scanf("%f", &diagonal1);
          printf("Enter the second diagonal of the Rhombus: ");
          scanf("%f", &diagonal2);
          float aRhombus = (diagonal1 *diagonal2) / 2;
          printf("Area of the Rhombus is: %f", aRhombus);
          break;
    
        default:
          printf("Invalid Input");
          break;
      }
    
      return 0;
    }

    Output: 1

    To find the areas of following press:
    1 for the Area of a Circle
    2 for the Area of a Square
    3 for the Area of a Right Angled Triangle
    4 for the Area of a Rectangle
    5: Area of Rhombus
    Enter your Choice: 1
    Enter radius for the circle:2
    Area: 12.560000

    Output: 2

    To find the areas of following press:
    1 for the Area of a Circle
    2 for the Area of a Square
    3 for the Area of a Right Angled Triangle
    4 for the Area of a Rectangle
    5: Area of Rhombus
    Enter your Choice: 4
    Enter length and breadth for rectangle:
    5
    4
    Area: 20

    Similarly, you can create a calculator or any other program that serves a menu of options to choose from. You can use if..else statement instead of switch case.


  • C Program to check whether a Number is Binary or Not

    In this tutorial, we will write a program to check for the binary number in C.

    What is a Binary Number?

    A binary number is a number expressed in the base-2 numeral system that is the number that contains 2 symbols to represent all the numbers. The symbols are 0 and 1.

    For example: 101011, 110011110, 10001111 are binary numbers.


    C Program to check whether a Number is Binary or Not

    We have used the while loop to iterate through the entered number in the program below. The program takes the input of a number from the user.

    With each iteration, there is an if condition to check for 1 and 0 in the digit, If it is not 1 or 0 then it is not a binary number.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int i, number;
    
      printf("Enter the Number: ");
      scanf("%d", &number);
    
      int temp = number;
    
      while (number > 0)
      {
        i = number % 10;
    
        if (i != 0 && i != 1)
        {
          printf("%d is not a binary number", temp);
          break;
        }
    
        number /= 10;
    
        if (number == 0)
        {
          printf("%d is a binary number", temp);
        }
      }
    
      getch();
      return 0;
    }

    Output:

    Enter the Number: 1101
    1101 is a binary number


  • C Program to Print ASCII Value of a Character

    In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on the following topic in C.

    ASCII stands for American Standard Code for Information Interchange. It is a 7-bit character set that contains 128 (0 to 127) characters. It represents the numerical value of a character.

    Example: ASCII value for character A is 65, B is 66 and a is 97, b is 98, and so on.


    C Program to Print ASCII Value of a Character

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      char ch;
      printf("Enter a Character: ");
      scanf("%c", &ch);
    
      printf("Its ASCII Value: %d", ch);
    
      getch();
      return 0;
    }

    Output:

    Enter a Character: A
    Its ASCII Value: 65


    C Program to print ASCII value of all Characters

    Source Code: Print the ASCII value of all the characters in C.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int i;
      printf("ASCII Value of all the Characters.\n");
    
      for (i = 0; i < 255; i++)
        printf("ASCII value of %c: %i\n", i, i);
    
      getch();
      return 0;
    }

    Output: You will get the list of all the characters starting from 0 to 255 on the output display screen.

    ASCII value of character in C

  • C++ Program to Convert Fahrenheit to Celsius

    In this tutorial, we will learn the conversion of a temperature from Fahrenheit to Celsius (or Centigrade) in C++. Let us start by understanding the formula for the conversion of Fahrenheit into Celsius.

    Fahrenheit and Celsius, both are the unit for measuring the temperature. Fahrenheit is represented by oF and Celsius by oC.

    Formula to convert Fahrenheit into Celsius

    celsius = (fahrenheit – 32)*5/9

    We will write two programs for the conversion of Fahrenheit to Celsius in C++.

    1. Without the use of Function
    2. With the use of function

    Fahrenheit to Celsius in C++

    The program simply asks the user for the Fahrenheit temperature value in order to convert temperature from Fahrenheit to Celsius in C++. Then converts into its equivalent Celsius value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       float f, c;
    
       cout << "Enter the Fahrenheit value: ";
       cin >> f;
    
       // conversion calculation
       c = (f - 32) * 5/9;
    
       cout << "Equivalent Celsius value: " << c;
    
       return 0;
    }

    Output:

    Enter the Fahrenheit value: 64
    Equivalent Celsius value: 17.7778


    C++ Program to convert Fahrenheit to Celsius using function

    Here, we create a separate user-defined function in C++ to convert Fahrenheit to Celsius. The value of Fahrenheit entered by the user is passed to a function as an argument. The function returns the value after the calculation of the celsius value.

    #include <iostream>
    using namespace std;
    
    // conversion function
    float conversion(float f)
    {
       float c;
    
       c = (f - 32) * 5/9;
       return c;
    }
    
    int main()
    {
       float f, c;
    
       cout << "Enter the Fahrenheit value: ";
       cin >> f;
    
       // calling function
       c = conversion(f);
    
       cout << "Equivalent Celsius value: " << c;
    
       return 0;
    }

    Output:

    Enter the Fahrenheit value: 80
    Equivalent Celsius value: 26.6667


  • C Program to Convert Fahrenheit to Celsius

    In this tutorial, we will write a C Program to convert Fahrenheit into Celsius. Let us start by understanding the formula for the conversion of Fahrenheit into Celsius.

    Fahrenheit and Celsius, both are the unit for measuring the temperature. Fahrenheit is represented by oF and Celsius by oC.

    Formula to convert Fahrenheit into Celsius

    celsius = (fahrenheit – 32)*5/9


    C ProgrC Program to Convert Temperature from Fahrenheit to Celsius

    #include<stdio.h>
     
    void main()
    {
        float c,f;
     
        //user input
        printf("Enter the Fahrenheit value: ");
        scanf("%f",&f);
     
        //conversion
        c = (f - 32) * 5 / 9;
        
        //Display the result
        printf("Celsius: %.3f", c); 
    }

    Output:

    Enter the Fahrenheit value: 64
    Celsius: 17.778


  • C Program to calculate Simple Interest

    In this tutorial, we will write a program to find the Simple Interest in C. And the formula to calculate the Simple Interest (SI) is:

    SI = ((p * r * t) / 100))
    where,
    p = Principal
    r = Rate of Interest
    t = Time Period

    Explanation:
    The program takes the user input for Principal, Rate, and Time, calculates the result, and displays the result on the screen.


    C Program to calculate the Simple Interest

    //Program to calculate simple interest in C
    
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
      float p, r, t;
      float SI;
      
      printf("Enter the principal: ");
      scanf("%f", &p);
      
      printf("Enter the rate: ");
      scanf("%f", &r);
      
      printf("Enter the time: ");
      scanf("%f", &t);
      
      //calculating
      SI = (p * r * t) / 100;
      
      //Display the result
      printf("\nThe simple interest is: %.3f", SI);
    
      return 0;
    }

    Output:

    Enter the principal: 34000
    Enter the rate: 30
    Enter the time: 5

    The simple interest is: 51000.00

    The above source code to calculate the Simple Interest in C is compiled and ran successfully on a windows system. Browse through C Program to learn more.


  • C Program to Print Hello World

    This is the most basic program where the program prints the hello world. Through this, you can understand the basic structure of the C program.

    This the basic program for beginners in C. Let us go through an example.

    Program to Print “Hello World” in C

    //This is the header for standard I/O
    #include<stdio.h>
    
    //This is the main function
    int main() 
    {
      printf("Hello, World!");
    
      return 0;
    }

    Output:

    Hello, World!

    Run the above program in your C compiler and you will get the above output. You may go through the Program structure in C to understand the programming format better.


  • C Program to Multiply two numbers using Addition operator

    In this tutorial, we will write a C program to multiply using the plus operator (+). You may go through the following topics in C to understand the program better.

    Multiplication of two numbers using plus arithmetic operator:
    To find the result of the product of two numbers, we need to add the first number to the second number of times

    Example:

    Input:
    First number: 5
    Second number: 3
    
    Output:
    Multiplication by addition of 5 and 3 is: 15
    
    Solution:
    Multiplication is = 5 + 5 + 5 = 15

    C Program to Multiply number using Addition operator

    //C program to multiply two numbers using plus operator
     
    #include <stdio.h>
     
    int main()
    {
        int num1, num2;
        int result = 0, i;
         
        printf("Enter the first number: ");
        scanf("%d",&num1);
        printf("Enter the second number: ");
        scanf("%d",&num2);
         
        result=0;
    
        for(i = 1; i <= num2; i++)
            result += num1;
        
    
        printf("\nResult of Multiplication of %d and %d through Addition: %d\n", num1, num2, result);
    
        return 0;
    }

    Output:

    Enter the first number: 5
    Enter the second number: 3

    Result of Multiplication of 5 and 3 through Addition: 15