Posts

Showing posts from July, 2017

C Program to Display Prime Numbers Between Intervals Using Function

Example to print all prime numbers between two numbers (entered by the user) by making a user-defined function. To understand this example, you should have the knowledge of following C programming topics: C Programming for Loop C Programming break and continue Statement C Programming Functions C Programming User-defined functions To find all prime numbers between two integers,  checkPrimeNumber()  function is created. This function checks whether a number is prime or not. Read -  Programming language Example: Prime Numbers Between Two Integers #include <stdio.h> int checkPrimeNumber ( int n ); int main () { int n1 , n2 , i , flag ; printf ( "Enter two positive integers: " ); scanf ( "%d %d" , & n1 , & n2 ); printf ( "Prime numbers between %d and %d are: " , n1 , n2 ); for ( i = n1 + 1 ; i < n2 ; ++ i ) { // i is a prime number, flag will be equal to 1

C Program to Check Prime or Armstrong Number Using User-defined Function

Example to check whether an integer is a prime number or an Armstrong or both by creating two separate functions. To understand this example, you should have the knowledge of following C programming topics: C Programming for Loop C Programming while and do...while Loop C Programming break and continue Statement C Programming Functions Types of User-defined Functions in C Programming In this program, two user-defined functions  checkPrimeNumber()  and  checkArmstrongNumber() are created. The  checkPrimeNumber()  returns 1 if the number entered by the user is a prime number. Similarly,  checkArmstrongNumber()  returns 1 if the number entered by the user is an Armstrong number. Read -  Programming language Visit these pages to learn to check whether a number is a prime number or not an Armstrong number or not Example: Check Prime and Armstrong Number #include <stdio.h> #include <math.h> int checkPrimeNumber ( int n ); int checkA

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.  Read -  Programming language To understand this example, you should have the knowledge of following C programming topics: C if, if...else and Nested 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 if ( checkPrime ( i ) == 1 ) {

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. Read -  Programming language 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 + addNumbers ( n - 1 ); el

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 return

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 ;