C Programming

Solved Example

Practice Questions

Exercise Question

Find the sum of first and third digit in a 3 digit number

Objective

Input 3 digit numbers and find the sum of first and 3rd digit of a number. For example, if the number that is input is 523 then the sum of first and third digit is (5 + 3 = ) 8, where 3 is the first digit and 5 is the 3rd digit.

Program / Solution

#include<stdio.h>
int main() {
	int n;
	int first_dig,third_dig,sum;
	printf("Enter a 3 digit number: ");
	scanf("%d",&n);
	
	first_dig = n % 10;
	third_dig = (n / 100) % 10;
	
	sum = first_dig + third_dig;
	
	printf("Sum = %d",sum);
	
	return 0;
}

Sample Output:

Enter a 3 digit number: 279
Sum = 11