Essential C Programming Projects: Calculating Digit Sum, Merging Numbers, Reversing Digits, and Computing Gross Salary

Essential C Programming Projects: Calculating Digit Sum, Merging Numbers, Reversing Digits, and Computing Gross Salary
Essential C Programming Projects: Calculating Digit Sum, Merging Numbers, Reversing Digits, and Computing Gross Salary

Question One 

Write a program to calculate the sum of a three digit number e.g. 125 is 8.

Steps to Calculate the Sum of the Digits:

1. Input the Three-Digit Number:
  •    Read the number from the user. Ensure that the input is a three-digit number.
2. Extract Each Digit:
  • Use mathematical operations to extract each digit of the number.
    • The hundreds digit can be found by performing integer division by 100.
    • The tens digit can be found by first performing integer division by 10 and then taking the result modulo 10.
    • The units digit is found by taking the number modulo 10.

3. Sum the Digits:

  • Add the hundreds digit, tens digit, and units digit together.

4. Output the Result:

  • Display the sum of the digits to the user.

Example:

Let's take the number 125 as an example:
  • Hundreds digit: (125 div 100 = 1)
  • Tens digit: ((125 div 10) mod 10 = 12 mod 10 = 2\)
  • Units digit: (125 mod 10 = 5)
  • Sum: (1 + 2 + 5 = 8)

The C Program:

Here's how you could write the program in C:

 #include <stdio.h>

int main()
{
    int x, a, b, c, sum;

    printf("\nPlease enter a 3 digit number: ");
    scanf("%d", &x);

    a = x/100;
    b = (x/10)%10;
    c = x%10;

    sum = a + b + c;
    printf("\nThe sum of the digits %d is %d", x, sum);
}

Running the Program:

When you run this program and input 125, the output will be 8, which is the sum of the digits \(1 + 2 + 5\).

Question Two

Write a program to merge three number, e.g. a = 1, b = 2, c = 8, is 128.

Steps to Merge the Numbers:

1. Input the Numbers:

  • Read three single-digit numbers from the user: `a`, `b`, and `c`.

2. Merge the Numbers:

  • To merge the digits into a single number, you can use the concept of place values:
    • `a` will be in the hundreds place.
    • `b` will be in the tens place.
    • `c` will be in the units place.
  • Multiply `a` by 100, `b` by 10, and add `c` to get the final number.

3. Output the Result:

  • Display the merged number to the user.

Example:

Given `a = 1`, `b = 2`, and `c = 8`, the merged number should be:
  • (1 times 100 + 2 times 10 + 8 = 100 + 20 + 8 = 128)

The C Program:

Here's how you could write the program in C:

#include <stdio.h>

int main()
{
    int a, b, c, sum;
    printf("\nEnter the first single digit number: ");
    scanf("%d", &a);
    printf("\nEnter the second single digit number: ");
    scanf("%d", &b);
    printf("\nEnter the third single digit number: ");
    scanf("%d", &c);

    sum = a*100 + b*10 + c;
    printf("\nMerging %d, %d, and %d together gives %d", a, b, c, sum);
}

Running the Program:

When you run this program and input `a = 1`, `b = 2`, and `c = 8`, the output will be `128`, which is the merged number.

Summary:

  • The key idea is to understand how place values work in numbers.
  • By multiplying `a` by 100, `b` by 10, and adding `c`, you can create a three-digit number from three separate digits.

Question Three

Write a program to print the reverse of a 3 digit number.

Steps to Reverse the Digits of a Number:

1. Input the Three-Digit Number:

  • Read the three-digit number from the user.

2. Extract Each Digit:

  • Use mathematical operations to extract each digit of the number:
    • The hundreds digit can be found by performing integer division by 100.
    • The tens digit can be found by first performing integer division by 10 and then taking the result modulo 10.
    • The units digit is found by taking the number modulo 10.

3. Rearrange the Digits:

  • Reverse the order of the digits by placing the units digit in the hundreds place, the tens digit in the tens place, and the hundreds digit in the units place.

4. Output the Reversed Number:

  • Display the reversed number to the user.

Example:

Let's take the number `341` as an example:
  • Hundreds digit: (341 div 100 = 3)
  • Tens digit: ((341 div 10) mod 10 = 34 mod 10 = 4)
  • Units digit: (341 mod 10 = 1)
  • Reversed Number: The digits in reverse order are `143`.

The C Program:

Here's how you could write the program in C:

#include <stdio.h>

int main()
{
    int x, a, b, c, reverse;

    printf("\nInput three digit numbers: ");
    scanf("%d", &x);

    a = x/100;
    b = (x/10)%10;
    c = x%10;

    reverse = c*100 + b*10 + a;
    printf("\nThe reverse number is %d", reverse);
}

Running the Program:

When you run this program and input `341`, the output will be `143`, which is the reverse of the original number.

Summary:

  • The process involves extracting individual digits from the number and rearranging them in reverse order.
  • By multiplying and adding the digits accordingly, you can construct the reversed number.

Question Four

Compute the gross salary of Mr. Harish. Input his basic salary. His DA is 40% of basic salary, and HRA is 20% of his basic salary.

Explanation:

To compute the gross salary of Mr. Harish, you need to consider his basic salary along with two additional components: Dearness Allowance (DA) and House Rent Allowance (HRA). Both DA and HRA are calculated as percentages of the basic salary. The gross salary is the sum of the basic salary, DA, and HRA.

Steps to Compute the Gross Salary:

1. Input the Basic Salary:

  • Read the basic salary of Mr. Harish from the user.

2. Calculate the DA (Dearness Allowance):

  • DA is 40% of the basic salary.
  • To calculate DA: ( text{DA} = text{Basic Salary} times 0.40 )

3. Calculate the HRA (House Rent Allowance):

  • HRA is 20% of the basic salary.
  • To calculate HRA: ( text{HRA} = text{Basic Salary} times 0.20 )

4. Calculate the Gross Salary:

  • Gross Salary is the sum of the basic salary, DA, and HRA.
  • To calculate Gross Salary: 
     [
     text{Gross Salary} = text{Basic Salary} + text{DA} + text{HRA}
     ]

5. Output the Gross Salary:

  • Display the gross salary of Mr. Harish.

Example:

Suppose Mr. Harish’s basic salary is `₹50,000`.
  • DA: ( 50000 times 0.40 = ₹20,000 )
  • HRA: ( 50000 times 0.20 = ₹10,000 )
  • Gross Salary: ( 50000 + 20000 + 10000 = ₹80,000 )

The C Program:

Here's how you could write the program in C:

#include <stdio.h>

int main()
{
    float basicSalary, da, hra, grossSalary;

    printf("\nInput the basic salary of Mr. Harish (figures only): ");
    scanf("%f", &basicSalary);

    da = basicSalary * 0.40;
    hra = basicSalary * 0.20;
    grossSalary = basicSalary + da + hra;

    printf("\nThe Gross Salary of Mr. Harish is %.2f", grossSalary);
}

Running the Program:

If you input a basic salary of `₹50,000`, the program will output `₹80,000` as the gross salary.

Summary:

  • The gross salary is calculated by adding the basic salary, DA, and HRA.
  • DA is 40% of the basic salary, and HRA is 20% of the basic salary.