Posts

Showing posts from January, 2021

Swap Two Numbers in C

  Swap Two Numbers in C In this example, you will learn to swap two numbers in C programming using two different techniques. To understand this example, you should have the knowledge of the following C programming topics: C Data Types C Programming Operators C Input Output (I/O) Swap Numbers Using Temporary Variable # include <stdio.h> int main () { double first, second, temp; printf ( "Enter first number: " ); scanf ( "%lf" , &first); printf ( "Enter second number: " ); scanf ( "%lf" , &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf ( "\nAfter swapping, firstNumber = %.2lf\n" , first); printf ( "After swapping, secondNumber = %.2lf" , second); return 0 ; } Output ...

Swap Two Numbers in C

  Swap Two Numbers in C In this example, you will learn to swap two numbers in C programming using two different techniques. To understand this example, you should have the knowledge of the following C programming topics: C Data Types C Programming Operators C Input Output (I/O) Swap Numbers Using Temporary Variable # include <stdio.h> int main () { double first, second, temp; printf ( "Enter first number: " ); scanf ( "%lf" , &first); printf ( "Enter second number: " ); scanf ( "%lf" , &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf ( "\nAfter swapping, firstNumber = %.2lf\n" , first); printf ( "After swapping, secondNumber = %.2lf" , second); return 0 ; } Output ...

Display Prime Numbers Between Two Intervals in C

  Display Prime Numbers Between Two Intervals in C In this example, you will learn to print all prime numbers between two numbers entered by the user. To understand this example, you should have the knowledge of the following C programming topics: C if...else Statement C for Loop C break and continue 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); // iteration until low is not equal to high while (low < high) { flag = 0 ; // ignore numbers less than 2 if (low <= 1 ) { ++low; continue ; } // if low is a non-prime number, flag will be 1 for (i = 2 ; i <= low / 2 ; ++i) { if (low % i == 0 ) { flag = 1 ; break ; } ...

Check Whether a Number is Prime or Not in C

  Check Whether a Number is Prime or Not in C In this example, you will learn to check whether an integer entered by the user is a prime number or not. To understand this example, you should have the knowledge of the following C programming topics: C if...else Statement C for Loop C break and continue A prime number is a positive integer that is divisible only by  1  and itself. For example: 2, 3, 5, 7, 11, 13, 17 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 non-prime if (n % i == 0 ) { flag = 1 ; break ; } } if (n == 1 ) { printf ( "1 is neither prime nor composite." ); } else { if (flag == 0 ) printf ( "%d is a prime number." , n); else printf...

Display Prime Numbers Between Intervals Using Function in C

  Display Prime Numbers Between Intervals Using Function in C In this example, you will learn to print all prime numbers between two numbers (entered by the user). To understand this example, you should have the knowledge of the following C programming topics: C for Loop C break and continue C Functions C User-defined functions To find all the prime numbers between the two integers,  checkPrimeNumber()  is created. This function checks whether a number is prime or not. 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) { // flag will be equal to 1 if i is prime flag = checkPrimeNumber(i); if (flag == 1 ) printf ( "%d " , i); } ...

Find G.C.D Using Recursion in C

  Find G.C.D Using Recursion in C In this example, you will learn to find the GCD (Greatest Common Divisor) of two positive integers entered by the user using recursion. To understand this example, you should have the knowledge of the following C programming topics: C Functions C User-defined functions C Recursion This program takes two positive integers as input from the user and calculates GCD using recursion. Visit this page to learn how you can  calculate the GCD using loops . 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 n1; } Output Enter two positive integers: 366 60 G.C.D of 366 and 60 is 6...

Find LCM of two Numbers in C

  Find LCM of two Numbers in C In this example, you will learn to calculate the LCM (Lowest common multiple) of two numbers entered by the user. To understand this example, you should have the knowledge of the following C programming topics: C Programming Operators C if...else Statement C 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. LCM using while and if # include <stdio.h> int main () { int n1, n2, max; printf ( "Enter two positive integers: " ); scanf ( "%d %d" , &n1, &n2); // maximum number between n1 and n2 is stored in min max = (n1 > n2) ? n1 : n2; while ( 1 ) { if (max % n1 == 0 && max % n2 == 0 ) { printf ( "The LCM of %d and %d is %d." , n1, n2, max); break ; ...