Thursday 14 March 2013

Write a program to find the G.C.D of two numbers.

Statement:
Write a program to find the GCD of two numbers.



 Algorithm:

            Step-1: Read two numbers a,b
            Step-2: if (a>b)
                        Max=a, Min=b
            Step-3: Else,
                Max=b, Min=a
            Step-4: c=Max%Min
            Step-5: while(c!=0)
                       1.    Max=Min
                       2.    Min=c
                       3.    C=Max%Min
            Step-6: Print Min (GCD)
            Step-7: End
   


Program Listing:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,max,min;
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
if(a>b)
{
max=a;
min=b;
}
else
{
max=b;
min=a;
}
c=max%min;
while(c!=0)
{
max=min;
min=c;
c=max%min;
}
printf("The GCD of given two numbers is=%d", min);
getch();
}



Output:
Enter the two numbers: 56
42
The GCD of given two numbers is= 14


Discussion:
I.    It works properly.
II.    The GCD can be made fo more than two numbers.
III.    Since we use short signed int so it works within the number range  -32768  to +32767
IV.    If we use long int then it works within the number range  -2147483648  to  +2147483647

Write a program to calculate the interest of the loan.

 Statement:
            Write a program to calculate the interest of the loan.



Algorithm:
            Step-1: Read a (Amount of Rs)
            Step-2: Read i (Percentage of interest)
            Step-3: Read y (No of year)
            Step-4: e=a*i*y (Total interest)
            Step-5: t=e + a(Total amount of Rs.)
            Step-6: print e, t
            Step-7: End
       



Program Listing:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a,i,t,y,e;
printf("\nAmount of Rs=");
scanf("%f",&a);
printf("\nPercentage of interest =");
scanf("%f",&i);
printf("\nNo of year=");
scanf("%f",&y);
e=a*i*y/100;
t=e+a;
printf("\nTotal interest=%f",e);
printf("\n\nTotal amount you get after %f year is= %f Rs" ,y,t);
getch();
}




Output:
Amount of Rs=1000
Percentage of interest = 13
No of year=5
Total amount you get after 5 year is= 650 Rs
Total amount you get after 5 year is= 1650 Rs
   
Discussion:
           I.    It works within the number range -32767 to 32767
          II.    If we use long integer then it works in more than range.
         III.    It works in negative number.

Find the Maximum & Minimum between Two Numbers.

Statement:
Find the Maximum & Minimum between Two Numbers.


Algorithm:

            Step-1: Read a
            Step-2: Read b
            Step-3: if (a>b)
            Step-4: print maximum=a & minimum=b
            Step-5: else, print maximum=b & minimum=a
            Step-6: print maximum=b & minimum=a
            Step-7: End
           

Program Listing:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b ,max, min;
clrscr();
printf("Please Enter Two Numbers");
scanf("%d%d", &a, &b);
if(a>b)
{
max=a;
min=b;
}
else
{
max=b;
min=a;
}
printf("\n\n  Max=%d    Min=%d", max, min);
getch();
}



•    Output:
Please Enter Two Numbers 56
42
Max=56    Min=42


  Discussion:
I.    It works properly.
II.    It can be made for more than 10 numbers.
III.    Since we use short signed int so it works within the number range  -32768  to +32767
IV.    If we use long int then it works within the number range                                           -2147483648  to  +2147483647

Determine a seller’s profit or loss and how much



Statement:
If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.



ü           Algorithm:

                                    Step-1: Read a (cost price)
                                    Step-2: Read b (selling price)
                                    Step-3: if(a>b)
1)      Print “LOSS”
2)      c=a-b
3)      The amount loss is  c
                                    Step-4: else
1)      print “PROFIT”
2)      c=b-b
3)      The amount of profit c
                                    Step-5: End
                       


v                        Program Listing:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c;
printf("Enter the cost price=");
scanf("%f",&a);
printf("\nEnter the selling price=");
scanf("%f",&b);
if (a>b)
{
printf("\nSeller incurred LOSS");
c=a-b;
printf("\nThe LOSS is=%f",c);
}
else
{
printf("\nSeller makes PROFIT");
c=b-a;
printf("\nThe PROFIT is=%f",c);
}
getch();
}



·        Output:
Enter the cost price= 102
Enter the selling price= 112
Seller makes PROFIT
The PROFIT is=10


Enter the cost price= 156
Enter the selling price= 114
Seller incurred LOSS
The LOSS is=42
                                   
           
           
§  Discussion:
                                                                                I.            It works within the number range -32767 to 32767
                                                                              II.            If we use long integer then it works in more than range.
                                                                            III.            It works in negative number.

Calculate the multiple of two given numbers.


Statement:
Calculate the multiple of two given numbers.





      Algorithm:

                                                Step 1: Read a 
                                                Step 2: Read b
                                                Step 3: c=a*b
                                                Step 4: print c
                                                Step 5: End





v Program Listing:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Please enter a number:");
scanf("%d",&a);
printf("\nPlease enter another number:");
scanf("%d",&b);
c=a*b;
printf("\nThe multiple of two numbers is:%d",c);
getch();
}



·  Output:
Please enter a number: 42
Please enter another number: 56
The multiple of two number is: 2352





§  Discussion:
                                                I.            It works properly.
                                              II.            We can made the multiple of more than two numbers similarly.
                                            III.            Since we use short signed int so it works within the number range  -32768  to +32767
                                           IV.            If we use long int then it works within the number range   -2147483648  to  +2147483647

Calculate the sum of two given numbers.


Statement:
Calculate the sum of two given numbers.



ü    Algorithm:

                                                Step 1: Read a 
                                                Step 2: Read b
                                                Step 3: c=a+b
                                                Step 4: print c
                                                Step 5: End



v Program Listing:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Please enter a number:");
scanf("%d",&a);
printf("\nPlease enter another number:");
scanf("%d",&b);
c=a+b;
printf("\nThe sum of two number is:%d",c);
getch();
}



·   Output:
Please enter a number: 42
Please enter another number: 56
The sum of two number is:98

§  Discursion:
                                                I.            It works properly.
                                              II.            We can made the sum of more than two numbers similarly.
                                            III.            Since we use short signed int so it works within the number range  -32768  to +32767
                                           IV.            If we use long int then it works within the number range                    -2147483648  to  +2147483647