Posts

Nested IF

Nested IF Statement Here is an example for nested if statement in C Programming Language. Nested if means, an simple if statement occurs with in another if statement. if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }. if statement can be combined with else part which is explained in if else statement. Nested if statement is explained with finding the biggest of 3 numbers example given below. Source Code #include <stdio.h> void main () {    int a , b , c ;    int biggest ;    printf ( "Enter 1st Number: " );    scanf ( "%d" , & a );    printf ( "Enter 2nd Number: " );    scanf ( "%d" , & b );    printf ( "Enter 3rd Number: " );    scanf ( "%d" , & c );    if ( a >

IF ELSE

Simple IF ELSE Statement Here is an example for simple if else statement. if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }. if statement can be combined with else part. Else part will be executed when the if condition is false. Source Code #include <stdio.h> void main () {    int x ;    printf ( "Enter a Number: " );    scanf ( "%d" , & x );    if ( x < 0)       printf ( "%d is a negative number\n" , x );    else       printf ( "%d is a positive number\n" , x ); } Output Enter a Number: -100 -100 is a negative number Enter a Number: 55 55 is a positive number

IF Statement

Simple IF Statement Here is an example for simple if statement. if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }. Source Code #include <stdio.h> void main () {    int x ;    printf ( "Enter a Number: " );    scanf ( "%d" , & x );    if ( x < 0)       printf ( "%d is a negative number\n" , x ); } Output Enter a Number: -100 -100 is a negative number

Display numbers using loops

C Programming  Loops - display numbers using for loop, while loop and do while loop The main difference between for loop, while loop, and do while loop is 1.While loop checks for the condition first. so it may not even enter into the loop, if the condition is false. 2.do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false. 3.for loop is similar to while loop except that •initialization statement, usually the counter variable initialization •a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement. The following sample code that can display numbers explains all 3 different loops: Source Code #include <stdio.h> int main() {       int num = 0;     printf( "Using while loop\n") ;     while (1)  

Fibonacci Series

Image
Fibonacci Series The logic for Fibonacci series very simple. It starts with 0 and 1. The next subsequent term is the sum of the previous two terms. Source Code // Program for Fibonacci Number #include <stdio.h> void main() {       int f1 = 0, f2 = 1, f3, n;       printf( "Program for Fibonacci Series\n" );       printf( "Enter the maximum number for Fibonacci Series: " );       scanf( "%d" , &n);       printf( "\nPrinting Fibonacci Series from 0 - %d\n" , n);       printf( "%d\n%d\n" , f1, f2);       while (1)       {             f3 = f1 + f2;             if (f3 > n)                   break ;             printf( "%d\n" , f3);             f1 = f2;             f2 = f3;       } }   Output Program for Fibonacci Series Enter the maximum number for Fibonacci Series: Printing Fibonacci Series from 0 - 1000 0 1 1 2 3 5 8 13 21 34

Finding Armstrong Number

Armstrong Numbers Program for finding Armstrong number of a three digits number. The condition for Armstrong number is, Sum of the cubes of its digits must equal to the number itself. For example, 407 is given as input. 4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number. Source Code #include <stdio.h> int main() {       int i, a, b, c, d;       printf( "List of Armstrong Numbers between (100 - 999):\n" );       for (i = 100; i <= 999; i++)       {             a = i / 100;             b = (i - a * 100) / 10;             c = (i - a * 100 - b * 10);             d = a*a*a + b*b*b + c*c*c;             if (i == d)             {                   printf( "%d\n" , i);             }       }         return 0; } Output List of Armstrong Numbers between (100 - 999): 153 370 371 407

Calculating Slope of a Line

Calculating Slope of a Line Given Two End Points Program for Calculating Slope of a Line Given Two End Points It uses the following formula given points are (x1,y1) and (x2, y2) slope = (y2 - y1) / (x2 - x1) Source Code #include <stdio.h> #include <math.h> void main() {     float slope;     float x1, y1, x2, y2;     float dx, dy;     printf( "Program to find the slope of a line given two end points\n" );     printf( "Enter X1: " );     scanf( "%f" , &x1);     printf( "Enter Y1: " );     scanf( "%f" , &y1);     printf( "Enter X2: " );     scanf( "%f" , &x2);     printf( "Enter Y2: " );     scanf( "%f" , &y2);     dx = x2 - x1;     dy = y2 - y1;     slope = dy / dx;     printf( "Slope of the line with end points (%.4f