Coding Practice

Write a C program to convert IP address to 32-bit binary format

Convert IP Address to 32-bit Binary Format

1. Write a C program that takes a classless IP address as input and displays it in binary, the number of network and host address depending on number represented after the backslash

Classless is called Classless Inter-Domain Routing. It was introduced in 1993 to replace classfull addressing and it allows the user to use Variable Length Subnet Makss(VLSM). Class-less subnet masks are denoted by /X. X is the subnet mask. For example 192.168.0.1/24.
[Note: Input must be like this XXX.XXX.XXX.XXX/XX
Example: 192.168.0.1/24]
Network Address: (224-2)
Host Address: (28-2)

Sample Output
Enter IPv4: 192.168.0.1/24
IP in binary: 11000000.10101000.00000000.00000001
Network address: (2^24)-2 = 16777214
Host Address: (2^8)-2 = 254


Process returned 29 (0x1D)   execution time : 19.943 s
Press any key to continue.
Source Code
#include<stdio.h>
#include<math.h>

void decToBin(int number)
{
    int i, bin[8];

    for(i = 0; number > 0; i++)
    {
        bin[i] = number % 2;
        number = number/2;
    }
    for(i = i-1; i >= 0; i--)
    {
        number = (10 * number) + bin[i];
    }
    printf("%0.8d", number);
}

void main()
{
    int i, count = 0, ipv4Len, number = 0, digit, netMask = 0, inputLen;
    long netAddress, hostAddress;
    char ip[30];

    printf("Enter IPv4: ");
    scanf("%s", ip);

    for(i = 0; ip[i] != 47; i++);
    ipv4Len = i;
    for(i = ipv4Len+1; ip[i] != '\0'; i++);
    inputLen = i;

    printf("IP in binary: ");
    for(i = 0; i < ipv4Len; i++)
    {
        if(ip[i] == '.')
        {
            decToBin(number);
            number = 0;
            printf(".");
        }
        else
        {
            digit = ip[i] - '0';
            number = (10 * number) + digit;
        }
    }
    decToBin(number);

    for(i = ipv4Len+1; i < inputLen; i++)
    {
        //ch = '8' = 56, '0' = 48, digit = ch - '0'
        digit = ip[i] - '0';
        netMask = (10 * netMask) + digit;
    }
    netAddress = pow(2, netMask);
    hostAddress = pow(2, (32-netMask));

    printf("\nNetwork address: (2^%d)-2 = %ld\n", netMask, (netAddress-2));
    printf("Host Address: (2^%d)-2 = %ld\n\n", (32-netMask), (hostAddress-2));
}
Sample Output
Enter IPv4: 103.237.76.98/16
IP in binary: 01100111.11101101.01001100.01100010
Network address: (2^16)-2 = 65534
Host Address: (2^16)-2 = 65534


Process returned 32 (0x20)   execution time : 43.430 s
Press any key to continue.

Write a C program to input electricity unit charges and calculate total electricity bill

Input electricity unit charges and calculate total electricity bill

Calculate total electricity bill according to the given condition:

For first 50 units Tk. 1.50/unit
For next 100 units Tk. 2.25/unit
For next 100 units Tk. 3.00/unit
For unit above 250 Tk. 3.75/unit

An additional surcharge of 20% is added to the bill

Sample Output
Enter unit: 111

Total cost: 399.60


Process returned 0 (0x0)   execution time : 1.793 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    float unit, surcharge, taka;
 
    printf("Enter unit: ");
    scanf("%f", &unit);
 
    if(unit >=1 && unit <= 50)
    {
        //For first 50 units Tk. 1.50/unit
        taka = unit * 1.5;
        surcharge = taka * 0.2; //Surcharge 20%
        taka = taka + surcharge;
    }
    else if(unit >=51 && unit <=100)
    {
        //For next 100 units Tk. 2.25/unit
        taka = unit * 2.25;
        surcharge = taka * 0.2; //Surcharge 20%
        taka = taka + surcharge;
    }
    else if(unit >=101 && unit <= 250)
    {
        //For next 100 units Tk. 3.00/unit
        taka = unit * 3.0;
        surcharge = taka * 0.2; //Surcharge 20%
        taka = taka + surcharge;
    }
    else if(unit >=251)
    {
        //For unit above 250 Tk. 3.75/unit
        taka = unit * 3.75;
        surcharge = taka * 0.2; //Surcharge 20%
        taka = taka + surcharge;
    }
    printf("\nTotal cost: %0.2f\n\n", taka);
    return 0;
}
Sample Output
Enter unit: 500

Total cost: 2250.00


Process returned 0 (0x0)   execution time : 3.293 s
Press any key to continue.

Write a C program to input basic salary of an employee and calculate its gross salary

Input basic salary of an employee and calculate its Gross salary

Calculate salary according to following:

Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%

Sample Output
Enter employee basic salary: 25000

Gross Salary: 56250.00

Process returned 0 (0x0)   execution time : 6.148 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    float basicSalary, grossSalary, hra, da;
 
    printf("Enter employee basic salary: ");
    scanf("%f", &basicSalary);
 
    if(basicSalary <= 10000)
    {
        hra = basicSalary * 0.2;
        da = basicSalary * 0.8;
        grossSalary = basicSalary + hra + da;
    }
    else if(basicSalary <= 20000)
    {
        hra = basicSalary * 0.25;
        da = basicSalary * 0.9;
        grossSalary = basicSalary + hra + da;
    }
    else if (basicSalary > 20000)
    {
        hra = basicSalary * 0.3;
        da = basicSalary * 0.95;
        grossSalary = basicSalary + hra + da;
    }
    printf("\nGross Salary: %0.2f\n", grossSalary);
 
    return 0;
}
Sample Output
Enter employee basic salary: 25000

Gross Salary: 56250.00

Process returned 0 (0x0)   execution time : 2.081 s
Press any key to continue.

Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer

Input marks of five subjects and calculate the grade

Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:

    Percentage >= 80% : Grade A+
    Percentage >= 75% : Grade A
    Percentage >= 70% : Grade A-
    Percentage >= 65% : Grade B+
    Percentage >= 60% : Grade B
    Percentage >= 55% : Grade B-
    Percentage >= 50% : Grade C+
    Percentage >= 45% : Grade C
    Percentage >= 40% : Grade D
    Percentage < 40% : Grade F

Sample Output
Physics: 83

Chemistry: 89

Biology: 71

Mathematics: 85

Computer: 82

Grade A+

Process returned 0 (0x0)   execution time : 15.385 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int phy, che, bio, mat, c, marks;
 
    printf("Physics: ");
    scanf("%d", &phy);
 
    printf("\nChemistry: ");
    scanf("%d", &che);
 
    printf("\nBiology: ");
    scanf("%d", &bio);
 
    printf("\nMathematics: ");
    scanf("%d", &mat);
 
    printf("\nComputer: ");
    scanf("%d", &c);
 
    marks = (phy + che + bio + mat + c) / 5;
 
    if(marks >= 80 && marks <= 100)
    {
        printf("\nGrade A+\n");
    }
    else if(marks >= 75 && marks <= 79)
    {
        printf("\nGrade A\n");
    }
    else if(marks >= 70 && marks <= 74)
    {
        printf("\nGrade A-\n");
    }
    else if(marks >= 65 && marks <= 69)
    {
        printf("\nGrade B+\n");
    }
    else if(marks >= 60 && marks <= 64)
    {
        printf("\nGrade B\n");
    }
    else if(marks >= 55 && marks <= 59)
    {
        printf("\nGrade B-\n");
    }
    else if(marks >= 50 && marks <= 54)
    {
        printf("\nGrade C+\n");
    }
    else if(marks >= 45 && marks <= 49)
    {
        printf("\nGrade C\n");
    }
    else if(marks >= 40 && marks <= 44)
    {
        printf("\nGrade D\n");
    }
    else if(marks < 40)
    {
        printf("\nFail!\n");
    }
    else
    {
        printf("\nInvalid marks!\n");
    }
 
    return 0;
}
Sample Output
Physics: 78

Chemistry: 67

Biology: 80

Mathematics: 75

Computer: 77

Grade A

Process returned 0 (0x0)   execution time : 12.276 s
Press any key to continue.

Write a C program to calculate profit or loss

Calculate Profit or Loss
Sample Output
Purchase Price: 143

Selling Price: 150

Profit!

Process returned 0 (0x0)   execution time : 8.603 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int buy, sell;
 
    printf("Purchase Price: ");
    scanf("%d", &buy);
    printf("\nSelling Price: ");
    scanf("%d", &sell);
 
    if(buy < sell)
    {
        printf("\nProfit!\n");
    }
    else if(sell == buy)
    {
        printf("\nNo Loss No Profit.\n");
    }
    else
    {
        printf("\nLoss!\n");
    }
}
Sample Output
Purchase Price: 150

Selling Price: 140

Loss!

Process returned 0 (0x0)   execution time : 12.151 s
Press any key to continue.

Write a C program to find all roots of a quadratic equation

Find All Roots of a Quadratic Equation
Sample Output
Value of a: 1

Value of b: -5

Value of c: 6

x1 = 3.000000
x2 = 2.000000


Process returned 0 (0x0)   execution time : 7.980 s
Press any key to continue.
Source Code
#include<stdio.h>
#include<math.h>
 
int main()
{
    int a, b, c;
    float x1, x2, root;
 
    printf("Value of a: ");
    scanf("%d", &a);
    printf("\nValue of b: ");
    scanf("%d", &b);
    printf("\nValue of c: ");
    scanf("%d", &c);
 
    root = sqrt(pow(b, 2) - (4 * a * c));
 
    x1 = (- b + root) / (2 * a);
    x2 = (- b - root) / (2 * a);
 
    printf("\nx1 = %f\nx2 = %f\n\n", x1, x2);
 
    return 0;
}
Sample Output
Value of a: 1

Value of b: -5

Value of c: 6

x1 = 3.000000
x2 = 2.000000


Process returned 0 (0x0)   execution time : 7.980 s
Press any key to continue.

Write a C program to check whether the triangle is equilateral, isosceles or scalene triangle

Check Equilateral, Isosceles or Scalene Triangle
Sample Output
Enter three sides...
4
4
5

The triangle is isosceles.

Process returned 0 (0x0)   execution time : 4.104 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int side1, side2, side3;

    printf("Enter three sides...\n");
    scanf("%d %d %d", &side1, &side2, &side3);

    if(side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)
    {
        if(side1 == side2 && side2 == side3)
        {
            printf("\nThe triangle is equilateral.\n");
        }
        else if(side1 == side2 || side2 == side3 || side1 ==side3)
        {
            printf("\nThe triangle is isosceles.\n");
        }
        else
        {
            printf("\nThe triangle is scalene.\n");
        }
    }
    else
    {
        printf("\nThe triangle is not valid.\n");
    }
    return 0;
}
Sample Output
Enter three sides...
5
6
7

The triangle is scalene.

Process returned 0 (0x0)   execution time : 2.203 s
Press any key to continue.

Write a C program to input all sides of a triangle and check whether triangle is valid or not

Valid Triangle Check by Sides
Sample Output
Enter three sides...
6
5
7
Triangle is valid.

Process returned 0 (0x0)   execution time : 10.850 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int side1, side2, side3;
 
    printf("Enter three sides...\n");
    scanf("%d %d %d", &side1, &side2, &side3);
 
    if(side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)
    {
        printf("Triangle is valid.\n");
    }
    else
    {
        printf("Triangle is not valid.\n");
    }
    return 0;
}
Sample Output
Enter three sides...
2
3
5
Triangle is not valid.

Process returned 0 (0x0)   execution time : 3.128 s
Press any key to continue.

Write a C program to input angles of a triangle and check whether triangle is valid or not

Valid Triangle Check by Angles
Sample Output
Enter three angles...
40
50
60
Triangle is not valid.

Process returned 0 (0x0)   execution time : 7.433 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int angle1, angle2, angle3;
 
    printf("Enter three angles...\n");
    scanf("%d %d %d", &angle1, &angle2, &angle3);
 
    if(angle1 + angle2 + angle3 == 180)
    {
        printf("Triangle is valid.\n");
    }
    else
    {
        printf("Triangle is not valid.\n");
    }
    return 0;
}
Sample Output
Enter three angles...
45
45
90
Triangle is valid.

Process returned 0 (0x0)   execution time : 3.617 s
Press any key to continue.

Write a C program to count total number of notes in given amount

Count total number of notes in given amount
Sample Output
Count total number of notes in given amount.


Enter your amount: 999
Note of 1000: 0
Note of 500: 1
Note of 100: 4
Note of 50: 1
Note of 20: 2
Note of 10: 0
Note of 5: 1
Note of 2: 2
Note of 1: 0

Process returned 13 (0xD)   execution time : 3.449 s
Press any key to continue.
Source Code
#include<stdio.h>

void main()
{
    int amount, note1000, note500, note100, note50, note20, note10, note5, note2, note1;
    
    printf("Count total number of notes in given amount.\n\n");
    
    scanf("%d", &amount, printf("\nEnter your amount: "));

    note1000 = amount / 1000;
    amount = amount % 1000;
    printf("Note of 1000: %d\n", note1000);

    note500 = amount / 500;
    amount = amount % 500;
    printf("Note of 500: %d\n", note500);

    note100 = amount / 100;
    amount = amount % 100;
    printf("Note of 100: %d\n", note100);

    note50 = amount / 50;
    amount = amount % 50;
    printf("Note of 50: %d\n", note50);

    note20 = amount / 20;
    amount = amount % 20;
     printf("Note of 20: %d\n", note20);
     
    note10 = amount / 10;
    amount = amount % 10;
    printf("Note of 10: %d\n", note10);

    note5 = amount / 5;
    amount = amount % 5;
    printf("Note of 5: %d\n", note5);

    note2 = amount / 2;
    printf("Note of 2: %d\n", note2);

    note1 = amount % 2;
    printf("Note of 1: %d\n", note1);
}
Sample Output
Count total number of notes in given amount.


Enter your amount: 5693
Note of 1000: 5
Note of 500: 1
Note of 100: 1
Note of 50: 1
Note of 20: 2
Note of 10: 0
Note of 5: 0
Note of 2: 1
Note of 1: 1

Process returned 13 (0xD)   execution time : 2.904 s
Press any key to continue.

Write a C program to input month number and print number of days in that month

Input month number and print number of days in that month
Sample Output
Enter month number(1 - 12): 2

Month number 2

2 for February.  (28 or 29 days)

Process returned 0 (0x0)   execution time : 5.216 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    char num;
 
    printf("Enter month number(1 - 12): ");
    scanf("%d", &num);
 
    if(num == 1)
    {
        printf("\nMonth number %d\n\n%d for January. (31 days)\n", num, num);
    }
    else if(num == 2)
    {
        printf("\nMonth number %d\n\n%d for February.  (28 or 29 days)\n", num, num);
    }
    else if(num == 3)
    {
        printf("\nMonth number %d\n\n%d for March. (31 days)\n", num, num);
    }
    else if(num == 4)
    {
        printf("\nMonth number %d\n\n%d for April. (30 days)\n", num, num);
    }
    else if(num == 5)
    {
        printf("\nMonth number %d\n\n%d for May. (31 days)\n", num, num);
    }
    else if(num == 6)
    {
        printf("\nMonth number %d\n\n%d for June.  (30 days)\n", num, num);
    }
    else if(num == 7)
    {
        printf("\nMonth number %d\n\n%d for July. (31 days)\n", num, num);
    }
    else if(num == 8)
    {
        printf("\nMonth number %d\n\n%d for August. (31 days)\n", num, num);
    }
    else if(num == 9)
    {
        printf("\nMonth number %d\n\n%d for September. (30 days)\n", num, num);
    }
    else if(num == 10)
    {
        printf("\nMonth number %d\n\n%d for October. (31 days)\n", num, num);
    }
    else if(num == 11)
    {
        printf("\nMonth number %d\n\n%d for November. (30 days)\n", num, num);
    }
    else if(num == 12)
    {
        printf("\nMonth number %d\n\n%d for December. (31 days)\n", num, num);
    }
    else
    {
        printf("\nInvalid Month number!\n");
    }
 
    return 0;
}
Sample Output
Enter month number(1 - 12): 3

Month number 3

3 for March. (31 days)

Process returned 0 (0x0)   execution time : 2.333 s
Press any key to continue.

Write a C program to input week number and print week day

Input week number and print week day
Sample Output
Enter week number(1 - 7): 1

Week number 1

1 for Saturday.

Process returned 0 (0x0)   execution time : 2.988 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    char num;
 
    printf("Enter week number(1 - 7): ");
    scanf("%d", &num);
 
	// Start From Satureday = 1
    if(num == 1)
    {
        printf("\nWeek number %d\n\n%d for Saturday.\n", num, num);
    }
    else if(num == 2)
    {
        printf("\nWeek number %d\n\n%d for Sunday.\n", num, num);
    }
    else if(num == 3)
    {
        printf("\nWeek number %d\n\n%d for Monday.\n", num, num);
    }
    else if(num == 4)
    {
        printf("\nWeek number %d\n\n%d for Tuesday.\n", num, num);
    }
    else if(num == 5)
    {
        printf("\nWeek number %d\n\n%d for Wednesday.\n", num, num);
    }
    else if(num == 6)
    {
        printf("\nWeek number %d\n\n%d for Thursday.\n", num, num);
    }
    else if(num == 7)
    {
        printf("\nWeek number %d\n\n%d for Friday.\n", num, num);
    }
    else
    {
        printf("\nInvalid week number!\n");
    }
 
    return 0;
}
Sample Output
Enter week number(1 - 7): 3

Week number 3

3 for Monday.

Process returned 0 (0x0)   execution time : 1.441 s
Press any key to continue.
Change Theme
X