Tuesday, December 30, 2014

Go To Statements C Program



Program1

#include
#include
void main()
{
int n,sum=0,ans;
clrscr();
          read:
          printf("Enter Number");
          scanf("%d",&n);
          sum=sum+n;
          printf("\n Will you give another number (press 1 for yes / 0 for No) ");
          scanf("%d",&ans);
          if(ans==1)
                   {
                   goto read;
                   }
          else
                   {
                   printf("\n Sum of given numbers:%d",sum);
                   }
getch();
}

Output
          Enter number
          30
          Will you give another number (press 1 for yes / 0 for No)
          1
          Enter number
          20
          Will you give another number (press 1 for yes / 0 for No)
          0
          Sum of given numbers : 50

Program 2
         
          #include
          #include
          void main()
          {
          int n,x,sum=0,ans;
          float avg;
          clrscr();
          printf(" How many numbers have you enter?\n");
          scanf("%d",&n);
          if(n==0)
          {
          goto end;
          }
          else
          {
          read:
          printf("Enter a number\n");
          scanf("%d",&x);
          sum=sum+x;
          avg=sum/n;
          printf("\n Will you give another number? Press(1/0) ");
          scanf("%d",&ans);
          goto read;
          }
          printf(" Sum of Numbers        :%d", sum);
          printf(" Avg of numbers          : %d", avg);
          end:
getch();
}

Output
           
          How many numbers have you enter?
          2
          Will you give another number? Press (1/0)
          1
          Enter a number
          25
          Will you another (1/0)
          0
          Avg of numbers   : 30.000000
          Sum of numbers   :  60.000000

Program 3

#include
#include
void main()
{
char name[10];
int m1,m2,m3,tot,avg,x;
clrscr();
          read:
                   printf("Enter the name\n");
                   scanf("%s",name);
                   printf(" Enter the m1,m2,m3\n");
                   scanf("%d%d%d",&m1,&m2,&m3);
                   tot=m1+m2+m3;
                   avg=tot/3;
                   printf("\n Total     :%d",tot);
                   printf("\n Avg      :%d",avg);
                   printf("\n Do you want to enter another person……..?");
                   printf("\n Press 1 for yes\n");
                   scanf("%s",&x);
                   if(x==’n’)
                   {
                             goto end;
                   }
                   elseif (x= = ‘y’)
                   {
                             goto read;
                   }
                   else
                             {
                             printf("\n Sorry enter only \n Thank u……….");
                             }
                             end:
          getch();
          }

Output

Enter the name
Raja
Enter the m1,m2,m3
45
55
80
Total :180
Avg   :60
Do you want to enter another person……..?
Yes / No
2
Sorry enter only y/n Thank U…….

No comments:

Post a Comment