Archive | September, 2012

The change return program

21 Sep

Change Return Program – The user enters a cost and then the amount of money given. The program will figure out the change in Rs notes of 1000,500,100,50,10,1’s and paise.

#include<stdio.h>

void change(int,int,int,int,int,int,double);

int main()

{

      double amt,cash,bal;

      int th=0,fhd=0,hd=0,fifty=0,tens=0,ones=0;int notes,cnt=0,n1,i;

       printf(“enter the amount\n”);

       scanf(“%lf”,&amt);

       printf(“enter the cash recieved\n”);

       scanf(“%lf”,&cash);

      bal=cash-amt;

      printf(“change=%lf\n”,bal);

      notes=bal;

      bal=bal-notes;

     while(notes!=0)

     {

             n1=notes%10;

            if(cnt==0)

          {

                  i=0;

                  while(n1!=i)

                           i++;

                  ones=i;

        }

        elseif(cnt==1)

       {

              i=0;

              while(n1!=i)

              i++;

             if(i>=5 && i<=9)

            {

                  fifty++;

                  tens=i-5;

            }

           else

                tens=i;

         }

        elseif(cnt==2)

       {

            i=0;

            while(n1!=i)

                       i++;

           if(i>=5 && i<=9)

         {

                  fhd++;

              hd=i-5;

          }

        else

          hd=i;

        }

        elseif(cnt==3)

       {

            i=0;

             while(n1!=i)

                     i++;

             th=i;

        }

    notes=notes/10;

   cnt++;

 

  }

     change(th,fhd,hd,fifty,tens,ones,bal);

    return 0;

}

void change(int t,int fh,int hd,int fifty,int tens,int ones,doublep)

{

        if(t)

        printf(“1000 rs – %d\n”,t);if(fh)

        printf(“\t500 rs – %d\n”,fh);if(hd)

        printf(“\t100 rs – %d\n”,hd);if(fifty)

         printf(“\t50 rs – %d\n”,fifty);if(tens)

       printf(“\t10 rs – %d\n”,tens);if(ones)

        printf(“\t1 rupee – %d\n”,ones);if(p)

       printf(“\tpaise – %lf\n”,p);

}

C Puzzle1

20 Sep

Program:

Input

Enter a number

5

Output

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

This program can be solved in two steps

1.Figure out how to increment the numbers with every increasing row.

2.Create a mirror image.

By using plain for loops,this program can be solved.

#include<stdio.h>
int main()
{

int num,r,c;
printf(“enter the number\n”);
scanf(“%d”,&num);
for(r=0;r<=num;r++)  // to keep track of each row
{
for(c=1;c<=r;c++)
{
printf(“%d\t”,c);   //to print the numbers in each row
}

printf(“\n”);
}

// to create the mirror image
for(r=1;r<=num-1;r++)
{
for(c=1;c<=num-r;c++)
{
printf(“%d\t”,c);
}

printf(“\n”);
}
return 0;
}

Hope you enjoy coding 🙂