
- 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
Print a number containing K digits with digital root D in C++
In this problem, we are given two numbers K and D. Our task is to print a number of k digits and which has digital root equal to D.
Digital Root is a single-digit value which is the result of the recursive addition of the digits of the number till the one a single-digit number is reached. Also known as a digital sum.
Let’s take an example to understand the problem,
Input: D = 5 , K = 6 Output: 60000
To solve this problem, we will be using trials of zero’s after the number D. Our number will be {D000..(k-1 times)}. This is a simple and elegant solution to our problem which is also less complex.
Example
Program to show the implementation of our solution,
#include <bits/stdc++.h> using namespace std; void printKdigitNumber(int k, int d) { if (d == 0 && k != 1) cout << "-1"; else { cout << d; k--; while (k--) cout << "0"; } } int main() { int K=6, D=5; cout<<K<<" digit number with digital Root = "<<D<<" is : "; printKdigitNumber(K, D); return 0; }
Output
6 digit number with digital Root = 5 is : 500000
- Related Articles
- C++ code to count number of lucky numbers with k digits
- Find Nth positive number whose digital root is X in C++
- C++ Program to find Numbers in a Range with Given Digital Root
- Print first k digits of 1/n where n is a positive integer in C Program
- Digital root sort algorithm JavaScript
- C program to print digital clock with current time
- Remove K Digits in C++
- Print a number strictly less than a given number such that all its digits are distinct in C++
- Find smallest number with given number of digits and sum of digits in C++
- Digital Root (repeated digital sum) of the given large integer in C++ Program
- Largest number with prime digits in C++
- Returning number with increasing digits. in JavaScript
- Print prime numbers with prime sum of digits in an array
- Find the Largest number with given number of digits and sum of digits in C++
- Print all the paths from root, with a specified sum in Binary tree in C++

Advertisements