Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Saturday, September 17, 2011

C-44 : Check your Progress -1

Refer Sum of 1 to 100 numbers


1 : State the output of the below given programs :



 (a)
##include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int j=5;
            for (printf("Will this loop be entered\n");j<2;j=1)
            printf("Print this,if the loop is entered...");
            printf("Was the loop entered\n");
            getch();
            }

Answer :
Will this loop be entered
Was the loop entered
==================================================================================
 (b)

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int j=5;
            for (printf("Will this loop be entered\n");j<6;j=7)
            printf("Print this,if the loop is entered...j=%d\n",j);
            printf("Is the Boolean j 6 still true?\n%s",j<6?"Yes,":"No,");
            printf("Because j is now %d\n",j);
            getch();
            }



Answer :
Will this loop be entered
Print this,if the loop is entered...j=5
Is the Boolean j 6 still true?
No,Because j is now 7
==================================================================================
 (c)

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int j;
            for(j=0;j<2;j++)
              {
              printf("The for(;;)loop is really quite easy to use.\n");
              printf("I\'ve said this %s.\n",j==0?"once":"twice");
              }
            printf("What I say %d times must be true!\n",j);
            getch();
            }

Answer :
The for(;;)loop is really quite easy to use.
\'ve said this once
The for(;;)loop is really quite easy to use.
\'ve said this twice
What I say 2 times must be true!
==================================================================================
2 : Replace the for(;;) statement in the program 1(a) above by :


for (i=1;sum=0;i<101;i++)
Realize that the comma operator can be used with the expressions controlling a for(;;) statement. That in fact is its most common usage.


Observe that the value of the loop index is retained on exit from the loop.

==================================================================================
3 : In Program 1(c) above, can the && operator be replaced by the || operator ?
Can it be replaced by the comma operator ?
Are the parentheses around sum+=i++ required ?


==================================================================================
4 :



(Will continue...)


===========================================================
Back to Home   &  C

C-43 : Sum of 1 to 100 numbers

Refer  Find Sum , Sum of n even numbers using user defined fun... 


1st Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101;i++)
                        sum=sum+i;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }

2nd Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101;sum+=i++)
            ;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }


3rd Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101 && (sum+=i++);)
            ;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }



Output : 


The sum of the numbers from 1 to 100 is 5050
=================================================================
Back to Home   &  C