CSE Rockz Home Blog Links Forum VSF

Tuesday, December 16, 2008

RENEW 1 :

RENEW is like an exensin to basic which gives a whole lot of practice in aready discussed basic posts..this gives a immense practice and workout schedule to 'RENEW' ones capablity in programming with 'c'

1./* wap which accepts the dimensions of a rectangle and displays its perimeter*/



main()
{

float l,b,p;
printf("give the dimensions");
scanf("%f %f",&l,&b);

p = 2*(l+b);

printf("the perimeter is %d",p);


}

dry run :

  • we declared 3 variables l,b,p corresponding to length, breadth and perimeter of the rectangle.
  • we requested user to provide the values of l,b
  • we saved respective values in thier variables l,b;
  • appliying formula perimeter = 2*(length+breadth)
  • printing final value of p
out put:
give the dimensions 5 5
the perimeter is 20

2. /* wap which accepts basic salary of a person , also his expenditure on rent(in percent of his salary),food(in percent of his salary) and other amenities(in percent of his salary) and dedects his gross savings*/


main()
{

float salrary,rent,food,other,saving;
printf("give salary,rent,food,other");
scanf("%f %f %f %f",&salary,&rent,&food,&other);

saving = salary - ((food+rent+other)/100)*salary);

printf("%d is savings",saving);

}

dry run:

  • create all convienient variables
  • take salary,rent,food,and other expenditure in to the respective variables
  • calculate savings by the above formula
  • print the savings
out put
give salary food rent and other - 10000 25 25 10 
4000 is the savings

3./*wap which accepts coefficients of a quadratic equation and prints its roots*/


#include(math.h)
main()
{

int a,b,c;
float r1,r2;
printf("give the coefficients");
scanf("%d %d %d",&a,&b,&c);

r1= -b+sqrt(b*b-4*a*c)/(2*a);
r2= -b-sqrt(b*b-4*a*c)/(2*a);

printf("%f %f",r1,r2);


}

dry run:
  • take integer variables for coefficients be cause they are generally not flaot type
  • take float type for roots as they are generally of float type
  • request values from user
  • read the values into respective variables
  • operate with the formulas and obtain the roots
  • print the roots
4./*wap which accepts coordinates of a point and determines its distance from the origin*/


#include(math.h)
main()
{
float x,y,d;
printf("give coordinates");
scanf("%f %f",&x,&y);

d= sqrt(x*x+y*y);

printf("%f",d);
}


5./*wap to find modulous of a given number*/

#include
main()
{

int x,a;
printf("give n");
scanf("%d",&n);

a=(x>0)?x:-x;

printf("|x| = %d",a);
}

output:

give n -6
|-6| = 6

dryrun:

  • take a variable and request a value
  • take the value into the variable
  • assume a variable that stores a value which is dependent on the condition x>0
  • if condition is true it stores x in it else -x here we use conditional operator.
  • print the resultant variable to get desired result

6./*wap to double a nuimber with out using any arthematic operators*/


main()
{

int a,b;
printf("give a");
scanf("%d",&a);

b=a<<1;>
printf("%d",b);


}

out put:
give n 6
12

dry run
  • the above prog is just an example for leftshift
  • we take a variable,reqest for a value...and read tht value into the variable..
  • then we operate a with leftshift one time .....and name the resultant as b
  • the bits written leniarly ....are shifted one pos to left and resultant is the double of the initial 
  • we at last print the resultant
  • this program is just an example for left shift it does not work with all numbers

7./*wap to demonstate increment and decrement operators*/


main()
{

int a,b,c,d,e,f,g;
printf("give a value of a");
scanf("%d",&a);

b =a++;
c=++a;
d=a++ + ++a
e=++a + a++
f =a++*++a
g=++a*a++

printf("%d %d %d %d %d %d %d ",a,b,c,d,e,f,g);
}



dry run + out put:

  • request the user and read in value of a
  • as per operation of an incerment operator b is a++ ,ie if a is five thaen b will become 5
  • at the end of the above step a will become 6 so as c=++a , first increment is done and then assignment which follows hence, c will become 6+1=7
  • at the end of the above step a is 7 so as d=a++ + ++a; first increment is done so a=7+1=++a,which is added to a,ie d =8+8=16;and at the end a=8+1=9
  • similarly by folloyin all the operation of increment and decrement operaors, we get all the above results

sources:

  • "let us c" by Yashwant Kanethkar
  • "C programming" and data structures by Balaguruswamy

No comments:

Post a Comment