CSE Rockz Home Blog Links Forum VSF

Thursday, December 18, 2008

DAILY:2-CLASS PRACTICE

Class practice is a section similar to the renew section ...but what differs is this section deals with current section not the basic section and contains mostly the practice sessions held in the class...

1./*wap to get 

1
12
123
1234
12345
*/

main()
{
int i,j;

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

printf("\n");
}


}


dry run:
  • analyse the pattern . .. . . .. .before writing the program
  • in the pattern the numbers are printed from 1 to 2,1 to 3 ..... 1-5 if generalized it's 1-n
  • the whole set is repeated 5 times such that n increases from 1-n in each repetition of above series
  • so the pattern is nothing but a repitition of 1-n in general which is inturn repeated n times.
  • so we cunstruct one loop to print 1-n and other to repaet this loop n times.

2./*
wap to print

5
54
543
5432
54321
*/

main()
{

int i,j;

for(i=5;i<=1;i--)
{

for(j=n;j>i;j--)
printf("%d",j);

printf("\n");
}


}

dryrun:
  • this prog is similar to the above program
  • here in the above pattern the numbers are repeated from n-1 in general
  • a loop is cunstructed in order to evaluate the pattern
  • this pattern is repeated n times and an according loop is cunstructed.
  • if i=1;i is less than5 and the loop is enterd if j=5 and j is more than thanor equal to i hence it prints frpm n-i or 5-1 so on .....all the two loops are executed .

3./* wap to print

12345
1234
123
12
1
*/

main()
{

int i,j;

for(i=5;i>0;i--)
{

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

printf("\n");

}





4./*wap to print 

5
45
345
2345
12345

*/

main()
{

int i,j;

for(i=1;i>0;)
{

for(j=i;j<=5;j++)
printf("%d",j);
printf("\n");
}


}

5./*

12345
2345
345
45
5

*/

main()
{

int i,j;

for(i=5;i>0;i++)
{

for(j=i;j<=5;j++)
printf("%d",j);
printf("\n");
}

}


6./*

54321
5432
543
54
5

*/

main()
{

int i,j;

for(i=5;i>0;i++)
{

for(j=5;j>=i;j--)
printf("%d",j);
printf("\n");

}



}

7./*

1
121
12321
1234321
123454321
*/

main()
{

int i,j,k;

for(i=1;i<=5;i++)

{

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

for(k=(i-1);k>0;k++)
printf("%d",k);

printf("\n");
}


}

8./*

123454321
1234321
12321
121
1
*/

main()
{

int i,j,k;

for(i=5;i>0;i--)
{

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

for(k=(i-1);k>0;k++)
printf("%d",k);

printf("\n");


}


}

9./*

5
545
54354
5432543
543215432
*/

main()
{

int i,j;

for(i=5;i<0;i--)>
{

for(j=i;j<=i;j--)
printf("%d",j);

for(k=5;k<=i-1;k--)
printf("%d",k);

printf("\n");


}




}

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