You may have made program to print numbers from 1 to n using
loops, recursion or goto statement. But have you ever thought about doing this
without help of loops, recursion or goto? It can be done without using such
things as explained in below program.Also Read: C program to find factorial of any number using recursion
Also Read: C++ Templates: Program to Swap Two Numbers Using Function Template
loops, recursion or goto statement. But have you ever thought about doing this
without help of loops, recursion or goto? It can be done without using such
things as explained in below program.Also Read: C program to find factorial of any number using recursion
Also Read: C++ Templates: Program to Swap Two Numbers Using Function Template
#include<iostream>
using namespace std;
class Num
{
public:
static int i;
Num()
{
cout<<i++<<” “;
}
};
int Num::i=1;
int main()
{
int n;
cout<<“Enter value on n:”;
cin>>n;
Num obj[n];
return 0;
}
In this program we are using the concept of static data
member and array of objects. Class Num
contains a static variable i whose value
will remain till the program terminates. We are creating an array of objects
of class Num. In this program we are
creating n objects, value of n depends on input we give. The default
constructor is called for all objects one by one. In the constructor the value
of i is printed and is incremented
by one in each call. In this way numbers from 1 to n are printed.
member and array of objects. Class Num
contains a static variable i whose value
will remain till the program terminates. We are creating an array of objects
of class Num. In this program we are
creating n objects, value of n depends on input we give. The default
constructor is called for all objects one by one. In the constructor the value
of i is printed and is incremented
by one in each call. In this way numbers from 1 to n are printed.
If you have any doubts or any suggestion regarding above
program then you can ask it by commenting below.
program then you can ask it by commenting below.
The array declaration is doubtful.. In compiletime array decalaration (static array) size cant b a variable..
C++ standard doesn't allow this but it is possible in gcc compiler.
Ham aapko facebook request send nahi kar pa rahe hai
I have reached 5000 friends so can’t add more, you can follow me.
int n;
cout <> n;
Num *obj = new Num[n];
Nice one, but 2.438s to print print 60 numbers looks a little slow…
This time also include the time taken to enter input. I am using codeblocks.
Where is obj declared?
Cool!! Thanks. Our professor doesn't teach us well.
Yes the programe give errors about "obj" whats the problem is…?????
But this is in c++
I want in C
Cant be done in C as the concept of constructor & destructor belongs to classes in C++ and in C we have structures.
"Under the hood" this is just a loop too.
runtime sized array are not allowed in C++ standard. It's allowed by gcc as an extension.
try this code if anyone facing problem
#include
using namespace std;
class Num
{
public:
static int i;
Num()
{
cout<<i++<<" ";
}
};
int Num::i=1;
int main()
{
Num obj[60];
return 0;
}
Your code will print from 1 to 60 only. We want to print from 1 to n, the value of n depends on input. So the code I have shared above is suitable for it. Anyways, thanks for your nice suggestion.
Num obj[60]; instead this use below thing
int n;
cout << "enter the value of n" <> n;
num *obj = new Num[n];
When I compile,errors says unused variable 'obj' and ISO C++ forbids variable length array 'obj'. Do I have to run this in header file seperate from the main or can I run it as shown here in a single cpp file?