Pergunta de entrevista da empresa Digital Core Technologies

Write a program to program to print 1 to N without any loop or goto statements ?

Respostas da entrevista

Sigiloso

9 de nov. de 2016

use recursion. #include #include int rec(int); void main(){ scanf(%d, &N); rec(N+1); } int rec(int N){ if(N == 1) return 1; else printf("%d\n", rec(N-1)); return N; }

2

Sigiloso

8 de jun. de 2017

#include #include int add(int num1, int num2); int main() { int num1, num2, product = 0, i; printf ("Enter first number\n"); scanf("%d", &num1); printf("Enter second number\n"); scanf("%d", &num2); /* Add num1 to itself, num2 times */ for (i = 0; i < num2; i++) { product = add(product, num1); } printf("Product of %d and %d is %d\n", num1, num2, product); getch(); return 0; } /* Add two numbers using bitwise operators */ int add(int num1, int num2) { int carry; while (num2 != 0) { carry = (num1 & num2) << 1; /* calculating the sum */ num1 = num1 ^ num2; num2 = carry; } return num1; }