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

Thursday, September 1, 2011

C-42 : Read values from keyboard for calculations

Read values from keyboard for calculations . And see how scanf() works.

Refer Simple Calculations also.



#include<stdio.h>
#include<conio.h>
main()
     {
      clrscr();
      int x,y,z;
      printf("Enter a value of x:");
      scanf("%d",&x);
      printf("Enter a value for y:");
      scanf("%d",&y);
      z=x*y;
      printf("z=x*y=%d\n",z);
      getch();
      return 0;
     }


Output :

Enter a value of x:
3Enter a value of y: 4
z=x*y=12
=========================================================================
Back to Home   &  C


C-41: Simple Calculations

Elementary operations with small integers:

Refer Find Sum also.



#include<stdio.h>
#include<conio.h>
main()
      {
      clrscr();
      printf("\n\t\n\t");
      printf("x=%d,y=%d\n",x,y);
      z=x-y;
      printf("z=x-y=%d\n",z);
      z=x*y;
      printf("z=x*y=%d\n",z);
      z=x/y;
      printf("z=x/y=%d\n",z);
      z=y/x;
      printf("z=y/x=%d\n",z);
      z=x%y;
      printf("z=x%%y=%d\n",z);
      z=y%x;
      printf("z=y%%x=%d\n",z);
      getch();
      }



Output :x=5,y=7
z=x-y=-2

z=x*y=35
z=x/y=0
z=y/x=1
z=x%y=5
z=y%x=2
=========================================================================
Back to Home   &  C


C-40 : Check your Progress -1



Refer

  1.  My first C Program 
  2.  Example for Nested IF
  3. Example of Escape Sequence (\n)
Then , try to answer the following questions :

  1. Do you think comments can be nested , that is , can you have a comment with in a comment? Write a program with a nested comment and see if you can compile it.
  2. Write a C language program that gives for its output :    \* This is a C Comment*\
  3. Debug the following program :


#include<stdio.h>
#include<conio.h>
void main{}
(
            print("print this\m"\*very easy*\).
)

========================================================================
(Please use Comment section to post your answer).
===================================================================== === Back to Home   &  C