Monday, 13 June 2016

Number pattern 3



Firstly we will take a for loop(outer) for 4 rows.In the outer for loop we need 3 for loops(spaces,i...2*i-1,2*i-1...i)
Print spaces
For every ith row , print n-i spaces.

Print numbers i....2*i-1
For every ith row , print numbers from i to 2*i-1.


Print numbers 2*i-1....i
For every ith row , print numbers from 2*i-1 to i.
#include<stdio.h>

int main()
{
    int i,j,k;

    for(i=1;i<=4;i++)
    {
        for(j=i;j<4;j++)
            printf(" ");

        j = 2*i-1;

        for(k=i;k<=j;k++)
            printf("%d",k);

        for(k=j-1;k>=i;k--)
            printf("%d",k);

        printf("\n");
    }

    return 0;
}


Star Diamond


Firstly we will take a for loop(outer) for no of rows (n).

For the 1st (n/2 + 1) rows

Print spaces
For every ith row print (n/2) - i spaces.

Print stars
For every ith row print i + 1 stars.


For the last n/2 rows

Print spaces
For every ith row print i spaces.

Print stars
For every ith row print (n/2) - i + 1 stars.

#include<stdio.h>

int main()
{
    int i,j,k,n;

    printf("Enter number of rows ");
    scanf("%d",&n);

    for(i=0;i<=n/2;i++)
    {
        for(j=i;j<n/2;j++)
            printf(" ");

        for(j=0;j<=i;j++)
            printf("* ");

        printf("\n");
    }

    for(i=1;i<=n/2;i++)
    {
        for(j=1;j<=i;j++)
            printf(" ");

        for(j=i;j<=n/2;j++)
            printf("* ");

        printf("\n");
    }

    return 0;
}

Saturday, 11 June 2016

Find number is odd or even

An odd number when divided by 2 gives remainder as 1 and an even number when divided by 2 gives remainder as 0.
#include<stdio.h>

int main()
{
    int no;

    printf(" Enter a no" );
    scanf("%d",&no);

    if(no % 2)                 
    printf("\n Odd number);

    else
    printf("\n Even number");

    return 0;
}
n1 % n2 = r
Mod operator(%) gives the remainder r when n1 is divided by n2.

Pascal's triangle



Let no = number of rows

Print spaces

For every nth row , print no - n - 1 spaces.

Print numbers

You can find any term in Pascal's triangle by using the below formula
            n!
C(n,r) = ----------       n = row number , r = term number in that row
          r! (n-r)!
If you recall your school maths ,this is the formula for combinations(no of ways of selecting r items from n items).

! stands for factorial.Factorial of an integer is product of the number and all integers below it until 1.
Factorial of 5 = 5 * 4 * 3 * 2 * 1 = 120
#include<stdio.h>

int factorial(int);

int main()
{
    int n,r,no,term;

    printf("Enter no of rows ");
    scanf("%d",&no);

    for(n=0;n<no;n++)
    {
          printf("\n");

          for(r=n;r<no-1;r++)
                printf(" ");

          for(r=0;r<=n;r++)
          {
                term = factorial(n) / (factorial(r) * factorial(n-r));
                printf("%d ",term);
          }

    }

    return 0;
}

int factorial(int n)
{
    int fact=1;

    while(n>=1)
    {
         fact = fact * n;
         n--;
    }
    return fact;
}

Floyd's Triangle




#include<stdio.h>

int main()
{
    int i,j,n,no=1;

    printf("Enter no of rows ");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
         printf("\n");

         for(j=1;j<=i;j++)
         {
             printf("%d ",no);
             no++;
         }
    }

    return 0;
}

Star triangle



Let n = number of rows

Firstly we will take a for loop(outer) for 3 rows.
In the outer for loop,we need 2 for loops(one for spaces and other for stars).

Print spaces
For every ith row , print n-i spaces.
Row 1 3 - 1 = 2
Row 2 3 - 2 = 1
Row 3 3 - 3 = 0

Print stars
For every ith row , print 2*i-1 stars.
Row 1 2*1-1 = 1
Row 2 2*2-1 = 3
Row 3 2*3-1 = 5
#include<stdio.h>

int main()
{
    int i,j;

    for(i=1;i<=3;i++)
    {
        for(j=i;j<3;j++)
            printf(" ");

        for(j=1;j<=2*i-1;j++)
            printf("*");

        printf("\n");
    }

    return 0;
}

Letter pattern


Firstly we will take a for loop(outer) for no of rows.

Print spaces
For every ith row , print n-i spaces.

Print letters
For every ith row , print i letters.Also remember letters are printed in descending order.
#include<stdio.h>

int main()
{
    int i,j,n;

    printf("Enter number of rows ");
    scanf("%d",&n);

    for(i=65;i<=68;i++)
    {
        for(j=i;j<68;j++)
             printf(" "); 

        for(j=i;j>=65;j--)
             printf("%c",j);

        printf("\n");
    }   

    return 0;
}