Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

Thursday, 9 May 2013

Sum of 2 numbers without using Arithmetic Operators


DESCRIPTION: This program finds the sum of two numbers without using any of the arithmetic operators. Here we have made use of the looping methods to find the sum of two numbers.


PROGRAM:


#include<stdio.h>
#include<stdlib.h>
void main()
{
    int n1,n2,sum,temp;
    printf("enter the 2 number's\n");
    scanf("%d%d", &n1,&n2);
   sum=n2;
   temp=n1;
   while(temp>0)
   {
       sum++;
       temp--;
   }
   printf("%d+%d=%d",n1,n2,sum);
}


OUTPUT WINDOW:


Pattern of strings-1



DESCRIPTION: This program prints the string pattern as below-
                                                                hello
                                                                  ello
                                                                    llo
                                                                     lo
                                                                      o


PROGRAM:



#include<stdio.h>
main()
{
    char str[50];
    int i,j,n;
    printf("enter the string\n");
    scanf("%s",str);
    for(i=0;str[i]!='\0';i++);
    n=i;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        printf(" ");
        for(j=i;j<n;j++)
        printf("%c",str[j]);
        printf("\n");
    }
}



OUTPUT WINDOW:



Triangle-1



DESCRIPTION: This program prints the triangle as below-
                                                                1
                                                                01
                                                                101
                                                                0101
                                                                10101


PROGRAM:


#include<stdio.h>
main()
{
int i,j,n,a,b;
printf("Enter the number of lines\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    if(a%2==0)
    {
        a=1;
        b=0;
    }
    else
    {
        a=0;
        b=1;
    }
    for(j=0;j<=i;j++)
    {
        if(j%2==0)
        printf("%d",a);
        else
        printf("%d",b);
    }
    printf("\n");
}
}



OUTPUT WINDOW:



Pyramid-3



DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               121
                                                             12321
                                                           1234321


PROGRAM:


#include<stdio.h>
main()
{
    int i,j,k,n,m;
    printf("enter no. of lines\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(m=0;m<n-i;m++)
        printf(" ");
        for(j=0,k=i;j<=i||k!=0;j++)
        {
            if(j<=i)
            printf("%d",j+1);
            else if(k!=0)
            {
            printf("%d",k--);
            }
        }
        printf("\n");
    }
}



OUTPUT WINDOW:



Pyramid-2


DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               1  1
                                                             1  2  1
                                                           1  3  3  1
                                                         1  4  6  4  1


PROGRAM:


#include<stdio.h>
 fact(int x)
{
    int f=1,i;
    for(i=x;i>0;i--)
    f=f*i;
    return f;
}
main()
{
   int i,j,n,k;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        printf(" ");
        for(k=0;k<=i;k++)
        printf("%2d",fact(i)/(fact(k)*fact(i-k)));
        printf("\n");
    }
}



OUTPUT WINDOW:


Pyramid-1


DESCRIPTION: This program prints the pyramid as below-
                                                                 1
                                                               232
                                                             34543
                                                           4567654


PROGRAM:


#include<stdio.h>
main()
{
    int p,j,k,l,n;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(p=0;p<n;p++)
    {
       for(j=0;j<(n-p-1);j++)
       printf(" ");
       for(k=p+1;k<(2*p+2);k++)
       printf("%d",k);
       for(l=2*p;l>p;l--)
       printf("%d",l);
        printf("\n");
    }
 }


OUTPUT WINDOW:


Inverted pyramid-1


DESCRIPTION: To print the inverted pyramid pattern as below-
                                                                 1234321
                                                                   12321
                                                                     121
                                                                       1


PROGRAM:


#include<stdio.h>
main()
{
    int i,j,k,l,n;
    printf("enter the value of n\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=n-i;j<n;j++)
        printf(" ");
        for(k=1;k<=n-i;k++)
        printf("%d",k);
        for(l=n-i-1;l>0;l--)
        printf("%d",l);
        printf("\n");
    }
}


OUTPUT WINDOW: