Posts

Showing posts from February 10, 2013

Switch Case

Switch Case Example for Simple Arithmetic Operations The source code illustrating the use Switch Case Example for Simple Arithmetic Operations - Addition, Subtraction, Multiplication and Division are given on this page. Source Code #include <stdio.h>     void main() {       int opcode;       int a, b;       int result;         printf( "Program for Addition, Subtraction, Multiplication and Division\n" );       printf( "Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: " );       scanf( "%d" , &opcode);       printf( "Enter First Number:" );       scanf( "%d" , &a);       printf( "Enter Second Number:" );       scanf( "%d" , &b);         switch (opcode)       {       case 1:             result = a + b;             printf( "%d + %d = %d" , a, b, result);       

Break and Continue statements

Use of break and continue statements break statement is used to come out the loop. continue statement is used to continue the loop but skip the execution of the remaining statements in the loop. These statements are used in any loop statements (for,do while, while) and switch case statements. The sample code given below has infinite for loop. if i >= 60, then break statement will get executed and loop will get terminated. if (i % 2) is false meaning odd number, then the continue statement will skip the remainder of the statement. In this case, eventcount will not get incremented. Source Code void main() {     int evencount = 0, i = 0;     for (i = 0; ; i++)     {         if (i >= 60)             break ; // Terminate the for loop         if ((i % 2) != 0)             continue ; // Will skip the reminder of the for loop         evencount++;     }     prin

Build Pattern

Build Pattern of * and numbers using loops This program will accept a number as input. The loops are used to build a pattern of * and numbers. There are four loops, two for displaying number and other two for displaying pattern of stars. Source Code #include <stdio.h> int main() {     int i,j,n;     printf( "Enter a number: ") ;     scanf( "%d", &n);       printf( "\n") ;       for (i = n; i > 1; i--)     {         for (j = 1; j <= i; j++)             printf( "*") ;         printf( "\n") ;     }     for (i = 1; i <= n; i++)     {         for (j = 1; j <= i; j++)             printf( "*") ;         printf( "\n") ;     }     printf( "\n") ;       for (i = 1; i < n; i++)     {         for (j = 1; j <= i; j++)             printf( "%d", j)

Reverse a String

Reverse a String With Out Using String Library Functions I have used while loop to find out the length of the string and for loop to reverse the string. The function name is ReverseString which does the work for you. Source Code bool ReverseString( const char *src, char *dst, int len) {     const char *p = src;       // count the length     int count = 0;     int i = 0, j = 0, k = 0;     while (1)     {         if (p == NULL || *p == '\0' ||             *p == '\t' || *p == '\n' || *p == '\r' )         {             count = i;             break ;         }         p = p + 1;         i = i + 1;     }       if (count > len)     {         return false ; // Insufficient memory in the destination pointer     }       for (j = count - 1; j >= 0; j--)     {         dst[k++] = src[j];     }     dst[k

Check Odd or Even Number

Loops - Read Numbers and Check Odd or Even Number I have given here the code to read set of integers and display whether odd or even number. Source Code #include <stdio.h> int main() {     printf( "Program to find ODD or Even Number\n") ;     while (1)     {         int n = 0;         printf( "\nEnter a number(-1 for Exit): ") ;         scanf( "%d", &n);           if ( n == -1)             break ;           if ((n % 2) == 0)         {             printf( "%d is a EVEN number.\n", n);         }         else         {             printf( "%d is a ODD number.\n", n);         }     }     return 0; } Output Program to find ODD or Even Number Enter a number(-1 for Exit): 1 1 is a ODD number. Enter a number(-1 for Exit): 2 2 is a EVEN number. Enter a numbe

Creating Pyramid

Creating Pyramid using loops I have given here a simple program withg loops to build a pyramid with *. You can replace * with any other character you like. Source Code #include <stdio.h> int main() {     int i,j,k, n;     printf( "Enter number of rows for pyramid: " );     scanf( "%d" , &n);       printf( "\n" );       for (i = 1; i <= n; i++)     {         for (j = 0; j < (n - i); j++)             printf( " " );         for (j = 1; j <= i; j++)             printf( "*" );         for (k = 1; k < i; k++)             printf( "*" );         printf( "\n" );     }     printf( "\n\n" );       return 0; } Output Enter number of rows pyramid: 21                     *                    ***                   *****              

ELSE IF Ladder

ELSE IF ladder Here is an example for else if ladder in C Programming Language. Else if ladder means, an simple if statement occurs with in the else part of 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 which is explained in if else statement. Nested if statement is explained with finding the biggest of 3 numbers example. Else if ladder is explained with an example given below: Source Code #include <stdio.h> void main () {    int choice ;    printf ( "[1] Add\n" );    printf ( "[2] Edit\n" );    printf ( "[3] Delete\n" );    printf ( "[4] View\n" );    printf ( "[5] Exit\n" );    printf ( "Enter your choice