Problem 20

author
By Supreme2023-03-15
Problem Statement :
Write a program to find out sum of first and last digit of a given number
Code:

#include <stdio.h>

int main(){

//20. Write a program to find out sum of first and last digit of a given number.

int n,lastdig,firstdig,sum=0;

printf("\tSum Of First And Last Digit Of A Given Number");
printf("\n=============================================================");


printf("\n\nEnter The Value Of N : ");
scanf("%d",&n);


lastdig = n%10;


while(n>=10){
n=n/10;
}
firstdig=n;

sum = firstdig + lastdig;

printf("\n______________________________________________________________");
printf("\n\nSum Of Last & First Digit Of Given Number Is : %d",sum);
printf("\n______________________________________________________________\n\n");

return 0;
}

Description :

This C program:

  • Calculates the sum of the first and last digits of a given number
  • Declares variables to hold the number, the last digit, the first digit, and the sum
  • Prints a header and prompts the user to enter the number
  • Uses the modulo operator to find the last digit
  • Uses a loop to find the first digit
  • Calculates the sum of the first and last digits
  • Prints the sum to the console with a footer
  • Returns 0 to indicate successful execution
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.