Skip to main content

Posts

Showing posts from January, 2019

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

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

C Program to Demonstrate the Working of Keyword long

C Program to Demonstrate the Working of Keyword long The long is a size modifier, indicated by keyword long, that may increase the size of a variable during declaration. This program will demonstrate the working of long keyword.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) Example: Program to Demonstrate the Working of long #include <stdio.h> int main () { int a ; long b ; long long c ; double e ; long double f ; printf ( "Size of int = %ld bytes \n" , sizeof ( a )); printf ( "Size of long = %ld bytes\n" , sizeof ( b )); printf ( "Size of long long = %ld bytes\n" , sizeof ( c )); printf ( "Size of double = %ld bytes\n" , sizeof ( e )); printf ( "Size of long doub

C Program to Find the Size of int, float, double and char

C Program to Find the Size of int, float, double and char This program declares 4 variables of type int, float, double and char. Then, the size of each variable is evaluated using sizeof operator.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) Example: Program to Find the Size of a variable #include <stdio.h> int main () { int integerType ; float floatType ; double doubleType ; char charType ; // Sizeof operator is used to evaluate the size of a variable printf ( "Size of int: %ld bytes\n" , sizeof ( integerType )); printf ( "Size of float: %ld bytes\n" , sizeof ( floatType )); printf ( "Size of double: %ld bytes\n" , sizeof ( doubleType )); printf ( "Size of char: %ld byte\n" , sizeof ( charType ));

C Program to Compute Quotient and Remainder

C Program to Compute Quotient and Remainder This program evaluates the quotient and remainder when an integer is divided by another integer.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) C Programming Operators Program to Compute Quotient and Remainder #include <stdio.h> int main (){ int dividend , divisor , quotient , remainder ; printf ( "Enter dividend: " ); scanf ( "%d" , & dividend ); printf ( "Enter divisor: " ); scanf ( "%d" , & divisor ); // Computes quotient quotient = dividend / divisor ; // Computes remainder remainder = dividend % divisor ; printf ( "Quotient = %d\n" , quotient ); printf ( "Remainder = %d" , remainder ); return 0 ; }

C Program to Find ASCII Value of a Character

C Program to Find ASCII Value of a Character In C programming, a character variable holds ASCII value (an integer number between 0 an 127) rather than character itself. You will learn how to find ASCII value of a character in this program.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) A character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself in C programming. That value is known as ASCII value. For example , ASCII value of 'A' is 65. What this means is that, if you assign 'A' to a character variable, 65 is stored in that variable rather than 'A' itself. Program to Print ASCII Value #include <stdio.h> int main () { char c ; printf ( "Enter a character: " ); // Re

C Program to Multiply two Floating Point Numbers

C Program to Multiply two Floating Point Numbers In this program, user is asked to enter two numbers (floating point numbers). Then, the product of those two numbers is stored in a variable and displayed on the screen.   To understand this example, you should have the knowledge of following C programming topics: C Programming Constants and Variables C Programming Data Types C Input Output (I/O) C Programming Operators Program to Multiply Two Numbers #include <stdio.h> int main () { double firstNumber , secondNumber , product ; printf ( "Enter two numbers: " ); // Stores two floating point numbers in variable firstNumber and secondNumber respectively scanf ( "%lf %lf" , & firstNumber , & secondNumber ); // Performs multiplication and stores the result in variable productOfTwoNumbers product = firstNumber * secondNumber ; //

C Program to Add Two Integers

C Program to Add Two Integers In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) C Programming Operators Program to Add Two Integers #include <stdio.h> int main () { int firstNumber , secondNumber , sumOfTwoNumbers ; printf ( "Enter two integers: " ); // Two integers entered by user is stored using scanf() function scanf ( "%d %d" , & firstNumber , & secondNumber ); // sum of two numbers in stored in variable sumOfTwoNumbers sumOfTwoNumbers = firstNumber + secondNumber ; // Displays sum printf ( "%d + %d = %d" , firstNumber , secondNum

C Program to Print an Integer (Entered by the User)

C Program to Print an Integer (Entered by the User) In this program, integer entered by the user is stored in a variable. Then, that variable is displayed on the screen using printf() function.   To understand this example, you should have the knowledge of following C programming topics: C Programming Constants and Variables C Programming Data Types C Input Output (I/O) Program to Print an Integer #include <stdio.h> int main () { int number ; // printf() dislpays the formatted output printf ( "Enter an integer: " ); // scanf() reads the formatted input and stores them scanf ( "%d" , & number ); // printf() displays the formatted output printf ( "You entered: %d" , number ); return 0 ; } Output Enter a integer: 25 You entered: 25 In this program, an integer variable  number is declared.

C "Hello, World!" Program

C "Hello, World!" Program A simple C program to display "Hello, World!" on the screen. Since, it's a very simple program, it is often used to illustrate the syntax of a programming language.   To understand this example, you should have the knowledge of following C programming topics: C Input Output (I/O) Program to Display "Hello, World!" #include <stdio.h> int main () { // printf() displays the string inside quotation printf ( "Hello, World!" ); return 0 ; } Output Hello, World! How "Hello, World!" program works? The #include <stdio.h> is a preprocessor command. This command tells compiler to include the contents of stdio.h (standard input and output) file in the program. The  stdio.h  file contains functions such as scanf() and print() to take input and display output respectively. If you use printf() function

C Program to Check Whether a Number is Palindrome or Not

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...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. 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 = reversed

C Program to Find Factorial of a Number Using Recursion

C Program to Find Factorial of a Number Using Recursion Example to find factorial of a non-negative integer (entered by the user) using recursion. To understand this example, you should have the knowledge of following C programming topics: C Programming Functions C User-defined functions C Recursion 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. You will learn to find the factorial of a number using recursion in this example. Visit this page to learn, how you can find the  factorial of a number using loop . Example: Factorial of a Number Using Recursion #include <stdio.h> long int multiplyNumbers ( int n ); int main () { int n ; printf ( "Enter a positive integer: " ); scanf ( "%d" , & n ); printf ( "Factorial of %d = %ld