Count number of consecutive repeating digits in a number
Here given code implementation process.
// C Program for
// Count number of consecutive repeating digits in a number
#include <stdio.h>
int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void consecutiveRepeat(int num)
{
int n = absValue(num);
int back = -1;
int count = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count++;
}
// Remove last digit
n = n / 10;
}
printf("\n Given n : %d", num);
printf("\n Result : %d", count);
}
int main(int argc, char
const *argv[])
{
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
consecutiveRepeat(-555666);
return 0;
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
/*
Java program for
Count number of consecutive repeating digits in a number
*/
public class DigitCounting
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void consecutiveRepeat(int num)
{
int n = absValue(num);
int back = -1;
int count = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count++;
}
// Remove last digit
n = n / 10;
}
System.out.print("\n Given n : " + num );
System.out.print("\n Result : " + count );
}
public static void main(String[] args)
{
DigitCounting task = new DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666);
}
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Count number of consecutive repeating digits in a number
*/
class DigitCounting
{
public: int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void consecutiveRepeat(int num)
{
int n = this->absValue(num);
int back = -1;
int count = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count++;
}
// Remove last digit
n = n / 10;
}
cout << "\n Given n : " << num;
cout << "\n Result : " << count;
}
};
int main()
{
DigitCounting *task = new DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task->consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task->consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task->consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task->consecutiveRepeat(-555666);
return 0;
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
package main
import "fmt"
/*
Go program for
Count number of consecutive repeating digits in a number
*/
type DigitCounting struct {}
func getDigitCounting() * DigitCounting {
var me *DigitCounting = &DigitCounting {}
return me
}
func(this DigitCounting) absValue(num int) int {
if num < 0 {
return -num
}
return num
}
func(this DigitCounting) consecutiveRepeat(num int) {
var n int = this.absValue(num)
var back int = -1
var count int = 0
for (n > 0) {
if (n % 10) != back {
// Get new digit
back = n % 10
} else {
// When digits are repeated
count++
}
// Remove last digit
n = n / 10
}
fmt.Print("\n Given n : ", num)
fmt.Print("\n Result : ", count)
}
func main() {
var task * DigitCounting = getDigitCounting()
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444)
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022)
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023)
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666)
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
// Include namespace system
using System;
/*
Csharp program for
Count number of consecutive repeating digits in a number
*/
public class DigitCounting
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void consecutiveRepeat(int num)
{
int n = this.absValue(num);
int back = -1;
int count = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count++;
}
// Remove last digit
n = n / 10;
}
Console.Write("\n Given n : " + num);
Console.Write("\n Result : " + count);
}
public static void Main(String[] args)
{
DigitCounting task = new DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666);
}
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
<?php
/*
Php program for
Count number of consecutive repeating digits in a number
*/
class DigitCounting
{
public function absValue($num)
{
if ($num < 0)
{
return -$num;
}
return $num;
}
public function consecutiveRepeat($num)
{
$n = $this->absValue($num);
$back = -1;
$count = 0;
while ($n > 0)
{
if (($n % 10) != $back)
{
// Get new digit
$back = $n % 10;
}
else
{
// When digits are repeated
$count++;
}
// Remove last digit
$n = (int)($n / 10);
}
echo("\n Given n : ".$num);
echo("\n Result : ".$count);
}
}
function main()
{
$task = new DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
$task->consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
$task->consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
$task->consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
$task->consecutiveRepeat(-555666);
}
main();
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
/*
Node JS program for
Count number of consecutive repeating digits in a number
*/
class DigitCounting
{
absValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
consecutiveRepeat(num)
{
var n = this.absValue(num);
var back = -1;
var count = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count++;
}
// Remove last digit
n = parseInt(n / 10);
}
process.stdout.write("\n Given n : " + num);
process.stdout.write("\n Result : " + count);
}
}
function main()
{
var task = new DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666);
}
main();
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
# Python 3 program for
# Count number of consecutive repeating digits in a number
class DigitCounting :
def absValue(self, num) :
if (num < 0) :
return -num
return num
def consecutiveRepeat(self, num) :
n = self.absValue(num)
back = -1
count = 0
while (n > 0) :
if ((n % 10) != back) :
# Get new digit
back = n % 10
else :
# When digits are repeated
count += 1
# Remove last digit
n = int(n / 10)
print("\n Given n : ", num, end = "")
print("\n Result : ", count, end = "")
def main() :
task = DigitCounting()
# Test A
# num = 112223444
# 112223444
# - -- --
# output = 5
task.consecutiveRepeat(112223444)
# Test B
# num = 10001022
# 10001022
# -- -
# output = 3
task.consecutiveRepeat(10001022)
# Test C
# num = 505023
# output = 0
task.consecutiveRepeat(505023)
# Test D
# num = -555663
# 555666
# -- --
# output = 4
task.consecutiveRepeat(-555666)
if __name__ == "__main__": main()
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
# Ruby program for
# Count number of consecutive repeating digits in a number
class DigitCounting
def absValue(num)
if (num < 0)
return -num
end
return num
end
def consecutiveRepeat(num)
n = self.absValue(num)
back = -1
count = 0
while (n > 0)
if ((n % 10) != back)
# Get new digit
back = n % 10
else
# When digits are repeated
count += 1
end
# Remove last digit
n = n / 10
end
print("\n Given n : ", num)
print("\n Result : ", count)
end
end
def main()
task = DigitCounting.new()
# Test A
# num = 112223444
# 112223444
# - -- --
# output = 5
task.consecutiveRepeat(112223444)
# Test B
# num = 10001022
# 10001022
# -- -
# output = 3
task.consecutiveRepeat(10001022)
# Test C
# num = 505023
# output = 0
task.consecutiveRepeat(505023)
# Test D
# num = -555663
# 555666
# -- --
# output = 4
task.consecutiveRepeat(-555666)
end
main()
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
/*
Scala program for
Count number of consecutive repeating digits in a number
*/
class DigitCounting()
{
def absValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
def consecutiveRepeat(num: Int): Unit = {
var n: Int = absValue(num);
var back: Int = -1;
var count: Int = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count += 1;
}
// Remove last digit
n = n / 10;
}
print("\n Given n : " + num);
print("\n Result : " + count);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: DigitCounting = new DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666);
}
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
/*
Swift 4 program for
Count number of consecutive repeating digits in a number
*/
class DigitCounting
{
func absValue(_ num: Int) -> Int
{
if (num < 0)
{
return -num;
}
return num;
}
func consecutiveRepeat(_ num: Int)
{
var n: Int = self.absValue(num);
var back: Int = -1;
var count: Int = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count += 1;
}
// Remove last digit
n = n / 10;
}
print("\n Given n : ", num, terminator: "");
print("\n Result : ", count, terminator: "");
}
}
func main()
{
let task: DigitCounting = DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666);
}
main();
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
/*
Kotlin program for
Count number of consecutive repeating digits in a number
*/
class DigitCounting
{
fun absValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
fun consecutiveRepeat(num: Int): Unit
{
var n: Int = this.absValue(num);
var back: Int = -1;
var count: Int = 0;
while (n > 0)
{
if ((n % 10) != back)
{
// Get new digit
back = n % 10;
}
else
{
// When digits are repeated
count += 1;
}
// Remove last digit
n = n / 10;
}
print("\n Given n : " + num);
print("\n Result : " + count);
}
}
fun main(args: Array < String > ): Unit
{
val task: DigitCounting = DigitCounting();
// Test A
// num = 112223444
//
// 112223444
// - -- --
// output = 5
task.consecutiveRepeat(112223444);
// Test B
// num = 10001022
//
// 10001022
// -- -
// output = 3
task.consecutiveRepeat(10001022);
// Test C
// num = 505023
//
// output = 0
task.consecutiveRepeat(505023);
// Test D
// num = -555663
//
// 555666
// -- --
// output = 4
task.consecutiveRepeat(-555666);
}
Output
Given n : 112223444
Result : 5
Given n : 10001022
Result : 3
Given n : 505023
Result : 0
Given n : -555666
Result : 4
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.
New Comment