Skip to main content

Posts

C Program to Check Whether a Number is Positive or Negative

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...else Statement This program takes a number from the user and checks whether that number is either positive or negative or zero. 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 num
Recent posts

C Program to Check Leap Year

C Program to Check Leap Year This program checks whether an year (integer) entered by the user is a leap year or not.   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. Example: Program to Check Leap Year #include <stdio.h> int main () { int year ; printf ( "Enter a year: " ); scanf ( "%d" ,& year ); if ( year % 4 == 0 ) { if ( year % 100 == 0 ) { // year is divisible by 400, hence the year is a leap year if ( year % 400 == 0 ) printf ( "%d is a leap year." , year ); else printf ( "%d i

C Program to Find all Roots of a Quadratic Equation

C Program to Find all Roots of a Quadratic Equation This program accepts coefficients of a quadratic equation from the user and displays the roots (both real and complex roots depending upon the discriminant ).   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement The standard form of a quadratic equation is: ax 2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0 The term b 2 -4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots. If discriminant is greater than 0, the roots are real and different. If discriminant is equal to 0, the roots are real and equal. If discriminant is less than 0, the roots are complex and different. Example: Program to Find Roots of a Quadratic Equation #include <stdio.h> #include <math.h> int main () {

C Program to Find the Largest Number Among Three Numbers

C Program to Find the Largest Number Among Three Numbers In this example, the largest number among three numbers (entered by the user) is found using three different methods.   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement This program uses only if statement to find the largest number. Example #1 #include <stdio.h> int main () { double n1 , n2 , n3 ; printf ( "Enter three different numbers: " ); scanf ( "%lf %lf %lf" , & n1 , & n2 , & n3 ); if ( n1 >= n2 && n1 >= n3 ) printf ( "%.2f is the largest number." , n1 ); if ( n2 >= n1 && n2 >= n3 ) printf ( "%.2f is the largest number." , n2 ); if ( n3 >= n1 && n3 >= n2 ) printf ( "%.2f is the

C Program to Check Whether a Character is Vowel or Consonant

C Program to Check Whether a Character is Vowel or Consonant In this example, if...else statement is used to check whether an alphabet entered by the user is a vowel or a constant.   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement C Programming while and do...while Loop The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants. This program assumes that the user will always enter an alphabet character. Example #1: Program to Check Vowel or consonant #include <stdio.h> int main () { char c ; int isLowercaseVowel , isUppercaseVowel ; printf ( "Enter an alphabet: " ); scanf ( "%c" ,& c ); // evaluates to 1 (true) if c is a lowercase vowel isLowercaseVowel = ( c == 'a' ||

C Program to Check Whether a Number is Even or Odd

C Program to Check Whether a Number is Even or Odd In this example, if...else statement is used to check whether a number entered by the user is even or odd.   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24 An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15 Example #1: Program to Check Even or Odd #include <stdio.h> int main () { int number ; printf ( "Enter an integer: " ); scanf ( "%d" , & number ); // True if the number is perfectly divisible by 2 if ( number % 2 == 0 ) printf ( "%d is even." , number ); else printf ( "%d is odd." , number ); return 0 ; } Output Enter an integer:

C Program to Swap Two Numbers

C Program to Swap Two Numbers This example contains two different techniques to swap numbers in C programming. The first program uses temporary variable to swap numbers, whereas the second program doesn't use temporary variables.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Operators C Input Output (I/O) Example 1: Program to Swap Numbers Using Temporary Variable #include <stdio.h> int main () { double firstNumber , secondNumber , temporaryVariable ; printf ( "Enter first number: " ); scanf ( "%lf" , & firstNumber ); printf ( "Enter second number: " ); scanf ( "%lf" ,& secondNumber ); // Value of firstNumber is assigned to temporaryVariable temporaryVariable = firstNumber ; // Value of secondNumber