Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, July 12, 2022

C Program to Sum of First and Last Digits of a Four-Digit number for SEE and NEB


C Program to Sum of First and Last Digits of a Four-Digit number



In this article, we'll look at how to write a C program that computes the sum of a four-digit number's first and last digits. We will provide a detailed explanation of the algorithm and its implementation, as well as sample code. You will have a clear understanding of how to solve this problem using C programming by the end of this article.


Introduction:

A common programming problem is calculating the sum of the first and last digits of a four-digit number. This problem requires determining the sum of the first and last digits of a given number. You can improve your programming skills and gain a better understanding of C programming concepts by solving this problem.


Understanding the problem:

Before we get into the implementation, let's take a closer look at the problem statement. We're given a four-digit number and asked to calculate the sum of its first and last digits. If the input number is 1234, the program should return 5 (1 + 4).


Steps to solve the problem:


  • Accept the four-digit number from the user.Extract the first digit of the number.
  • Extract the last digit of the number.
  • Calculate the sum of the first and last digits.
  • Display the sum as the output.


Program coding:


#include<stdio.h>
int main()
{
//program to sum of first and fourth digit of input four digit number
int a,b,s;
long int x;
printf("Enter four digits number");
scanf("%ld",&x);
a=x%10;
b=x/1000;
s=a+b;
printf("\n sum =%d",s);
}


Explanation of the program:

This C program calculates the sum of the first and fourth digits of a four-digit number entered by the user. It starts by accepting the input number, storing it in the variable x. The last digit of the number is extracted using x % 10, and the first digit is obtained by dividing the number by 1000 (x / 1000). The sum of the first and fourth digits is calculated and stored in s. Finally, the program displays the sum using printf. It's important to note that this program assumes the user will provide a valid four-digit number.


Output:


Conclusion:

Finally, this article demonstrated how to write a C program to calculate the sum of the first and last digits of a four-digit number. We discussed the problem statement and devised a solution algorithm. The program computes the sum of the first and last digits using simple arithmetic operations. We also provided a sample implementation and showed how to run the program with a sample input. Readers can improve their C programming skills and gain a deeper understanding of fundamental concepts by understanding and implementing this program.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages