Posts

Showing posts from 2018

Factorial Calculation in C++ Using recursive method

Image
In this C++ program, we will be using recursive method to calculate factorial from 0 through 10. A recursive method is a method that calls itself either directly or indirectly through another method. A recursive declaration of the factorial method is  n! = n.(n-1)! with that given, the source code below illustrates just how recursive methods works. #include <iostream> double factorial( double number) using namespace std; int main() {     for(int counter = 0; counter <= 10; counter++)     cout <<counter<<"! = "<<factorial(counter)<<"\n"<<endl;     return 0; } double factorial(double number) {     if(number <= 1)         return 1;     else         return number * factorial(number - 1); }  

C Program to Guess a Number

Image
From  Deitel C how to program (Making a difference) This is a carefully sort out source of in c program that plays "guess the number" as follows: The program chooses the number to be guessed by selecting a random integer in the range 1 to 1000.. The player types a first guess and presses the Enter key. If the player guess is incorrect, the program displays 'Too high.Try again or Too Low. Try again. When the user enters the correct answer, displays 'Congratulations. You guessed the number! We will basically be using the IF Statements to compare user input to determine if it matches the random generated number. So we  use the if statement to determine if the number input by the user is correct else the program either prints out too high or too low.  #include <stdio.h> #include <stdlib.h> #include <time.h>  int  main(void) {     int a=0, b=0;     int choice; srand(time(NULL));     a = 1 ...