Posts

C Program to Check Whether a Number is Positive or Negative

Image
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 also solve this problem using nested if else statement. Example #2: Check if a Number is Positive or Negative Using Nested if...else #include <stdio.h> int main () { double number ; printf ( "Enter a number: " ); scanf ( "%lf" , & number ); // true if number is less than 0 if ( number < 0.0 ) printf ( "You entered a negative number." );

C Program to Check Leap Year

Image
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 is not a leap year." , year ); } else printf ( "%d is a leap year." , year ); } else printf ( "%d is not a leap year." , year ); return 0 ; } Output 1 For Technical Related Posts Please Visit  http://codetherush.in   Enter a year: 1900 1900 is not a leap year. Output 2 Enter a year: 2012 2012 is a leap year.

C program to Find all Roots of a Quadratic equation

Image
Program to Find Roots of a Quadratic Equation #include <stdio.h> #include <math.h> int main () { double a , b , c , determinant , root1 , root2 , realPart , imaginaryPart ; printf ( "Enter coefficients a, b and c: " ); scanf ( "%lf %lf %lf" ,& a , & b , & c ); determinant = b * b - 4 * a * c ; // condition for real and different roots if ( determinant > 0 ) { // sqrt() function returns square root root1 = (- b + sqrt ( determinant ))/( 2 * a ); root2 = (- b - sqrt ( determinant ))/( 2 * a ); printf ( "root1 = %.2lf and root2 = %.2lf" , root1 , root2 ); } //condition for real and equal roots else if ( determinant == 0 ) { root1 = root2 = - b /( 2 * a ); printf ( "root1 = root2 = %.2lf;" , root1 ); } // if roots are not real else { realPart =

C Program to Find the Largest Number Among Three Numbers

Image
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 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 largest number." , n3 ); return 0 ; } This program uses if...else statement to find the largest number. Example #2 #include <stdio.h> int main () { double n1 , n2 , n3 ; printf ( "Enter three numbers: " ); scanf ( "%lf %lf %lf" , & n1 , & n2 , & n3 ); if ( n1 >= n2 ) { if ( n1 >

C Program to Check Whether a Character is Vowel or Consonant

Image
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 == 'e' || c == 'i' || c == 'o' || c == 'u' ); // evaluates to 1 (true) if c is an uppercase vowel isUppercaseVowel = ( c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true if ( isLowercaseVowel || isUppercaseVowel ) printf ( "%c is a vowel." , c ); else printf ( "%c is a consonant." , c ); return 0 ; } OutPut Enter an alphabet: G G is a consonant.

C Program to Check Whether a Number is Even or Odd

Image
#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: -7 -7 is odd. #2: Program to Check Odd or Even Using Conditional Operator #include <stdio.h> int main () { int number ; printf ( "Enter an integer: " ); scanf ( "%d" , & number ); ( number % 2 == 0 ) ? printf ( "%d is even." , number ) : printf ( "%d is odd." , number ); return 0 ; }

C Program to Swap Two Numbers

Image
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 is assigned to firstNumber firstNumber = secondNumber ; // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber secondNumber = temporaryVariable ; printf ( "\nAfter swapping, firstNumber = %.2lf\n" , firstNumber ); printf ( "After swapping, secondNumber = %.2lf" , secondNumber ); return 0 ; } OutPut For Technical Related Posts Please Visit  ht

C Program to Demonstrate the Working of Keyword long

Image
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 double = %ld bytes\n" , sizeof ( f )); return 0 ; } OutPut Size of int = 4 bytes Size of long = 8 bytes Size of long long = 8 bytes Size of double = 8 bytes Size of long double = 16 bytes

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

Image
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 )); return 0 ; } OutPut Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of char: 1 byte