Natural Numbers in C++ Program



In this article, we will cover C++ programs to work with natural numbers. we use natural numbers in various operations, such as indexing arrays, performing loops, and validating user input. we will also write code to demonstrate how natural numbers can be used for these purposes in C++.

Natural Numbers are a set of positive numbers, starting from 1 and going to infinity. natural numbers are: 1,2,3,4,5... etc

In C++, understand concepts like conditions, loops, and how we store and manipulate variables. We will provide everything you need to know about natural numbers along with programs. In some situations, 0 is considered a natural number, but traditionally natural numbers start from 1.

When Do We Use Natural Numbers in C++?

We will use natural numbers in C++ when we are ?

  • Indexing Arrays: To access array elements, we use natural numbers. For example, to print the whole array, we start the loop from 0 until the array's length.
  • Repeating Actions (Loops): Natural numbers are useful for repeating tasks. For instance, if we want to print numbers from 1 to 10, we can start the loop from 1 to 10.
  • Validating Input Values: When taking input from the user, we use natural numbers to validate if the input is positive. For this purpose, we use conditions to check the value.

Natural Numbers in C++

In C++ natural numbers are stored as integers, to declare a variable and store the natural number we use. below is an example that shows how we can declare the natural number in C++.int.

int num = 10;
  • Printing n natural numbers using C++
  • Printing n natural numbers, while reading the n value from the user.
  • Verifying whether a given number is a natural number.

Printing n Natural Numbers Using C++

In this section, we will see how we can print the natural numbers. below is the simplest program to print natural numbers ?

Steps:

  • Declare a variable n to store the last natural number you have to print
  • Start a for loop from 1 till n.
  • Print each number in the loop

Example

Following is an example, of how we can print natural numbers in C++.

#include <iostream>
using namespace std;
        
int main() {
   int n = 10; 
   for (int i = 1; i <= n; i++) {
      cout << i << " ";
   }
   return 0;
}

Reading n Value From User

Now, we will show you how we can take natural numbers as input from the user. below are the steps that provide the whole process ?

Steps:

  • Declare a Variable: Declare an integer variable to store the user's input.
  • Prompt the User: Use cout to ask the user to enter a positive integer.
  • Read the Input: Use cin to get the input from the user and store it in the variable.

Example

In the following example we are accepting an integer from the user and printing the natural numbers up to the given value ?

#include <iostream>
using namespace std;

int main() {
   int n;
   cout << "Enter a Number: ";
   cin >> n;

   for (int i = 1; i <= n; i++) {
      cout <<  i << " ";
   }
   return 0;
}

C++ program to check if a Number is Natural

Sometimes, we need to check if the given number is natural or not, we often use this kind of condition in simple as well as complex programs.Now, we will show you how we can check if the number is natural.

Steps:

  • Declare a variable num
  • Write a prompt message, asking the user to enter a number using cout
  • Take input from the user by using Cin
  • Write a condition to check if num is greater than 0 if yes, then print "Yes" else print "No"

Example

Following is a C++ program to verify whether the given number is a natural number ?

#include <iostream>
using namespace std;

int main() {
   int num = 9;

   if (num > 0) {
      cout << "Yes" << endl;
   } else {
      cout << "No" << endl;
   }
   return 0;
}
Updated on: 2024-11-11T15:30:26+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements