

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check whether a number is Proth number or not in C++
Given with a number ‘n’ and the task is to determine whether the given positive integer is a proth or not and display the result as an output.
What is Proth Number?
A proth number is given by
$$N=k\cdot\:2^{n}+1$$
Where, n is a positive integer and k is a odd positive integer
The first few proth numbers are given below −
3, 5, 9, 13, 17, 25, 33, 41, 49, 57, 65, 81, 97.......
Input
number: 17
Output
its a proth number
Input
number: 18
Output
its not a proth number
Approach used in the given program is as follows
Input the number to check for the condition
Apply the given formula to check whether its a proth number or not
If the condition holds true print its a proth number
If the condition doesn’t holds true print its not a proth number
Algorithm
Step 1→ declare function to calculate power of 2 bool isPower(int num) return (num && !(num & (num - 1))) Step 2→ Declare function to check if a number is a proth number or not bool isProth(int num) declare int k = 1 While (k < (num / k)) IF (num % k == 0) IF (isPower(num / k)) return true End Set k = k + 2 End End return false Step 3→ In main() Declare int num = 17 IF (isProth(num - 1)) Print "its a proth number" End Else Print "its not a proth number" End
Example
#include <bits/stdc++.h> using namespace std; //function to calculate power of 2 bool isPower(int num){ return (num && !(num & (num - 1))); } //function to check if a number is a proth number bool isProth(int num){ int k = 1; while (k < (num / k)){ if (num % k == 0){ if (isPower(num / k)) return true; } k = k + 2; } return false; } int main(){ int num = 17; if (isProth(num - 1)) cout << "its a proth number"; else cout << "its not a proth number"; return 0; }
Output
If run the above code it will generate the following output −
its a proth number
- Related Questions & Answers
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Program to check whether the given number is Buzz Number or not in C++
- Java Program to Check Whether a Number is Prime or Not
- C# Program to Check Whether the Entered Number is an Armstrong Number or Not
- Check whether a number is a Fibonacci number or not JavaScript
- Program to check whether given number is Narcissistic number or not in Python
- Write a Golang program to check whether a given number is prime number or not
- C++ Program to Check Whether Number is Even or Odd
- Check whether the given number is Euclid Number or not in Python
- C program to Check Whether a Number is Positive or Negative or Zero?
- Program to check a number is ugly number or not in Python
- How to check whether a number is finite or not in JavaScript?
- C# Program to check if a number is prime or not
Advertisements