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;
}

Number pattern 2




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

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

Print numbers
For every ith row print numbers.Initialize no = 1.After printing a number,increment no.


#include<stdio.h>

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

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

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

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

        printf("\n");
    }   

    return 0;
}

Number pattern 1

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

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

int main()
{
    int i,j;

    for(i=1;i<=4;i++)
    {
        for(j=i;j>=1;j--)
            printf("%d ",j);

        printf("\n");
    }

    return 0;
}

Friday, 10 June 2016

Print characters for corresponding ascii values

This program prints the characters for corresponding ascii values.
#include<stdio.h>

int main()
{
    int i;
    for(i=0;i<=255;i++)
    {
        printf("\n%c %d",i,i);

        if(i%10==0&&i!=0)
            getch();
    }

        return 0;
}
getch() holds the screen till a character is pressed.

Swap 2 numbers

This can be done without using a temporary variable.Here's the idea
no1 = 3 , no2 = 5
no1 = 8
store 8-5 = 3 in no2
store 8-3 = 5 in no1


Code

#include<stdio.h>

int main()
{
    int no1,no2;

    printf(" Enter the 2 nos ");
    scanf("%d %d",&no1,&no2);

    printf("\n Before swapping no1 = %d , no2 = %d",no1,no2);

    no1 = no1 + no2;

    no2 = no1 - no2;
    no1 = no1 - no2;

    printf("\n After swapping  no1 = %d , no2 = %d",no1,no2);

    return 0;
}


O/P





Thursday, 9 June 2016

Reverse a number

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

1234 % 10 = 4

Code

#include<stdio.h>

int main()
{
    int n,rem,rev_no = 0;

    printf("Enter an integer ");
    scanf("%d",&n);

    while(n != 0)
    {
        rem = n % 10;
        rev_no = rev_no*10 + rem;
        n = n/10;
    }
    printf("Reversed number = %d",rev_no);

    return 0;
}


O/P


Code tracing


Let n = 1234
rem = 1234 % 10 = 4
rev_no = 0*10 + 4 = 4
n = 1234/10 = 123 (n is integer)

rem = 123 % 10 = 3
rev_no = 4*10 + 3 = 43
n = 123/10 = 12

rem = 12 % 10 = 2
rev_no = 43*10 + 2 =432
n = 12/10 = 1

rem = 1 % 10 = 1
rev_no = 432*10 + 1 = 4321
n = 1/10 = 0  (STOP)

Check a string is palindrome without using library functions

Palindrome string is a string which reads the same forward or backward.
eg    LEVEL 

Place i on the start of string and j on the end of the string.Compare the characters.Only if they match we proceed or it is not a palindrome.If they match increment i and decrement j.Repeat the same process.If i equals   i.e all characters matched.So it is a palindrome.

Code

#include<stdio.h>

int main()
{
    char s[20];
    int i,j;

    printf("Enter a string : ");
    scanf("%s",s);

    //Position i and j

    i = 0;
    j = 0;  

    while (s[j] != '\0')
            j++;

    j--;          

    for(;i<j;i++,j--)
    {
        if(s[i] == s[j])
            continue;
        else
        {
            printf("\nNot a palindrome");
            break;
        }
    }
    if(i == j)
        printf("\nPalindrome");

    return 0;
}


O/P



Code Tracing

                         s[]                              

                  i               j                 
                  l   e   v   e   l                  Identical.Inc start,Dec end.


                      i       j
                  l   e   v   e   l                  Identical.Inc start,Dec end.


                         i,j
                  l   e   v   e   l                  i == j.   Palindrome


Smallest number in array

Take 1st element of array as min.Now for all elements in array,if an element is smaller than min then update the min else proceed.Atlast when all elements are traversed you will get the min element.


Code 

#include<stdio.h>

int main()
{
    int n,a[10],min,i;

    printf("Enter no of elements in array ");
    scanf("%d",&n);

    printf("\nEnter elements ");

    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    //calculate smallest element

    min = a[0];

    for(i=0;i<n;i++)
        if(a[i]<min)
            min = a[i];

    printf("\nSmallest element = %d",min);

    return 0;
}


O/P



Code Tracing

i = 0    |   i = 1      |   i = 2      |   i = 3     |   i = 4
min = 9  |   8 < 9 ?    |   6 < 8 ?    |   2 < 6 ?   |   5 < 2 ?
         |   min = 8    |   min = 6    |   min = 2   |   min = 2

Largest number in array

Take 1st element of array as max.Now for all elements in array,if an element is greater than max then update the max else proceed.Atlast when all elements are traversed you will get the max element.


Code 

#include<stdio.h>

int main()
{
    int n,a[10],max,i;

    printf("Enter no of elements in array ");
    scanf("%d",&n);

    printf("\nEnter elements ");

    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    //calculate largest element

    max = a[0];

    for(i=0;i<n;i++)
        if(a[i]>max)
            max = a[i];

    printf("\nLargest element = %d",max);

    return 0;
}


O/P



Code Tracing

i = 0    |   i = 1      |   i = 2      |   i = 3     |   i = 4
max = 5  |   6 > 5 ?    |   8 > 6 ?    |   9 > 8 ?   |   2 > 9 ?
         |   max = 6    |   max = 8    |   max = 9   |   max = 9

Greatest of 3 numbers

If 1st number is greater than the 2nd number,then compare 1st number with 3rd number.Whichever is greater,is greatest of 3 numbers.
If 2nd number is greater than 1st number,compare 2nd number with 3rd number.Whichever is greater,is greatest of 3 numbers.


Code

#include<stdio.h>

int main()
{
    int n1,n2,n3;

    printf("\nEnter 3 numbers ");
    scanf("%d %d %d",&n1,&n2,&n3);

    if(n1 > n2)
    { 
        if(n1 > n3)
            printf("Greatest number = %d",n1);
        else
            printf("Greatest number = %d",n3);
    }
    else
    {
        if(n2 > n3)
            printf("Greatest number = %d",n2);
        else
            printf("Greatest number = %d",n3);
    }       

    return 0;
}

O/P



This can also be done using a ternary operator.
max = (n1-n2) > 0?(n1-n3) > 0?n1:n3:(n2-n3) > 0?n2:n3;

Fibonacci Series

Fibonacci series is a series of numbers in which each number is the sum of preceding 2 numbers.Below is fibonacci series with 6 terms.
0  1  1  2  3  5

Code 

#include<stdio.h>

void fib(int);

int main()
{
    int no;

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

    printf("\n%d %d",0,1);

    fib(no);

    return 0;
}

void fib(int no)
{
    static int a=0,b=1,c;

    if(no>2)
    {
        c = a + b;
        a = b;
        b = c;

        printf(" %d",c);

        fib(no-1);
    }
}
Note : A static variable retains its value within function calls.


Code Tracing


When a function invokes,its activation record is pushed on the call stack and when it finishes execution,its activation record is popped off from the call stack.Activation record comprises of many things but I am just showing the things that help understand recursion.Below I trace the fib().





Print prime numbers in a given range


A prime number is a number divisible by only 1 and itself.

A number would never be divisible by numbers greater than half of itself.Suppose if you need to check whether 17 is a prime number then just check whether 17 is divisible by numbers<= 17/2.Numbers greater than 17/2 i.e greater than 8 would never divide 17 completely as they are greater than half of 17.

This program prints the prime numbers from start to end.


Code

#include<stdio.h>

int main()
{
    int no,i,start,end;

    printf("Enter start and end ");
    scanf("%d %d",&start,&end);

    for(no=start;no<=end;no++)
    {
        i=2;

        while(i<=no/2)
        {
            if(no%i)
            {
                i++;
                continue;
            }
            else
                break;
        }

        if(i==(no/2)+1)
            printf("\n%d",no);
    }

    return 0;
}



O/P




Check given number is prime

A prime number is a number divisible by only 1 and itself.

A number would never be divisible by numbers greater than half of itself.Suppose if you need to check whether 17 is a prime number then just check whether 17 is divisible by numbers<= 17/2.Numbers greater than 17/2 i.e greater than 8 would never divide 17 completely as they are greater than half of 17.


Code 

#include<stdio.h>

int main()
{
    int no,i=2;

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

    while(i<=no/2)
    {
        if(no % i)
        {
            i++;
            continue;
        }
        else
        {
            printf("\nNot a Prime no");
            break;
        }
    }
    if(i == no/2+1)
        printf("\nPrime no");

    return 0;
}


O/P


Check given year is leap year

A century year is a leap year if it is completely divisible by 400.
A non century year is a leap year if it completely divisible by 4.


Code

#include<stdio.h>

int main()
{
    int yr;

    printf(" Enter year ");
    scanf("%d",&yr);

    if(yr % 400 == 0 || yr % 100 != 0 && yr % 4 == 0)
        printf("\n Leap year");
    else
        printf("\n Not a leap year");

    return 0;
}


O/P




This can also be done using ternary operator.
(yr % 400 == 0)?printf("Leap year"):(yr % 4 == 0 && yr % 100 != 0)?printf("Leap year"):printf("Not a leap year");

Check given number is armstrong number

Armstrong number is a number in which sum of the cubes of all digits equals the number.

371 = 3^3 + 7^3 + 1^3


Code

#include<stdio.h>

int main()
{

    int sum=0,rem,no,n;

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

    n = no;

    while(n != 0)
    {
        rem = n % 10;
        sum = sum + rem * rem * rem;
        n = n/10;
    }

    if(no == sum)
        printf("\nArmstrong number");
    else
        printf("\nNot a armstrong number");

        return 0;
}


O/P




Code Tracing


Let no = 371
rem = 371 % 10 = 1
sum = 0 + 1 * 1 * 1 = 1
n = 371/10 = 37

rem = 37 % 10 = 7
sum = 1 + 7 * 7 * 7 = 344
n = 37/10 = 3

rem = 3 % 10 = 3
sum = 344 + 3 * 3 * 3 = 371
n = 3/10 = 0    STOP


Sum of digits of a number

Sum of digits of 1234 = 1 + 2  + 3 + 4 = 10

Code

#include<stdio.h>

int main()
{
     int sum=0,rem,no;

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

     while(no != 0)
     {
         rem = no % 10;
         sum = sum + rem;
         no = no/10;
     }
     printf("\nSum of the digits = %d",sum);

     return 0;
}

O/P




Code Tracing



Let no = 1234
rem = 1234 % 10 = 4
sum = 0 + 4 = 4
n = 1234/10 = 123 (n is integer)

rem = 123 % 10 = 3
sum = 4 + 3 = 7
n = 123/10 = 12

rem = 12 % 10 = 2
sum = 7 + 2 = 9
n = 12/10 = 1

rem = 1 % 10 = 1
sum = 9 + 1 = 10
n = 1/10 = 0  (STOP)

Factorial of a number

Factorial of an integer is product of the number and all integers below it until 1.
5! = 5 * 4! 
   = 5 * 4 * 3!
   = 5 * 4 * 3 * 2!
   = 5 * 4 * 3 * 2 * 1!
   = 5 * 4 * 3 * 2 * 1

Code

#include<stdio.h>

int main()
{
    int fact=1,n,i;

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

    for(i=n;i>=1;i--)
        fact = fact * i;

    printf("\nFactorial of %d = %d",n,fact);

    return 0;
}

O/P



Sum of natural numbers in a given range

This program finds the sum of natural numbers from start to end.
We calculate sum as per the below formula,
        tn = last term , a = 1st term , d = common diff , n = no of terms 

        tn = a + (n-1)d  

        Here d = 1

        n = tn - a + 1

        Sn = n * (a + tn)/2


Code

#include<stdio.h>

int main()
{
    int a,tn,Sn,n;

    printf("\n Enter start ");
    scanf("%d",&a);

    printf("\n Enter end ");
    scanf("%d",&tn);

    n = tn - a + 1;

    Sn = (n * (a + tn))/2;

    printf("\n Sum = %d",Sn);

    return 0;
}


O/P