Posts

Showing posts from July, 2017

C Program to Reverse a Number

Example to reverse an integer entered by the user. This problem is solved using while loop in this example. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C Programming while and do...while Loop Example: Reverse an Integer #include <stdio.h> int main () { int n , reversedNumber = 0 , remainder ; printf ( "Enter an integer: " ); scanf ( "%d" , & n ); while ( n != 0 ) { remainder = n % 10 ; reversedNumber = reversedNumber * 10 + remainder ; n /= 10 ; } printf ( "Reversed Number = %d" , reversedNumber ); return 0 ; } Output Enter an integer: 2345 Reversed Number = 5432 This program takes an integer input from the user. Then the while loop is used until  n != 0 is false. Read -  Programming language In each iteration of while loop, the remainder when 

C Program to Calculate the Power of a Number

Example on how to calculate the power of a number if the exponent is an integer. Also, you will learn to compute the power using pow() function. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C Programming while and do...while Loop The program below takes two integers from the user (a base number and an exponent) and calculates the power. Read -  Programming language For example:  In case of 2 3 2 is the base number 3 is the exponent And, the power is equal to 2*2*2 Example #1: Power of a Number Using while Loop #include <stdio.h> int main () { int base , exponent ; long long result = 1 ; printf ( "Enter a base number: " ); scanf ( "%d" , & base ); printf ( "Enter an exponent: " ); scanf ( "%d" , & exponent ); while ( exponent != 0 ) { result *= base ; -

C Program to Check Whether a Number is Palindrome or Not

This program reverses an integer (entered by the user) using while loop. Then, if statement is used to check whether the reversed number is equal to the original number or not. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if, if...else and Nested if...else Statement C Programming while and do...while Loop An integer is a palindrome if the reverse of that number is equal to the original number. Read -  Programming language Example: Program to Check Palindrome #include <stdio.h> int main () { int n , reversedInteger = 0 , remainder , originalInteger ; printf ( "Enter an integer: " ); scanf ( "%d" , & n ); originalInteger = n ; // reversed integer is stored in variable while ( n != 0 ) { remainder = n % 10 ; reversedInteger = reversedInteger * 10 + remainder ; n /= 10 ;

C Program to Check Whether a Number is Prime or Not

Example to check whether an integer (entered by the user) is a prime number or not using for loop and if...else statement. 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 break and continue Statement A prime number is a positive integer which is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13 Read -  Programming language Example: Program to Check Prime Number #include <stdio.h> int main () { int n , i , flag = 0 ; printf ( "Enter a positive integer: " ); scanf ( "%d" ,& n ); for ( i = 2 ; i <= n / 2 ; ++ i ) { // condition for nonprime number if ( n % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) printf ( "%d is a prime number." , n ); else

C Program to Display Prime Numbers Between Two Intervals

Example to print all prime numbers between two numbers (entered by the user). This problem is solved using nested for loop and if...else statement. 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 break and continue Statement Example #1: Display Prime Numbers Between two Intervals #include <stdio.h> int main () { int low , high , i , flag ; printf ( "Enter two numbers(intervals): " ); scanf ( "%d %d" , & low , & high ); printf ( "Prime numbers between %d and %d are: " , low , high ); while ( low < high ) { flag = 0 ; for ( i = 2 ; i <= low / 2 ; ++ i ) { if ( low % i == 0 ) { flag = 1 ; break ; } } if ( flag ==

C Program to Check Armstrong Number

Example to check whether an integer (entered by the user) is an Armstrong number or not using while loop and if...else statement. 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 while and do...while Loop A positive integer is called an Armstrong number of order  n  if abcd... = a n + b n + c n + d n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number. Example #1: Check Armstrong Number of three digits #include <stdio.h> int main () { int number , originalNumber , remainder , result = 0 ; printf ( "Enter a three digit integer: " ); scanf ( "%d" , & number ); originalNumber = number ; while ( originalNumber != 0 ) { remainder = orig

C Program to Display Armstrong Number Between Two Intervals

Example to find all Armstrong numbers between two integers (entered by the user) using loops and if...else statement. 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 A positive integer is called an Armstrong number of order  n  if abcd... = a n + b n + c n + d n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number. This program is built on the concept of how to check whether an integer is an Armstrong number or not. Read -  Programming language Example: Armstrong Numbers Between Two Integers #include <stdio.h> #include <math.h> int main () { int low , high , i , temp1 , temp2 , remainder , n = 0 , result = 0 ; printf ( "Enter two numbers(intervals): " );