Posts

Showing posts from July, 2017

C Program to Check Whether a Number is Positive or Negative

In this example, you will learn to check whether a number (entered by the user) is negative or positive. 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 This program takes a number from the user and checks whether that number is either positive or negative or zero. Read -  Programming language Example #1: Check if a Number is Positive or Negative Using if...else #include <stdio.h> int main () { double number ; printf ( "Enter a number: " ); scanf ( "%lf" , & number ); if ( number <= 0.0 ) { if ( number == 0.0 ) printf ( "You entered 0." ); else printf ( "You entered a negative number." ); } else printf ( "You entered a positive number." ); return 0 ; } You can als

C Program to Check Whether a Character is an Alphabet or not

In this example, you will learn to check whether a character entered by the user is an alphabet 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 Example: Program to Check Alphabet #include <stdio.h> int main () { char c ; printf ( "Enter a character: " ); scanf ( "%c" ,& c ); if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' )) printf ( "%c is an alphabet." , c ); else printf ( "%c is not an alphabet." , c ); return 0 ; } Output Enter a character: * * is not an alphabet In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. The ASCII value of lowercase alphabets are from

C Program to Calculate the Sum of Natural Numbers

To compute the sum of natural numbers from 1 to n (entered by the user), loops can be used. You will learn how to use for loop and while loop to solve this problem. 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 The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say  n ) as an input from the user and calculates the sum up to  n . Example #1: Sum of Natural Numbers Using for Loop #include <stdio.h> int main () { int n , i , sum = 0 ; printf ( "Enter a positive integer: " ); scanf ( "%d" ,& n ); for ( i = 1 ; i <= n ; ++ i ) { sum += i ; // sum = sum+i; } printf ( "Sum = %d" , sum ); return 0 ; } The above program takes the input from the user and stores in variabl

C Program to Find Factorial of a Number

The factorial of a positive integer n is equal to 1*2*3*...n. You will learn to calculate the factorial of a number using for loop in this example. To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Operators C if, if...else and Nested if...else Statement C Programming for Loop 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,  0! = 1 Example: Factorial of a Number #include <stdio.h> int main () { int n , i ; unsigned long long factorial = 1 ; printf ( "Enter an integer: " ); scanf ( "%d" ,& n ); // show error if the user enters a negative integer if ( n < 0 ) printf ( "Error! Factorial of a negative number doesn't exist." ); else { for (

C Program to Generate Multiplication Table

Example to generate the multiplication table of a number (entered by the user) using for loop. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C Programming for Loop The program takes an integer input from the user and generates the multiplication table up to 10. Read -  Programming language Example #1: Multiplication Table Up to 10 #include <stdio.h> int main () { int n , i ; printf ( "Enter an integer: " ); scanf ( "%d" ,& n ); for ( i = 1 ; i <= 10 ; ++ i ) { printf ( "%d * %d = %d \n" , n , i , n * i ); } return 0 ; } Output Enter an integer: 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90 Here's a little modification of the above program to generate the multiplication table up to a range (where  ra

C Program to Display Fibonacci Sequence

Example on how to display the Fibonacci sequence of first n numbers (entered by the user) using loop. Also in different example, you learn to generate the Fibonacci sequence up to a certain number. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C Programming while and do...while Loop C Programming for Loop C Programming break and continue Statement The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21 Visit this page to learn about Fibonacci sequence. Read -  Programming language Example #1: Fibonacci Series up to n number of terms #include <stdio.h> int main () { int i , n , t1 = 0 , t2 = 1 , nextTerm ; printf ( "Enter the number of terms: " ); scanf ( "%d" , & n ); pri

C Program to Find GCD of two Numbers

Examples on different ways to calculate GCD of two integers (for both positive and negative integers) using loops and decision making statements. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C Programming for Loop C if, if...else and Nested if...else Statement C Programming while and do...while Loop The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder). Read -  Programming language There are many ways to find the greatest common divisor in C programming. Example #1: GCD Using for loop and if Statement #include <stdio.h> int main () { int n1 , n2 , i , gcd ; printf ( "Enter two integers: " ); scanf ( "%d %d" , & n1 , & n2 ); for ( i = 1 ; i <= n1 && i <= n2 ; ++ i ) { // Checks if i is factor of both integers if ( n1 %