
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Largest number less than N with digit sum greater than the digit sum of N in C++
In this tutorial, we are going to write a program that finds the number less than N with digit sum greater than the digit sum of n.
Let's see the steps to solve the problem.
- Write a function to find the digits sum.
- Initialise n.
- Write a loop that iterates from n - 1 to 1.
- Check the digits sum of current number with the digits sum of n.
- If the digits sum of current number is greater than n, then return the current number.
- Move to the next number.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int sumOfDigits(int n) { int digitsSum = 0; while (n > 0) { digitsSum += n % 10; n /= 10; } return digitsSum; } int findLargestNumber(int n) { int i = n - 1; while (i > 0) { if (sumOfDigits(i) > sumOfDigits(n)) { return i; } i--; } return -1; } int main() { int n = 75; cout << findLargestNumber(n) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
69
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Largest even digit number not greater than N in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Count numbers with difference between number and its digit sum greater than specific value in C++
- Sum of two elements just less than n in JavaScript\n
- Count pairs with sum as a prime number and less than n in C++
- I am a five digit number. My ones digits is 3 . My hundreds digit is 2 times my ones digit. My tens digit is the sum of ones digit and hundreds digit. My thousands and ten thousands digit is one less than hundreds digit.(a) What number am I?(b) Write my successor.(c) Am I greater than or less than the number, fifty five thousand nine hundred thirty ( operatorname{six} " ? )"
- Find the sum of the largest 5 -digit number and the smallest 6 -digit number.
- Python – Average of digit greater than K
- Count the number of words having sum of ASCII values less than and greater than k in C++
- Largest subarray having sum greater than k in C++
- Check if frequency of each digit is less than the digit in Python
- The sum of the digit of a 2 digit number is 6 . On reversing it's digits, the number is 18 less than the original number find the number
- JavaScript - Find the smallest n digit number or greater
- Count of Binary Digit numbers smaller than N in C++
- Product of N with its largest odd digit in C

Advertisements