Posts

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

C Program to Find LCM of two Numbers

Examples on different ways to calculate the LCM (Lowest Common Multiple) of two 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 if, if...else and Nested if...else Statement C Programming while and do...while Loop The LCM of two integers  n1  and  n2  is the smallest positive integer that is perfectly divisible by both  n1  and  n2  (without a remainder). For example: the LCM of 72 and 120 is 360. Read -  Programming language Example #1: LCM using while Loop and if Statement #include <stdio.h> int main () { int n1 , n2 , minMultiple ; printf ( "Enter two positive integers: " ); scanf ( "%d %d" , & n1 , & n2 ); // maximum number between n1 and n2 is stored in minMultiple minMultiple = ( n1 > n2 ) ? n1 : n2 ; // Always tru...

C Program to Display Characters from A to Z Using Loop

You will learn to display the English alphabets using for loop in this example. 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 Example #1: Program to Display English Alphabets #include <stdio.h> int main () { char c ; for ( c = 'A' ; c <= 'Z' ; ++ c ) printf ( "%c " , c ); return 0 ; } Output A B C D E F G H I J K L M N O P Q R S T U V W X Y Z In this program, the for loop is used to display the English alphabets in uppercase. Here's a little modification of the above program to display the English alphabets in either uppercase or lowercase depending upon the input from the user. Read -  Programming language Example #2: Program to Display English Alphabets in Uppercase and Lowercase #include <stdio.h> int main () { ...

C Program to Count Number of Digits in an Integer

Example to count the number of digits in an integer entered by the user. The while loop is used to solve this program. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C Programming while and do...while Loop This program takes an integer from the user and calculates the number of digits. For example: If the user enters 2319, the output of the program will be 4. Read -  Programming language Example #1: Program to Count Number of Digits in an Integer #include <stdio.h> int main () { long long n ; int count = 0 ; printf ( "Enter an integer: " ); scanf ( "%lld" , & n ); while ( n != 0 ) { // n = n/10 n /= 10 ; ++ count ; } printf ( "Number of digits: %d" , count ); } Output Enter an integer: 3452 Number of digits: 4 The integer entered by the user is stored ...