Birla Institute of Technology & Science, Pilani, Hyderabad Campus First Semester 2020-2021 Computer Programming (CS F111) Lab 5
Birla Institute of Technology & Science, Pilani, Hyderabad Campus First Semester 2020-2021 Computer Programming (CS F111) Lab 5
Variables that are declared inside a function or block are called local variables. They can
be used only by statements that are inside that function or block of code. Local variables are not
known to functions outside their own. The following example shows how local variables are
used. Here all the variables a, b, and c are local to main() function.
#include <stdio.h>
int main () {
/* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
Global variables are defined outside a function, usually on top of the program. Global
variables hold their values throughout the lifetime of your program and they can be accessed
inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration. The following program show how global
variables are used in a program.
#include <stdio.h>
/* global variable declaration */
int g;
int main () {
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
Output: value of a = 10, b = 20 and g = 30
A program can have same name for local and global variables but the value of local variable
inside a function will take preference. Here is an example −
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main () {
/* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}
Output: value of g = 10
By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements
during declaration (if necessary).
There are two ways to define the variables of enum type as follows.
#include<stdio.h>
int main() {
return 0;
#include <stdio.h>
int main()
{
int multi1,multi2;
printf ("Multiplication Table from 1 to 15\n\n" );
printf (" " );
return 0;
}
2. Write a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result.
(Consider the operators +,-,*, /, % and use Switch Statement)
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,res,ch;
clrscr();
printf("\t *********************");
printf("\n\tMENU\n");
printf("\t********************");
printf("\n\t(1)ADDITION");
printf("\n\t(2)SUBTRACTION");
printf("\n\t(3)MULTIPLICATION");
printf("\n\t(4)DIVISION");
printf("\n\t(5)REMAINDER");
printf("\n\t(0)EXIT");
printf("\n\t********************");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
res=a+b;
printf("\n Addition:%d",res);
break;
case 2:
res=a-b;
printf("\n Subtraction:%d",res);
break;
case 3:
res=a*b;
printf("\n Multiplication:%d",res);
break;
case 4:
res=a/b;
printf("\n Division:%d",res);
break;
case 5:
res=a%b;
printf("\n Remainder:%d",res);
break;
case 0:
printf("\n Choice Terminated");
exit();
break;
default:
printf("\n Invalid Choice");
}
getch();
}
3. The total distance traveled by vehicle in 't' seconds is given by distance = ut+1/2at2
where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
Write C program to find the distance traveled at regular intervals of time given the values of
'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals
and repeat the calculations for different values of 'u' and 'a'.
Solution:
#include <stdio.h>
#include <math.h>
void main()
{
int tim_intrval, counter,time;
float accl, distance=0, velos;
clrscr();
printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A
VEHICLE===========>");
printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");
scanf("%d",&tim_intrval);
Exercise:
1. An electric power distribution company charges its domestic customers as follows:
Consumption Units Rate of charge
0-200 Rs 0.50 per unit
201-400 Rs 100 plus Rs 0.65 per unit excess 200
401-600 Rs 230 plus Rs 0.80 per unit excess 400
Write a C program that reads the customer number and power consumed and prints the
amount to be paid by the customer.
#include<stdio.h>
main()
{
int *j;
{
int i=10;
j=&i;
}
printf("%d",*j);
}
NOTE: Upload the screenshots of the Exercise programs along with the displayed results into
your corresponding Google Classroom.
PATH to Submit the Screenshots:
Google Classroom --> Classwork --> View Assignment --> Create/Upload