Thursday, 30 July 2015

Alphabet Pattern 1

/*
 * C Program to print following pattern
 *
 *      A B C D E D C B A
 *        A B C D C B A
 *          A B C B A
 *            A B A
 *              A
 *
 */

// Include
#include <stdio.h>

// Declarations
/*
 * printPattern
 * Description : Function to print pattern provided in description.
 * Parameters  : int - number of lines to be printed for given pattern.
 * Return      : void
 */
void printPattern(int i_numberOfLines);

// Definitions
int main()
{
    int i_numberOfLines = 0;
    printf("How many lines would you like to print : ");
    scanf("%d",&i_numberOfLines);
    printPattern(i_numberOfLines);
    return 0;
}

void printPattern(int i_numberOfLines)
{
    register int row, spaces, letters, noOfLetters;
    // Loop to repeat for each row
    for (row = 1; row <= i_numberOfLines; row++)
    {
        // Loop for printing spaces
        // Observe that spaces are incremental on each line.
        // row 1=0, row 2 = 1, row 3 =2 and so on.
        // So essentially number of spaces on each row is 1 less that current row.
        for (spaces = 1; spaces < row; spaces++)
        {
            printf("  ");
        }
        
        // Loop for printing incremental letters A B C D E
        // Observe that number of incremental letters are less on each row.
        // row 1 = 5, row 2 = 4, row 3 = 3 and so on.
        // So number of incremental letters on row n = Total lines - current row + 1
        noOfLetters = i_numberOfLines - row + 1;
        for (letters = 0; letters < noOfLetters; letters++)
        {
            printf("%c ", 'A'+letters);
        }
        
        // Loop for printing decremental letters D C B A
        // Observer that number of decremental letters are less on each row.
        // row 1 = 4, row 2 = 3, row 3  = 2 and so on
        // So number of decremental letters on row n = Total lines - current row.
        noOfLetters = i_numberOfLines - row;
        for (letters = noOfLetters; letters>0; letters--)
        {
            printf("%c ",'A'+letters -1);
        }
        
        // Move on to next line
        printf("\n");
    }

}

Wednesday, 29 July 2015

Print without semicolon

/*
 * C Program to print something without use of semicolon.
 * Trick here is  - control statements like 'if' expects expression in it.
 * The expression can be anything and we can even put printf function instead.
 */

// Include
#include <stdio.h>
int main()
{
    // No semicolon used here.
    if (printf("Gotcha !!!"))
    {
    }

}

Tuesday, 28 July 2015

Solve Quadratic Equation


/*
 * C Program to solve quadratic equation.
 * Quadratic Equation ->  ax^2 + bx + c = 0
 * Given - values of a, b and c.
 * We need to find out values of roots of x.
 * Formula to calculate roots of x is as follows : 
 *        
 *     
 *          
 */

// Include section
#include<stdio.h>
#include<string.h>
#include<math.h>

// Declarations
// Structure to represent roots of a quadratic equation.
typedef struct
{
    float root1;                // quadratic root 1
    float root2;                // quadratic root 2
    int   isImaginary:1;        // to represent imaginary value.
    int   errorCode;            // In case there is any problem - error code.
    char  errorDescription[50]; // Short error description.
} quadRoots;

/*
 * solveQuadraticEq
 * Description : Function to solve quadratic equation.
 * Parameters  : a, b, c values of a quadratic equation.
 * Return      : Returns quadRoots structure containing roots of a quadratic equation.
 */
quadRoots solveQuadraticEq(int a, int b, int c);

// Definitions
int main()
{
int a=0,b=0,c=0;
printf("Enter values of a,b,c in following quadratic equation :\n");
printf("ax^2 + bx + c = 0\n");
scanf("%d %d %d",&a,&b,&c);
    
    // Solve Quadratic Equation
    quadRoots result = solveQuadraticEq(a,b,c);
    if (result.errorCode != 0// Error occurred
    {
        printf("Error : %s\n",result.errorDescription);
    }
    else if (result.isImaginary)
    {
        // Roots are imaginary - 
        // So root 1 contains -b/2a and root 2 contains sqrt(-d)/2a
        printf("Quadratic equation %dx^2%+dx%+d = 0 has complex roots : ",a,b,c);
        printf("[ %.2f%+.2fi, %.2f%+.2fi]\n", result.root1,result.root2,result.root1,-result.root2);
    }
    else
    {
        printf("Roots of Quadratic equation %dx^2%+dx%+d = 0 are : ",a,b,c);
        printf("[ %.2f, %.2f] \n", result.root1, result.root2);
    }
return 0;
}

quadRoots solveQuadraticEq(int a, int b, int c)
{
    // Initialisations
    quadRoots roots = {0,0,0,0,""};
    float discriminant;
    
    // Value of a is 0 - meaning this is not quadratic equation.
    if (0 == a)
    {
        roots.errorCode = 1000;
        strcpy(roots.errorDescription, "Not a quadratic equation");
        return roots;
    }
    
    // discriminant = b^2 - 4*a*c
    discriminant = b*b - 4*a*c;
    
    if (discriminant == 0)      // Both roots are equal
    {
        roots.root1 = roots.root2 = -b/2.0*a;
    }
    else if (discriminant < 0// Roots are imaginary
    {
        roots.root1 = -b/(2.0*a);
        roots.root2 = sqrt(-discriminant)/(2.0*a);
        roots.isImaginary = 1;
    }
    else                        // Roots are real numbers
    {
        roots.root1 = (-b + sqrt(discriminant)) / (2.0*a);
        roots.root2 = (-b - sqrt(discriminant)) / (2.0*a);
    }
    return roots;

}

Saturday, 25 July 2015

Star Pattern Program - 1

/*
 C Program to print following pattern.

     *         *
     * *     * *
     * * * * * *

 */

// Include Section
#include<stdio.h>

// Declarations
/* printPattern
 * Description : This function prints the pattern
 * Param         : int- number of lines to be printed.
 * Return        : void
 */
void printPattern(int);

// Definitions
int main()
{
    int i_numberOfLines = 0;
    printf("Enter how many line you want to print : ");
    scanf("%d",&i_numberOfLines);
    
    printPattern(i_numberOfLines);
}

void printPattern(int i_numberOfLines)
{
    // hItr is horizontal iterator which moves one row at a time.
    for(int hIter=0; hIter < i_numberOfLines; hIter++)
    {
        // for loop to print first number of start.
        // On every line number of stars is equal to current line number.
        for(int starIter = 0; starIter <= hIter; starIter ++)
            printf("*");
        
        // Now we have to print spaces.
        //The max number of stars that can be there is 2*number of lines.
        // On every line we print before and after spaces * equal to current line number.
        // So number of spaces to print = (2*number of lines) - (2*current Line)
        for(int spaceIter = 0; spaceIter < ((2*i_numberOfLines - 2*hIter)-1); spaceIter++)
            printf(" ");
        
        // Now print again * equal to current line number
        for(int starIter = 0; starIter <= hIter; starIter ++)
            printf("*");
        
        // Go to next line
        printf("\n");
    }

}