Check that if given number is expressed sum of consecutive or not
Here given code implementation process.
// C program
// Check that if given number is expressed sum of consecutive or not
#include <stdio.h>
// Check whether given number is sum of consecutive two or more numbers
void consecutive_sum(int num)
{
if (num < 0)
{
return;
}
// Given number
printf("\n Number : %d", num);
if (num != 0 && ((num & (num - 1)) != 0))
{
printf("\n Yes ");
}
else
{
printf("\n No ");
}
}
int main(int argc, char
const *argv[])
{
consecutive_sum(9);
consecutive_sum(4);
return 0;
}
Output
Number : 9
Yes
Number : 4
No
/*
Java program
Check that if given number is expressed sum of consecutive or not
*/
public class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
public void consecutiveSum(int num)
{
if (num < 0)
{
return;
}
// Given number
System.out.print("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
System.out.print("\n Yes ");
}
else
{
System.out.print("\n No ");
}
}
public static void main(String[] args)
{
Consecutive task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
}
Output
Number : 9
Yes
Number : 4
No
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
public:
// Check whether given number is sum of consecutive two or more numbers
void consecutiveSum(int num)
{
if (num < 0)
{
return;
}
// Given number
cout << "\n Number : " << num;
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num &(num - 1)) != 0))
{
// When Yes
cout << "\n Yes ";
}
else
{
cout << "\n No ";
}
}
};
int main()
{
Consecutive task = Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
return 0;
}
Output
Number : 9
Yes
Number : 4
No
// Include namespace system
using System;
/*
C# program
Check that if given number is expressed sum of consecutive or not
*/
public class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
public void consecutiveSum(int num)
{
if (num < 0)
{
return;
}
// Given number
Console.Write("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
Console.Write("\n Yes ");
}
else
{
Console.Write("\n No ");
}
}
public static void Main(String[] args)
{
Consecutive task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
}
Output
Number : 9
Yes
Number : 4
No
<?php
/*
Php program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
public function consecutiveSum($num)
{
if ($num < 0)
{
return;
}
// Given number
echo "\n Number : ". $num;
// Check that number is constructed by two or more consecutive numbers or not
if ($num != 0 && (($num & ($num - 1)) != 0))
{
// When Yes
echo "\n Yes ";
}
else
{
echo "\n No ";
}
}
}
function main()
{
$task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
$task->consecutiveSum(9);
// num = 4
$task->consecutiveSum(4);
}
main();
Output
Number : 9
Yes
Number : 4
No
/*
Node Js program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
consecutiveSum(num)
{
if (num < 0)
{
return;
}
// Given number
process.stdout.write("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
process.stdout.write("\n Yes ");
}
else
{
process.stdout.write("\n No ");
}
}
}
function main()
{
var task = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
main();
Output
Number : 9
Yes
Number : 4
No
# Python 3 program
# Check that if given number is expressed sum of consecutive or not
class Consecutive :
# Check whether given number is sum of consecutive two or more numbers
def consecutiveSum(self, num) :
if (num < 0) :
return
# Given number
print("\n Number : ", num, end = "")
# Check that number is constructed by two or more consecutive numbers or not
if (num != 0 and((num & (num - 1)) != 0)) :
# When Yes
print("\n Yes ", end = "")
else :
print("\n No ", end = "")
def main() :
task = Consecutive()
# num = 9
# = 4+5 (consecutive number)
task.consecutiveSum(9)
# num = 4
task.consecutiveSum(4)
if __name__ == "__main__": main()
Output
Number : 9
Yes
Number : 4
No
# Ruby program
# Check that if given number is expressed sum of consecutive or not
class Consecutive
# Check whether given number is sum of consecutive two or more numbers
def consecutiveSum(num)
if (num < 0)
return
end
# Given number
print("\n Number : ", num)
# Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
# When Yes
print("\n Yes ")
else
print("\n No ")
end
end
end
def main()
task = Consecutive.new()
# num = 9
# = 4+5 (consecutive number)
task.consecutiveSum(9)
# num = 4
task.consecutiveSum(4)
end
main()
Output
Number : 9
Yes
Number : 4
No
/*
Scala program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
def consecutiveSum(num: Int): Unit = {
if (num < 0)
{
return;
}
// Given number
print("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
print("\n Yes ");
}
else
{
print("\n No ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Consecutive = new Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
}
Output
Number : 9
Yes
Number : 4
No
/*
Swift 4 program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
func consecutiveSum(_ num: Int)
{
if (num < 0)
{
return;
}
// Given number
print("\n Number : ", num, terminator: "");
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num & (num - 1)) != 0))
{
// When Yes
print("\n Yes ", terminator: "");
}
else
{
print("\n No ", terminator: "");
}
}
}
func main()
{
let task: Consecutive = Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
main();
Output
Number : 9
Yes
Number : 4
No
/*
Kotlin program
Check that if given number is expressed sum of consecutive or not
*/
class Consecutive
{
// Check whether given number is sum of consecutive two or more numbers
fun consecutiveSum(num: Int): Unit
{
if (num < 0)
{
return;
}
// Given number
print("\n Number : " + num);
// Check that number is constructed by two or more consecutive numbers or not
if (num != 0 && ((num and(num - 1)) != 0))
{
// When Yes
print("\n Yes ");
}
else
{
print("\n No ");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Consecutive = Consecutive();
// num = 9
// = 4+5 (consecutive number)
task.consecutiveSum(9);
// num = 4
task.consecutiveSum(4);
}
Output
Number : 9
Yes
Number : 4
No
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