Description
To find sum of first and last digit of a number first, find first and last digit. 1. To find last digit find modulus of number. When modulo divided by 10 returns its last digit. 2. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
C/C++
/* C Program to find sum of first and last digit of a number */ //Save it as SumOfFirstAndLastDigit.c #include<stdio.h> int main(){ int num; printf("Enter atleast two digit number : "); scanf("%d",&num); int lastDigit = findLastDigit(num); int firstDigit = findFirstDigit(num); int sum = firstDigit + lastDigit; printf("The first digit = %d and last digit = %d", firstDigit, lastDigit); printf("\nThe sum of first digit and last digit of num is %d", sum); } int findLastDigit(int num){ return (num % 10); } int findFirstDigit(int num){ while(num >= 10){ num /= 10; } return num; }
Input: Enter atleast two digit number : 157 Output: The first digit = 1 and last digit = 7 The sum of first digit and last digit of 157 is 8
Java
/* Java Program to find sum of first and last digit of a number */ //Save it as SumOfFirstAndLastDigit.java import java.io.*; import java.util.Scanner; public class SumOfFirstAndLastDigit { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter atleast two digit number : "); int num = scanner.nextInt(); int lastDigit = findLastDigit(num); int firstDigit = findFirstDigit(num); int sum = firstDigit + lastDigit; System.out.println("The first digit = " + firstDigit+ " and last digit = " + lastDigit); System.out.println("The sum of first digit and last digit of "+ num+ " is "+ sum); } private static int findLastDigit(int num) { return (num%10); } private static int findFirstDigit(int num) { while(num >= 10) { num /= 10; } return num; } }
Input: Enter atleast two digit number : 157 Output: The first digit = 1 and last digit = 7 The sum of first digit and last digit of 157 is 8
Related Programs
1) Program to find sum of digits of a number2) Program to find largest and smallest digit in a Number
3) Program to calculate Permutation And Combination
4) Program to reverse a number
5) Program to find largest and smallest character in a String
6) Program to calculate Volume and Surface Area of Sphere
7) Program To Check Armstrong Number
8) Program to Check Whether a Number is Prime or Not
9) Check whether a given number is a perfect number or not
10) Program to Check Leap Year