Posts

C Program to Find Factorial of a Number Using Recursion

Example to find factorial of a non-negative integer (entered by the user) using recursion. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions C Programming Recursion The factorial of a positive number n is given by: factorial of n (n!) = 1*2*3*4....n The factorial of a negative number doesn't exist. And the factorial of 0 is 1. You will learn to find the factorial of a number using recursion in this example. Visit this page to learn, how you can find the factorial of a number using loop.Read -  Programming language Example: Factorial of a Number Using Recursion #include <stdio.h> long int multiplyNumbers ( int n ); int main () { int n ; printf ( "Enter a positive integer: " ); scanf ( "%d" , & n ); printf ( "Factorial of %d = %ld" , n , multiplyNumbers ( n )); return 0...

C Program to Find G.C.D Using Recursion

Example to find the GCD of two positive integers (entered by the user) using recursion in C programming. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions C Programming Recursion This program takes two positive integers as input from the user and calculates  GCD  using recursion. Read -  Programming language Visit this page to learn how you can calculate the  GCD  using loops. Example: GCD of Two Numbers using Recursion #include <stdio.h> int hcf ( int n1 , int n2 ); int main () { int n1 , n2 ; printf ( "Enter two positive integers: " ); scanf ( "%d %d" , & n1 , & n2 ); printf ( "G.C.D of %d and %d is %d." , n1 , n2 , hcf ( n1 , n2 )); return 0 ; } int hcf ( int n1 , int n2 ) { if ( n2 != 0 ) return hcf ( n2 , n1 % n2 ); else ...

C Program to Convert Binary Number to Decimal and vice-versa

In this example, you will learn to convert binary number to decimal and decimal number to binary manually by creating a user-defined function. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions Visit this page to learn how to convert binary number to decimal. Read -  Programming language Example 1: Program to convert binary number to decimal #include <stdio.h> #include <math.h> int convertBinaryToDecimal ( long long n ); int main () { long long n ; printf ( "Enter a binary number: " ); scanf ( "%lld" , & n ); printf ( "%lld in binary = %d in decimal" , n , convertBinaryToDecimal ( n )); return 0 ; } int convertBinaryToDecimal ( long long n ) { int decimalNumber = 0 , i = 0 , remainder ; while ( n != 0 ) { remainder = n % 10 ; ...

C Program to Convert Octal Number to Decimal and vice-versa

In this example, you will learn to convert octal number to decimal and decimal number to octal manually by creating a user-defined function.  Read -  Programming language To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions Example 1: Program to Convert Decimal to Octal #include <stdio.h> #include <math.h> int convertDecimalToOctal ( int decimalNumber ); int main () { int decimalNumber ; printf ( "Enter a decimal number: " ); scanf ( "%d" , & decimalNumber ); printf ( "%d in decimal = %d in octal" , decimalNumber , convertDecimalToOctal ( decimalNumber )); return 0 ; } int convertDecimalToOctal ( int decimalNumber ) { int octalNumber = 0 , i = 1 ; while ( decimalNumber != 0 ) { octalNumber += ( decimalNumber % 8 ) * i ; dec...

C Program to Convert Binary Number to Octal and vice-versa

In this example, you will learn to convert binary number to octal and octal number to binary manually by creating a user-defined function. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions Example 1: Program to Convert Binary to Octal In this program, we will first convert binary number to decimal. Then, the decimal number is converted to octal. Read -  Programming language #include <stdio.h> #include <math.h> int convertBinarytoOctal ( long long binaryNumber ); int main () { long long binaryNumber ; printf ( "Enter a binary number: " ); scanf ( "%lld" , & binaryNumber ); printf ( "%lld in binary = %d in octal" , binaryNumber , convertBinarytoOctal ( binaryNumber )); return 0 ; } int convertBinarytoOctal ( long long binaryNumber ) { int octalNumber = 0...

C program to Reverse a Sentence Using Recursion

This program takes a sentence from user and reverses that sentence using recursion. This program does not use string to reverse the sentence or store the sentence. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C Programming User-defined functions C Programming Recursion Example: Reverse a sentence using recursion /* Example to reverse a sentence entered by user without using strings. */ #include <stdio.h> void reverseSentence (); int main () { printf ( "Enter a sentence: " ); reverseSentence (); return 0 ; } void reverseSentence () { char c ; scanf ( "%c" , & c ); if ( c != '\n' ) { reverseSentence (); printf ( "%c" , c ); } } Output Enter a sentence: margorp emosewa awesome program This program first prints "Enter a sentence: ". Then, i...