Posted on by Kalkicode
Code Mathematics

# Find two numbers whose sum is N and does not contain any digit as K

The statement "Find two numbers whose sum is N and does not contain any digit as K" means that you need to find two whole numbers that add up to a given number N, and neither of the numbers can contain the digit K.

For example, if N is 15 and K is 5, you would need to find two numbers that add up to 15 and do not contain the digit 5. One possible solution would be 8 and 7, since 8 does not contain the digit 5 and 7 does not contain the digit 5 either.

Note that when finding these numbers, it is important to consider all possible numbers that can be formed using the digits that are not equal to K. Additionally, it is important to ensure that both numbers are whole numbers and that they add up to N.

## Program Soltution

``````// C Program
// Find two numbers whose sum is N and does not contain any digit as K
#include <stdio.h>

int checkDigitK(int number, int k)
{
int temp = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return 1;
}
temp = temp / 10;
}
return 0;
}
void constructWithoutK(int n, int k)
{
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
int temp = n;
int extra = 0;
int count = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = temp / 10;
count = count *10;
}
printf("\n Given sum : %d ", n);
printf("\n Given k   : %d ", k);
if (extra == 0 && !checkDigitK(n, k))
{
printf("\n %d (%d + %d) \n", n, 0, n);
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!checkDigitK(temp, k) &&
!checkDigitK(extra, k))
{
printf("\n %d (%d + %d) \n", n, temp, extra);
return;
}
extra++;
temp--;
}
printf("\n number :  Without digit %d value %d not possible \n", n, k);
}
int main()
{
// Test
constructWithoutK(111, 1);
constructWithoutK(1, 1);
constructWithoutK(123, 2);
constructWithoutK(7331, 7);
constructWithoutK(333, 3);
return 0;
}``````

#### Output

`````` Given sum : 111
Given k   : 1
111 (89 + 22)

Given sum : 1
Given k   : 1
number :  Without digit 1 value 1 not possible

Given sum : 123
Given k   : 2
123 (113 + 10)

Given sum : 7331
Given k   : 7
7331 (6331 + 1000)

Given sum : 333
Given k   : 3
333 (222 + 111)``````
``````// Java program for
// Find two numbers whose sum is N and does not contain any digit as K
public class Construction
{
public boolean checkDigitK(int number, int k)
{
int temp = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return true;
}
temp = temp / 10;
}
return false;
}
public void constructWithoutK(int n, int k)
{
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
int temp = n;
int extra = 0;
int count = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = temp / 10;
count = count * 10;
}
System.out.print("\n Given sum : " + n);
System.out.print("\n Given k : " + k);
if (extra == 0 && !checkDigitK(n, k))
{
System.out.print("\n " + n + " (" + 0 + " + " + n + ") \n");
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!checkDigitK(temp, k) && !checkDigitK(extra, k))
{
System.out.print("\n " + n + " (" + temp + " + " + extra + ") \n");
return;
}
extra++;
temp--;
}
System.out.print("\n number : Without digit " + n + " value " + k + " not possible \n");
}
public static void main(String[] args)
{
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}
}``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find two numbers whose sum is N and
// does not contain any digit as K
class Construction
{
public: bool checkDigitK(int number, int k)
{
int temp = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return true;
}
temp = temp / 10;
}
return false;
}
void constructWithoutK(int n, int k)
{
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
int temp = n;
int extra = 0;
int count = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = temp / 10;
count = count *10;
}
cout << "\n Given sum : " << n;
cout << "\n Given k : " << k;
if (extra == 0 && !this->checkDigitK(n, k))
{
cout << "\n " << n << " (" << 0 << " + " << n << ") \n";
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!this->checkDigitK(temp, k) && !this->checkDigitK(extra, k))
{
cout << "\n " << n << " ("
<< temp << " + " << extra << ") \n";
return;
}
extra++;
temp--;
}
cout << "\n number : Without digit "
<< n << " value " << k << " not possible \n";
}
};
int main()
{
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
return 0;
}``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````// Include namespace system
using System;
// Csharp program for
// Find two numbers whose sum is N
// and does not contain any digit as K
public class Construction
{
public Boolean checkDigitK(int number, int k)
{
int temp = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return true;
}
temp = temp / 10;
}
return false;
}
public void constructWithoutK(int n, int k)
{
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
int temp = n;
int extra = 0;
int count = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = temp / 10;
count = count * 10;
}
Console.Write("\n Given sum : " + n);
Console.Write("\n Given k : " + k);
if (extra == 0 && !this.checkDigitK(n, k))
{
Console.Write("\n " + n + " (" + 0 + " + " + n + ") \n");
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!this.checkDigitK(temp, k) && !this.checkDigitK(extra, k))
{
Console.Write("\n " + n + " (" + temp + " + " + extra + ") \n");
return;
}
extra++;
temp--;
}
Console.Write("\n number : Without digit " + n + " value " + k + " not possible \n");
}
public static void Main(String[] args)
{
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}
}``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````package main
import "fmt"
// Go program for
// Find two numbers whose sum is N
// and does not contain any digit as K
type Construction struct {}
func getConstruction() * Construction {
var me *Construction = &Construction {}
return me
}
func(this Construction) checkDigitK(number, k int) bool {
var temp int = number
for (temp != 0) {
if temp % 10 == k {
// When number contains k digit
return true
}
temp = temp / 10
}
return false
}
func(this Construction) constructWithoutK(n, k int) {
if n <= 0 || (k < 0 || k > 9) {
return
}
var temp int = n
var extra int = 0
var count int = 1
for (temp != 0) {
if temp % 10 == k {
extra += count
}
temp = temp / 10
count = count * 10
}
fmt.Print("\n Given sum : ", n)
fmt.Print("\n Given k : ", k)
if extra == 0 && !this.checkDigitK(n, k) {
fmt.Print("\n ", n, " (", 0, " + ", n, ") \n")
return
}
temp = n - extra
if extra > temp {
// Swap value
count = extra
extra = temp
temp = count
}
for (extra < temp) {
if !this.checkDigitK(temp, k) &&
!this.checkDigitK(extra, k) {
fmt.Print("\n ", n, " (", temp, " + ", extra, ") \n")
return
}
extra++
temp--
}
fmt.Print("\n number : Without digit ", n,
" value ", k, " not possible \n")
}
func main() {
var task * Construction = getConstruction()
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````<?php
// Php program for
// Find two numbers whose sum is N
// and does not contain any digit as K
class Construction
{
public	function checkDigitK(\$number, \$k)
{
\$temp = \$number;
while (\$temp != 0)
{
if (\$temp % 10 == \$k)
{
// When number contains k digit
return true;
}
\$temp = (int)(\$temp / 10);
}
return false;
}
public	function constructWithoutK(\$n, \$k)
{
if (\$n <= 0 || (\$k < 0 || \$k > 9))
{
return;
}
\$temp = \$n;
\$extra = 0;
\$count = 1;
while (\$temp != 0)
{
if (\$temp % 10 == \$k)
{
\$extra += \$count;
}
\$temp = (int)(\$temp / 10);
\$count = \$count * 10;
}
echo("\n Given sum : ".\$n);
echo("\n Given k : ".\$k);
if (\$extra == 0 && !\$this->checkDigitK(\$n, \$k))
{
echo("\n ".\$n." (0 ".\$n.") \n");
return;
}
\$temp = \$n - \$extra;
if (\$extra > \$temp)
{
// Swap value
\$count = \$extra;
\$extra = \$temp;
\$temp = \$count;
}
while (\$extra < \$temp)
{
if (!\$this->checkDigitK(\$temp, \$k) &&
!\$this->checkDigitK(\$extra, \$k))
{
echo("\n ".\$n." (".\$temp." + ".\$extra.") \n");
return;
}
\$extra++;
\$temp--;
}
echo("\n number : Without digit ".\$n." value ".\$k." not possible \n");
}
}

function main()
{
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}
main();``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````// Node JS program for
// Find two numbers whose sum is N
// and does not contain any digit as K
class Construction
{
checkDigitK(number, k)
{
var temp = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return true;
}
temp = parseInt(temp / 10);
}
return false;
}
constructWithoutK(n, k)
{
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
var temp = n;
var extra = 0;
var count = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = parseInt(temp / 10);
count = count * 10;
}
process.stdout.write("\n Given sum : " + n);
process.stdout.write("\n Given k : " + k);
if (extra == 0 && !this.checkDigitK(n, k))
{
process.stdout.write("\n " + n +
" (" + 0 + " + " +
n + ") \n");
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!this.checkDigitK(temp, k) &&
!this.checkDigitK(extra, k))
{
process.stdout.write("\n " + n +
" (" + temp + " + " +
extra + ") \n");
return;
}
extra++;
temp--;
}
process.stdout.write("\n number : Without digit " + n +
" value " + k + " not possible \n");
}
}

function main()
{
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}
main();``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````#  Python 3 program for
#  Find two numbers whose sum is N
#  and does not contain any digit as K
class Construction :
def checkDigitK(self, number, k) :
temp = number
while (temp != 0) :
if (temp % 10 == k) :
#  When number contains k digit
return True

temp = int(temp / 10)

return False

def constructWithoutK(self, n, k) :
if (n <= 0 or(k < 0 or k > 9)) :
return

temp = n
extra = 0
count = 1
while (temp != 0) :
if (temp % 10 == k) :
extra += count

temp = int(temp / 10)
count = count * 10

print("\n Given sum : ", n, end = "")
print("\n Given k : ", k, end = "")
if (extra == 0 and not self.checkDigitK(n, k)) :
print("\n ", n ," (", 0 ,"+", n ,") ",sep="")
return

temp = n - extra
if (extra > temp) :
#  Swap value
count = extra
extra = temp
temp = count

while (extra < temp) :
if (not self.checkDigitK(temp, k) and not
self.checkDigitK(extra, k)) :
print("\n ", n ," (", temp ,"+", extra ,") ")
return

extra += 1
temp -= 1

print("\n number : Without digit ", n ," value ", k ," not possible ")

def main() :
#  Test  A
#  Number : 111
#  K      :  1
#  Test  B
#  Number :  1
#  K      :  1
#  Test  C
#  Number :  123
#  K      :  2
#  Test  D
#  Number :  7331
#  K      :  7
#  Test  E
#  Number :  333
#  K      :  3

if __name__ == "__main__": main()``````

#### Output

`````` Given sum :  111
Given k :  1
111  ( 89 + 22 )

Given sum :  1
Given k :  1
number : Without digit  1  value  1  not possible

Given sum :  123
Given k :  2
123  ( 113 + 10 )

Given sum :  7331
Given k :  7
7331  ( 6331 + 1000 )

Given sum :  333
Given k :  3
333  ( 222 + 111 )``````
``````#  Ruby program for
#  Find two numbers whose sum is N
#  and does not contain any digit as K
class Construction
def checkDigitK(number, k)
temp = number
while (temp != 0)
if (temp % 10 == k)
#  When number contains k digit
return true
end

temp = temp / 10
end

return false
end

def constructWithoutK(n, k)
if (n <= 0 || (k < 0 || k > 9))
return
end

temp = n
extra = 0
count = 1
while (temp != 0)
if (temp % 10 == k)
extra += count
end

temp = temp / 10
count = count * 10
end

print("\n Given sum : ", n)
print("\n Given k : ", k)
if (extra == 0 && !self.checkDigitK(n, k))
print("\n ", n ," (", 0 ," + ", n ,") \n")
return
end

temp = n - extra
if (extra > temp)
#  Swap value
count = extra
extra = temp
temp = count
end

while (extra < temp)
if (!self.checkDigitK(temp, k) &&
!self.checkDigitK(extra, k))
print("\n ", n ," (", temp ," + ", extra ,") \n")
return
end

extra += 1
temp -= 1
end

print("\n number : Without digit ", n ,
" value ", k ," not possible \n")
end

end

def main()
#  Test  A
#  Number : 111
#  K      :  1
#  Test  B
#  Number :  1
#  K      :  1
#  Test  C
#  Number :  123
#  K      :  2
#  Test  D
#  Number :  7331
#  K      :  7
#  Test  E
#  Number :  333
#  K      :  3
end

main()``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)
``````
``````// Scala program for
// Find two numbers whose sum is N
// and does not contain any digit as K
class Construction()
{
def checkDigitK(number: Int, k: Int): Boolean = {
var temp: Int = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return true;
}
temp = temp / 10;
}
return false;
}
def constructWithoutK(n: Int, k: Int): Unit = {
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
var temp: Int = n;
var extra: Int = 0;
var count: Int = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = temp / 10;
count = count * 10;
}
print("\n Given sum : " + n);
print("\n Given k : " + k);
if (extra == 0 && !checkDigitK(n, k))
{
print("\n " + n + " (" + 0 + " + " + n + ") \n");
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!checkDigitK(temp, k) && !checkDigitK(extra, k))
{
print("\n " + n + " (" + temp + " + " + extra + ") \n");
return;
}
extra += 1;
temp -= 1;
}
print("\n number : Without digit " + n +
" value " + k + " not possible \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Construction = new Construction();
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}
}``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````
``````// Kotlin program for
// Find two numbers whose sum is N
// and does not contain any digit as K
class Construction
{
fun checkDigitK(number: Int, k: Int): Boolean
{
var temp: Int = number;
while (temp != 0)
{
if (temp % 10 == k)
{
// When number contains k digit
return true;
}
temp = temp / 10;
}
return false;
}
fun constructWithoutK(n: Int, k: Int): Unit
{
if (n <= 0 || (k < 0 || k > 9))
{
return;
}
var temp: Int = n;
var extra: Int = 0;
var count: Int = 1;
while (temp != 0)
{
if (temp % 10 == k)
{
extra += count;
}
temp = temp / 10;
count = count * 10;
}
print("\n Given sum : " + n);
print("\n Given k : " + k);
if (extra == 0 && !this.checkDigitK(n, k))
{
print("\n " + n + " (" + 0 + " + " + n + ") \n");
return;
}
temp = n - extra;
if (extra > temp)
{
// Swap value
count = extra;
extra = temp;
temp = count;
}
while (extra < temp)
{
if (!this.checkDigitK(temp, k) &&
!this.checkDigitK(extra, k))
{
print("\n " + n + " (" + temp + " + " + extra + ") \n");
return;
}
extra += 1;
temp -= 1;
}
print("\n number : Without digit " + n +
" value " + k + " not possible \n");
}
}
fun main(args: Array < String > ): Unit
{
// Test  A
// Number : 111
// K      :  1
// Test  B
// Number :  1
// K      :  1
// Test  C
// Number :  123
// K      :  2
// Test  D
// Number :  7331
// K      :  7
// Test  E
// Number :  333
// K      :  3
}``````

#### Output

`````` Given sum : 111
Given k : 1
111 (89 + 22)

Given sum : 1
Given k : 1
number : Without digit 1 value 1 not possible

Given sum : 123
Given k : 2
123 (113 + 10)

Given sum : 7331
Given k : 7
7331 (6331 + 1000)

Given sum : 333
Given k : 3
333 (222 + 111)``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post