Add

20+ Most Asked C Coding Questions | 12 usefull Program in C Language

             

Let's see the Top 15  Numbers program in c langauage.

Best Way To Learn C Programming free

Basic For Loop program in c language

Top 10 Basic C Programs For Beginners

 

1.Program to identify a Leap Year.

Program to identify whether the user input year is a leap year or not.

%nd is used to align the text, representing the values within n spaces with remaining leading by blank spaces.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n"); 

    int year;

    printf("Enter the year to check if it is a leap year: ");

    scanf("%d", &year);

    // divisible by 4, 100 and 400

    if(year%400 == 0)

        printf("\n\n%d is a leap year\n", year);

    // divisible by 100 and 4 and not divisible by 400

    else if(year%100 == 0)

        printf("\n\n%d is not a leap year\n", year);

    // divisible only by 4 and not by 100

    else if(year%4 == 0)

    {  

        /*

            %0nd is used to represent the number

            in n digits with leading 0's

        */

        printf("\n\n%07d is a leap year\n", year);

    }

    // not divisible by 4

    else

        printf("\n\n%d is not a leap year\n", year);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

2. Program to find the Largest of Three Numbers.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    float a, b, c;

    printf("Enter 3 numbers:\n\n");

    scanf("%f%f%f", &a, &b, &c);

    if(a >= b && a >= c)

    {

        /*

            %.3f prints the floating number

            upto 3 decimal places

        */

        printf("\n\nLargest number = %.3f ", a);

    }

    else if(b >= a && b >= c)

    {

        printf("\n\nLargest number is = %.3f", b);

    }

    else

    {

        printf("\n\nLargest number is = %.3f", c);

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

3. Program to find the second Largest number among Three user input Numbers.

  • lf restricts the number till 2 decimal places.

Program to find the second largest number out of the three user input numbers using nested if-else loops.

#include<stdio.h>

 

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    double a, b, c;

    printf("Enter 3 numbers:\n");

    scanf("%lf%lf%lf", &a, &b, &c);  //lf is a format specifier to take double as input

    // a is the largest

    if(a >= b && a >= c)

    {

        if(b >= c)

        {

            /*

                .2lf restricts the number till

                2 decimal places

            */

            printf("\n\n%.2lf is the 2nd largest number\n", b);

        }

        else

        {

            printf("\n\n%.2lf is the 2nd largest number\n", c);

        }

    }

    else if(b >= a && b >= c)

    {

        if(a >= c)

        {

            printf("\n\n%.2lf is the 2nd largest number\n",a);

        }

        else

        {

            printf("\n\n%.2lf is the 2nd largest number\n",c);

        }

    }

    // c is the largest number of the three

    else if(a >= b)

    {

        printf("\n\n%.2lf is the 2nd largest number\n", a);

    else

    {

        printf("\n\n%.2lf is the 2nd largest number\n", b);

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

4. Program to Add two user input numbers using Pointers.

  • * operator returns 'value at' on the given address location.
  • & operator returns 'address of' a given value.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    int first, second, *p , *q, sum;

    printf("Enter two integers to add using pointers:\n");

    scanf("%d%d", &first, &second);

    p = &first;

    q = &second;

    sum = *p + *q;

    printf("\n\nThe sum of the entered numbers is: %d", sum);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

5. Program to find the Area and Circumference of a Circle.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    int rad;

    float PI = 3.14, area, ci;

    printf("\nEnter the radius of the circle: ");

    scanf("%d", &rad);

    area = PI*rad*rad;

    printf("\n\n\n Area of the circle is: %f ", area);

    ci = 2*PI*rad;

    printf("\n\n\n Circumference of the circle is: %f", ci);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

6. Program to find the Area of Triangle using Base and Height.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    int h, b;

    float area;

    printf("\n\nEnter the height of the Triangle: ");

    scanf("%d", &h);

    printf("\n\nEnter the base of the Triangle: ");

    scanf("%d", &b);

    /*

        Formula for the area of the triangle = (height x base)/2

        Also, typecasting denominator from int to float

        to get the output in float

    */

    area = (h*b)/(float)2;

    printf("\n\n\nThe area of the triangle is: %f", area);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

7. Program to find the Area of Triangle using Heron's Formula.

#include<stdio.h>

#include<math.h>  // to use sqrt() function

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    double a, b, c, area, s;

    printf("\nEnter the sides of the triangle:\n\n");

    scanf("%lf%lf%lf", &a, &b, &c);   // lf is format specifier for double input

    s = (a+b+c)/2;

    /*

        sqrt is a predefined system function that

        returns the square root of the input value

    */

    area = sqrt(s*(s-a)*(s-b)*(s-c));

    printf("\n\n\n\nThe area of the Triangle calculated using Heron's formula is: %lf", area);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

8. program to show basic Arithmetic Operations and role of Typecasting.

we have 2 simple programs to showcase various basic arithmetic operations and how typecasting is handled by C language with and without explicitly using Typecasting in our program.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    int a, b, add, subtract, multiply;

    float divide;

    printf("Enter two integers: \n");

    scanf("%d%d", &a, &b);

    add = a+b;

    subtract = a-b;

    multiply = a*b;

    divide = a/b;

    printf("\nAddition of the numbers = %d\n", add);

    printf("Subtraction of 2nd number from 1st = %d\n", subtract);

    printf("Multiplication of the numbers = %d\n", multiply);

    printf("Dividing 1st number from 2nd = %f\n", divide);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

8.Program Arithmetic Operations with Typecasting.

#include<stdio.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    int a, b, add, subtract, multiply, remainder;

    float divide;

    printf("Enter two integers: \n");

    scanf("%d%d", &a, &b);

    add = a+b;

    subtract = a-b;

    multiply = a*b;

    divide = a/(float)b;

    remainder = a%b;

    printf("\n\nAddition of the numbers = %d\n", add);

    printf("\nSubtraction of 2nd number from 1st = %d\n", subtract);

    printf("\nMultiplication of the numbers = %d\n", multiply);

    printf("\nDividing 1st number from 2nd = %f\n", divide);

    printf("\nRemainder on Dividing 1st number by 2nd is %d\n", remainder);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

9. Programs to showcase the conversion between various Number Systems.

we have multiple programs to showcase conversion between various Number systems like Binary to Decimal, Octal to Decimal, Decimal to Binary and we will even use recursion to help you understand how recursion can be used in such programs.

  • Program to convert Binary to Decimal Equivalent.

#include<stdio.h>

#include<math.h>

// Function prototype declaration

int binary_decimal(int n);

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    int n;

    char c;

    printf("Enter the binary number: ");

    scanf("%d", &n);

    printf("\n\n\nThe decimal equivalent of %d is  %d\n\n", n, binary_decimal(n)); // function calling

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

// Definition of the function to convert binary to decimal.

int binary_decimal(int n)

{

    int decimal = 0, i = 0, rem;

    while(n != 0)

    {

        rem = n%10;   // gives the digit at the units place

        n = n/10; // gives the number excluding its units digit

        /*

            pow is a system defined function that takes

            two integers as input parameters

        */

        decimal += rem*pow(2, i++);

    }

    /*

        return the decimal equivalent of the input

        binary number to the function call

    */

    return decimal;

}

9.Program to convert Octal to Decimal Equivalent.

  • %ld is the format specifier to input a long value.

#include<stdio.h>

#include<stdio.h>

#include<math.h>

int main()

{

    printf("\n\n\t\tLearnKro - Best place to learn\n\n\n");

    long int octal, val, decimal = 0;

    int i = 0;

    printf("Enter any octal number: ");

    scanf("%ld", &val);

    octal = val;

    while(octal != 0)

    {

        /*

            i++ is post increment, where value is

            first assigned and then incremented

        */

      decimal += (octal % 10)*pow(8, i++);

      octal/=10;    // same as octal=octal/10

    }

    printf("\n\n\nEquivalent decimal value of %ld is %ld\n\n\n", val, decimal);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

9.Program to convert Decimal to Binary Equivalent.

  • Program to convert decimal number to its binary equivalent without recursion.

#include<stdio.h>

int main()

{

    printf("\n\n\t\LearnKro - Best place to learn\n\n\n");

    int n,c,k;

    printf("Enter an integer in decimal number system: ");

    scanf("%d", &n);

    // In 31 bit format

    printf("\n\n\nThe binary equivalent of decimal value %d is:", n);

    for(c = 31; c >= 0; c--)

    {

        k = n>>c;

        /*

            num&1 = returns true if the last digit of num is 1

            else false

        */

        if(k&1)

            printf("1");

        else

            printf("0");

    }

    printf("\n");

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

9.Program to convert Decimal to Binary Equivalent using Recursion.

#include<stdio.h>

//Function prototype declarations

void decimal_binary(int );

void F(int );

void reverse(int );

int main()

{

    printf("\n\n\t\LearnKro - Best place to learn\n\n\n");

    int n;

    printf("\n\nEnter an integer in decimal number system: ");

    scanf("%d", &n);

    //In 31 bit format

    printf("\n\nThe binary equivalent of decimal value %d using decimal_binary method is: ", n);

    decimal_binary(n);  // function call

    printf("\n\nThe binary equivalent of decimal value %d using F() method is: ", n);

    F(n);   // function call

    printf("\n\nThe Reverse of the binary representation of value %d is: ", n);

    reverse(n); // function call

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

} 

// function definition

void decimal_binary(int i)

{

    if(i <= 1)

    printf("%d", i);   // to print in up to down format

    else

    {

        decimal_binary(i/2);

        printf("%d", i%2);

    }

}

 

void F(int j)

{

    if(j/2)

    {

        F(j/2);

    }

    printf("%d", j%2);

}

void reverse(int k)

{

    if(k <= 1)

        printf("%d", k);

    else

    {

        printf("%d", k%2);

        F(k/2);

    }

    printf("\n\n");

}

10. Program to convert Temparature in Celsius to Fahrenheit.

Program for temperature conversion from Celsius to Fahrenheit. All we have to do is use the simple formula in our program, which is, if a temparature value is in Celsius, multiply it with 1.8 or 9/5 and add 32 to the result, this will give the equivalent Fahrenhiet value.

#include<stdio.h>

int main()      

{

    printf("\n\n\t\LearnKro - Best place to learn\n\n\n");

    float celsius, fahrenheit;

    printf("\n\nEnter temperature in Celsius: ");

    scanf("%f", &celsius);

    fahrenheit = (1.8*celsius) + 32;

    printf("\n\n\nTemperature in Fahrenheit is: %f ", fahrenheit);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

11. Program to calculate Simple Interest.

program to calculate the Simple Interest for a given prinicipal amount, rate of interest and time duration.

%7.3f means the float value will be represented in 7 digits including 3 digits after decimal places.

#include<stdio.h>

void main()

{

    printf("\n\n\t\LearnKro - Best place to learn\n\n\n");

    float principal_amt, rate, simple_interest;

    int time;

    printf("Enter the value of principal amount, rate and time\n\n\n");

    scanf("%f%f%d", &principal_amt, &rate, &time);

    // considering rate is in percentage

    simple_interest = (principal_amt*rate*time)/100.0;

    // usually used to align text in form of columns in table

    printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);

    printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);

    printf("\n\n\t\t\tTime= %d years \n", time);

    printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

12. Program to find GCD of N Numbers.

 #include<stdio.h>

int main()

{

    printf("\n\n\t\tLearn Kro - Best place to learn\n\n\n");

    int x, y =- 1;

    printf("Enter numbers. To exit enter 0\n");

    while(1)    // infinite loop to take input

    {

        scanf("%d", &x);

        if(x < 1)

            break;

        else if(y ==- 1)    // only 1 number entered, its GCD is itself

            y = x;

        else if(x < y)

            y = gcd(x, y);

        else

            y = gcd(y, x);

    }

    printf("\n\n\nGCD of all the entered number is: %d", y);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

// GCD of 2 numbers is calculated at a time

int gcd(int a, int b)

{

    int i;

    /*

        a is the smallest of the two numbers

        of which GCD is to be calculated

    */

    for(i = a; i >= 1; i--)

    {

        // Greatest number that divides both the numbers

        if(a%i == 0 && b%i == 0)

            break;  // exits the loop

    }

    return i;

}

12. Program to find GCD of Two Numbers Using Functions.

 #include<stdio.h>

int gcd(int c, int d)   // function definition

{

    if(d == 0)

        return c;

    else

        return gcd(d, c%d);

}

int main()

{

    printf("\n\n\t\t LearnKro - Best place to learn\n\n\n");

    int a, b;

    printf("Enter 2 numbers: \n\n");

    scanf("%d%d", &a, &b);

    printf("\n\nGreatest Common Divisor is: %d", gcd(a, b)); // function calling

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

 

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.