Posts

Showing posts with the label C PROGRAMMING EXAMPLES

C program to Reverse a Sentence Using Recursion #43

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 p...

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

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. #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 ) {...

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

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. 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 ...

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

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 the binary number to decimal. 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 ) { ...

C Program to Find the Sum of Natural Numbers using Recursion #38

Image
C Program to Find the Sum of Natural Numbers using Recursion Example to find the sum of natural numbers by using a recursive function. To understand this example, you should have the knowledge of following C programming topics: C Programming User-defined functions C Programming Recursion The positive numbers 1, 2, 3... are known as natural numbers. The program below takes a positive integer from the user and calculates the sum up to the given number. You can  find the sum of natural numbers using loop  as well. However, you will learn to solve this problem using recursion here. Example: Sum of Natural Numbers Using Recursion #include <stdio.h> int addNumbers ( int n ); int main () { int num ; printf ( "Enter a positive integer: " ); scanf ( "%d" , & num ); printf ( "Sum = %d" , addNumbers ( num )); return 0 ; } int addNumbers ( int n ) { if ( n != 0 ) return n ...

C Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers #37

Image
C Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers Example to check if an integer (entered by the user) can be expressed as the sum of two prime numbers of all possible combinations with the use of functions. To understand this example, you should have the knowledge of following C programming topics: C if...else Statement C Programming for Loop C Programming Functions C Programming User-defined functions To accomplish this task,  checkPrime()  function is created. The  checkPrime()  returns 1 if the number passed to the function is a  prime number .  Example: Integer as a Sum of Two Prime Numbers #include <stdio.h> int checkPrime ( int n ); int main () { int n , i , flag = 0 ; printf ( "Enter a positive integer: " ); scanf ( "%d" , & n ); for ( i = 2 ; i <= n / 2 ; ++ i ) { // condition for i to be a prime number ...