
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Finding sum of first and last digit using divide and modulo operator in C language
Problem
What is the C program to obtain the sum of the first and last digit of the number, if a four-digit number is input through the keyboard?
Solution
In this program, we are taking a four-digit number at run time and trying to find the sum of first and last digit of that four-digit number by using the logic −
a=n%10; b=n/1000; result = a + b;
Let’s apply this logic to find sum of first and last digit of four-digit number −
Example
Following is the C program for finding sum of first and last digit by using divide and modulo operator −
#include<stdio.h> main(){ int n,a,b,result; printf("Enter a four digit number: "); scanf("%d",&n); a=n%10; b=n/1000; result = a + b; printf("After adding first and last digit is %d", result); getch(); }
Output
When the above program is executed, it produces the following result −
Enter a four digit number: 2345 After adding first and last digit is 7
Program
Following is the C program for a six-digit number and try to find sum of first and last digit −
#include<stdio.h> main(){ int n,a,b,result; printf("Enter a six digit number: "); scanf("%d",&n); a=n%10; b=n/100000; result = a + b; printf("After adding first and last digit is %d", result); getch(); }
Output
When the above program is executed, it produces the following result −
Enter a six digit number: 346713 After adding first and last digit is 6
- Related Articles
- Find the sum of first and last digit for a number using C language
- Sum of the first and last digit of a number in PL/SQL
- Differentiate the modulo and division by using C Programming language?
- Maximum Sum SubArray using Divide and Conquer in C++
- Program to find remainder without using modulo or % operator in C++
- Finding sum of digits of a number until sum becomes single digit in C++
- Finding number of alphabets, digits and special characters in strings using C language
- Maximum Subarray Sum using Divide and Conquer algorithm in C++
- Finding even and odd numbers in a set of elements dynamically using C language
- Sum of two numbers modulo M in C++
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- What is modulo % operator in Python?
- First and Last Three Bits in C++
- Maximum subarray sum modulo m in C++
- Explain the concept of logical and assignment operator in C language

Advertisements