0% found this document useful (0 votes)
13 views

Prog 5

The document provides code examples to illustrate various C programming concepts: 1) An enumerated data type example that defines states and prints the current state. 2) A macros example that defines PI and calculates circle area. 3) A typedef example that defines and prints an employee struct. 4) A goto statement example that calculates a factorial using a goto loop. 5) A break statement example that calculates a sum until a negative number is entered.

Uploaded by

Anuja Sovani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Prog 5

The document provides code examples to illustrate various C programming concepts: 1) An enumerated data type example that defines states and prints the current state. 2) A macros example that defines PI and calculates circle area. 3) A typedef example that defines and prints an employee struct. 4) A goto statement example that calculates a factorial using a goto loop. 5) A break statement example that calculates a sum until a negative number is entered.

Uploaded by

Anuja Sovani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 8: Write an interactive C program for each to

illustrate the following concepts: (a)Enumerated data type


(b) Macros in C (c) typedef (d) Goto statement (e) Break
statement.
a) enumerated data type

#include<stdio.h>
enum state {FAILED=0, WORKING};
enum state currentState=1;
enum state FindState()
{
return currentState;
}
void main()
{
if(FindState() == WORKING)
printf(“WORKING”);
else
printf(“FAILED”);
}

b) macros in C

#include<stdio.h>
#define PI 3.14
void main()
{
float radius, area;
printf(“Enter the radius…”);
scanf(“%f”,&radius);
area=PI*radius*radius;
printf(“Circle Area = %f”,area);
}

c) typedef

#include<stdio.h>

struct employee

int id;

char *name;

int salary;

};

typedef struct employee emp;

void main()

emp e;

e.id=1;

e.name=”Debabrata”;

e.salary=10000;
printf(“%d\t%s\t%d”,e.id,e.name,e.salary);

d) goto statement

//Factorial Calculation
#include<stdio.h>
void main()
{
int n,i=1,fact=1;
printf(“Enter the number…”);
scanf(“%d”,&n);
start:
fact=fact*i;
i++;
if(i <= n)
goto start;
printf(“Answer = %d”,fact);
}

e) break statement

/* Calculate the sum of 10 numbers */


/* If negative number is entered, loop terminates */

#include<stdio.h>
void main()
{
int i;
double n,sum=0.0;
for(i=1;i<=10;i++)
{
printf(“Enter the number…”);
scanf(“%lf”,&n);
if(n < 0.0)
{
break;
}
sum=sum+n;
}
printf(“Answer = %lf”,sum);
}

You might also like