Saturday, 18 June 2016

C And C++ Program to Print 1-10 numbers without using Conditional Loops Problem Solution.


C Solution:


#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void printNumber(int value) {
 
    printf("%d\n", value);
    value++;
 
    if (value > 10)return;
    printNumber(value);
}

int main(void) {
 
    printNumber(1);
    return 0;
}

Another Way:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(void) {
 
   static int i = 1;// Static variable inside a function means “once the variable has been initialized, it remains in memory until the end of the program.”
   if (i <= 10) {
      printf("%d\n",i++);
      main();
   }
   return 0;
}


C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void printNumber(int value) {
 
    cout << value << endl;
    value++;
 
    if (value > 10)return;
    printNumber(value);
}

int main() {
 
    printNumber(1);
    return 0;
}

Another Way:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(void) {
 
   static int i = 1;// Static variable inside a function means “once the variable has been initialized, it remains in memory until the end of the program.”
   if (i <= 10) {
      cout << i++ << endl;
      main();
   }
   return 0;
}

No comments:

Post a Comment