Coding Practice
Showing posts with label Structure Programming Assignment. Show all posts
Showing posts with label Structure Programming Assignment. Show all posts

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.

Write a C program to split a number and then add all factorial digit.

Split a number and then add all factorial digit

3. Take 3 digit number from user. Split the digits of the number using a user defined function splitter(). Find the factorial of each digit using a user defined function find_factorial(). Finally, print the sum of the factorials using anther user defined function calculate_sum().

Sample Output
Input Number: 123
Output : 9


Process returned 0 (0x0)   execution time : 1.911 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int arr[100], factorialSum = 0;
 
int calculate_sum(int n)
{
    factorialSum = factorialSum + n;
}
 
int find_factorial(int f)
{
    int fact = 1;
 
    for(int i = 1; i <= f; i++)
    {
        fact = fact * i;
    }
    calculate_sum(fact);
}
 
int splitter(int number)
{
    int i = 0, digit = 0;
    while(number != 0)
    {
        arr[i++] = number % 10;
        number = number/10;
        digit++;
    }
    return digit;
}
 
int main()
{
    int number, digit;
 
    printf("Input Number: ");
    scanf("%d", &number);
 
    digit = splitter(number);
 
    for(int i = 0; i < digit; i++)
    {
        find_factorial(arr[i]);
    }
 
    printf("Output : %d\n\n", factorialSum);
    return 0;
}
Sample Output
Input Number: 125
Output : 123


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

Write a C Program to check twin string or not

Twin String

2. Two strings s1 and s2 will be called “Twin String” if the following conditions are matched: If one of the strings have p distinct vowels, the other will contain the missing 5-p vowels from the vowel set, v = {a, e, i, o, u}

Both of them will be of same length.

Sample Input

Output

S1: national

S2: umbrella

Twin String

S1: education

S2: notorious

Twin String

S1: bangladesh

S2: pakistan

Not Twin String

Sample Output
S1: national
S2: umbrella

Twin String


Process returned 0 (0x0)   execution time : 23.699 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, k = 0, length1, length2, vowel = 0, a = 0, e = 0, iV = 0, o = 0, u = 0;
    char s1[30], s2[30];
 
    printf("S1: ");
    gets(s1);
 
    printf("S2: ");
    gets(s2);
 
    for(i = 0; s1[i] != '\0'; i++);
    length1 = i;
 
    for(i = 0; s2[i] != '\0'; i++);
    length2 = i;
 
    if(length1 == length2)
    {
        for(i = 0; i < length1; i++)
        {
            if(s1[i] == 'a' && a == 0)
            {
                a = 1;
                vowel++;
            }
            else if(s1[i] == 'e' && e == 0)
            {
                e = 1;
                vowel++;
            }
            else if(s1[i] == 'i' && iV == 0)
            {
                iV = 1;
                vowel++;
            }
            else if(s1[i] == 'o' && o == 0)
            {
                o = 1;
                vowel++;
            }
            else if(s1[i] == 'u' && u == 0)
            {
                u = 1;
                vowel++;
            }
            if(s2[i] == 'a' && a == 0)
            {
                a = 1;
                vowel++;
            }
            else if(s2[i] == 'e' && e == 0)
            {
                e = 1;
                vowel++;
            }
            else if(s2[i] == 'i' && iV == 0)
            {
                iV = 1;
                vowel++;
            }
            else if(s2[i] == 'o' && o == 0)
            {
                o = 1;
                vowel++;
            }
            else if(s2[i] == 'u' && u == 0)
            {
                u = 1;
                vowel++;
            }
        }
    }
 
    if(vowel == 5)
    {
        printf("\nTwin String");
    }
    else
    {
        printf("\nNot Twin String");
    }
    printf("\n\n");
    
    return 0;
}
Sample Output-1
S1: bangladesh
S2: pakistan

Not Twin String


Process returned 0 (0x0)   execution time : 11.287 s
Press any key to continue.
Sample Output-2
S1: abaca
S2: acada

Not Twin String


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

Find how many numbers in a array are greater than the average and then reverse the arrays numbers

1. Take n numbers in an array. Do the following operations
a. Find how many numbers in the array are greater than the average value of the array elements.
b. Reverse the array without using any library function.

Sample Input

Output

Array element: 4

10 20 30 40

Numbers greater than average value: 2

Reversed array: 40 30 20 10

Array element: 5

1 2 3  4 65

Numbers greater than average value: 1

Reversed array: 65 4 3 2 1

Sample Output
Array element: 4
10
20
30
40

Numbers greater than average value: 2
Reversed array: 40 30 20 10


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

int main()
{
    int i, size, arr[50], sum = 0, avg, count = 0;

    printf("Array element: ");
    scanf("%d", &size);

    for(i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
        sum = sum + arr[i];
    }

    avg = sum / size;

    for(i = 0; i < size; i++)
    {
        if(avg > arr[i])
        {
            count++;
        }
    }
    printf("\nNumbers greater than average value: %d\n", count);

    printf("Reversed array: ");
    for(i = size - 1; i >= 0; i--)
    {
        printf("%d ", arr[i]);
    }
    printf("\n\n");

    return 0;
}
Sample Output
Array element: 5
1
2
3
4
65

Numbers greater than average value: 4
Reversed array: 65 4 3 2 1


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

You're given all numbers between 1, 2, 3, 4 … n except one. Your task is to find the missing number

Find the Missing Number

2. You're given all numbers between 1, 2, 3, 4 … n except one. Your task is to find the missing number.

Input:
First input line contains an integer n. The second line contains (n−1) numbers. Every number is distinct and between 1 and n (inclusive).

Output:
Print the missing number.

Sample Input Sample Output
5
2 4 3 1
5
10
2 10 7 9 3 6 4 8 1
5
Sample Output
10
2 10 7 9 3 6 4 8 1
Missing Number is: 5


Process returned 0 (0x0)   execution time : 20.268 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, sum, arrSum = 0, n;
 
    scanf("%d", &n);
    int numbers[n];
 
    for(i = 0; i < n-1; i++)
    {
        scanf("%d", &numbers[i]);
    }
 
    sum = (n * (n+1))/2;
 
    for(i = 0; i < n-1; i++)
    {
        arrSum = arrSum + numbers[i];
    }
    printf("Missing Number is: %d\n\n", sum - arrSum);
 
    return 0;
}
Sample Output
5
2 4 3 1
Missing Number is: 5


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

Write a C program to generate the following pattern

Generate Pattern

4. Write a C program to generate the following pattern

Sample Output
Enter number of rows
9

9       9
 8     8
  7   7
   6 6
    5
   4 4
  3   3
 2     2
1       1

Process returned 0 (0x0)   execution time : 1.879 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, k, rows;
 
    printf("Enter number of rows\n");
    scanf("%d", &rows);
    printf("\n");
 
    for(i = rows, k = 1; i >= 1; i--, k++)
    {
        for(j = rows; j >= 1; j--)
        {
            if(i == j || j == k)
            {
                printf("%d", i);
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}
Sample Output
Enter number of rows
5

5   5
 4 4
  3
 2 2
1   1

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