EMC Interview Question
Software Engineer in TestsTeam: RSA
Country: India
Interview Type: Written Test
/* use of goto staements */
#include<stdio.h>
main()
{
int i=1;
A:
if(i<=500)
{
printf("%d\t",i);
i++;
goto A;
}
else
exit(0);
}
Use TMP - Template Meta Programming, to get the required functionality.
#include <iostream>
template<int N>
struct NumberGeneration{
static void out(std::ostream& os)
{
NumberGeneration<N-1>::out(os);
os << N << std::endl;
}
};
template<>
struct NumberGeneration<1>{
static void out(std::ostream& os)
{
os << 1 << std::endl;
}
};
int main(){
NumberGeneration<100>::out(std::cout);
}
Without loops, print 500 consecutive numbers? Recursion NOT allowed.
The answer is so simple :)
printf("1,2,3,4,5...498,499,500");
If recursion is allowed, The function can be:
void serial(int i) {
if(i > 0) { print("%d,", i); serial(i-1); }
}
serial(500);
It'll print 500 numbers in serial order (but in reverse)
can recursion be used?if yes
- Anonymous June 02, 2012