While Loop Example

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define ALLOWED_TIMES 4

void main()
{
    int secret, input=0;
    int times = 1;
                                           
                                                                 /* get a random number between 0 and 10 */
    srand((unsigned int)time(NULL));
    secret = rand() % 10 + 1;

                                                                    /* start the game */
    printf("--- Demonstrate C while loop statement --- \n\n");
    printf("--- Number Guessing Game --- \n");

    while(input!=secret && times<=ALLOWED_TIMES )
    {
        printf("Please enter a number (0-10):\n");
        scanf("%d",&input);

        if(input==secret)
            printf("yipee! you got it.\n");
        else if(input>secret)
            printf("No, it is smaller.\n");
        else
            printf("No, it is bigger.\n");
        times++;
        
    }
    
    if(times==ALLOWED_TIMES)
    {
        printf("You lose! The secret number is %d",secret);
    }

    getch();
}

\\next eg

#include<stdio.h>
#include<conio.h>

void main()
{

int i=1;
clrscr();

while(i<=15)
{
    printf("%d : Shashank Singh and his friends.\n", i);
    i++;
}
getch();
}

\\next eg

#include<stdio.h>
#include<conio.h>

void main()
{

int i;
clrscr();

printf("Value of uninitialized i : %d", i);

while(i<=10)
{
    printf("%d : Shashank and his firends.\n", i);
    i++;
}
getch();
}


\\next eg

\\ endless loop

#include<stdio.h>
#include<conio.h>
 
void main()
{
 
int i;
clrscr();
 
printf("Value of uninitialized i : %d", i);
 
while(i<=10)
{
    printf("%d : Shashank Singh and his friends.\n", i);
    //i++;
}
getch();
}

\\next eg

#include<stdio.h>
#include<conio.h>
 
void main()
{
 int var, sum=0, i=1;
  while(i<=10)
  {
    printf("Enter a number:");
    scanf("%d",&var);
    sum=sum+var;
    i=i+1;
  }
 
prinf("average=%d",sum/10.0);

getch();
}

\\next eg

#include<stdio.h>
#include<conio.h>
void main()
{
    int i=10;

    do
       {
       printf("Hello %d\n", i );
       i=i-1;

       }
    while ( i>0 );
getch();
}

\\next eg

No comments:

Post a Comment