0% found this document useful (0 votes)
104 views40 pages

EcE-21014 Lecture Notes

This document provides an introduction to C programming and covers topics such as: 1. Printing text and numbers 2. Adding/calculating with integers 3. Using conditions and if statements to check properties of inputs 4. Using loops like while, do-while, and for to iterate processes 5. Creating and calling functions to modularize and reuse code The document contains examples of C code demonstrating these basic programming concepts.

Uploaded by

Kyal Sin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views40 pages

EcE-21014 Lecture Notes

This document provides an introduction to C programming and covers topics such as: 1. Printing text and numbers 2. Adding/calculating with integers 3. Using conditions and if statements to check properties of inputs 4. Using loops like while, do-while, and for to iterate processes 5. Creating and calling functions to modularize and reuse code The document contains examples of C code demonstrating these basic programming concepts.

Uploaded by

Kyal Sin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

1. Introduction to C Programming ..................................................................................................................

3
1. A Simple C Program: Printing a Line of Text ..................................................................................... 3
2. Printing Numbers and Character ......................................................................................................... 3
3. Printing one line with two printf statements. ...................................................................................... 4
4. Printing multiple lines with a single printf. ......................................................................................... 4
5. Adding Two Integers ........................................................................................................................... 5
6. Calculate a + b, a - b, a × b, a ÷ b and its remainder. ......................................................................... 6
7. Write a program to calculate the sum and average of the three numbers. .......................................... 6
2. Conditions and If Statements ...................................................................................................................... 8
8. Write a program to check whether the user-input number is “Positive” or “Negative”. ..................... 9
9. Write a program to check whether the user-input number is “Even” or “Odd”. ............................... 10
10. Write a program that will accept two numbers and find the maximum number and minimum number.
10
11. Write a program to check whether the user-input number is “Zero”, “Positive” or “Negative”. ..... 10
12. You will receive the mark of a student for one subject from user. Write a program to determine the
grade according to the following table. ....................................................................................................... 11
13. Write a program that will examine the value of a floating point variable called temp and print one of
the following messages, depending on the value assigned to temp. (a) ICE: if the value of temp is less than
zero. (b) WATER: if the value of temp lies between o and 100. (c) STEAM: if the value of temp exceeds
100. 12
14. Write a program to accept two numbers and then display the following menu. ............................... 12
15. Write a program that reads the grade A, B, C, D or F and then prints “Excellent”, “Good”, “Fair”,
“Poor” or “Fail” according to the grade. ..................................................................................................... 13
16. Write a program that reads a character from user and print it is a vowel or not. .............................. 14
17. Write a program to perform the following calculation. User will choose a number from the following
message and you have to perform an appropriate calculation with the user-input degree according to this
number. ........................................................................................................................................................ 14
3. Looping Statements .................................................................................................................................. 16
18. Write a program to print the successive integers from 1 to 100 using while loop. ........................... 18
19. Write a program to print the successive odd integers between 1 and 100 using do-while loop. ...... 18
20. Write a program to print the successive integers from 1 to 100 using for-loop. ............................... 18
21. Write a program to calculate the sum and average of the first ten numbers. .................................... 19
22. Write a program to calculate the sum and average of the user-input numbers and then display the
result. Ask the user to enter the numbers or 0 to exit. ................................................................................. 19
23. Write the programs that print the following sequences of values using for-loop. ............................ 19
24. Create a program to find the factorial result of a user-input integer. ................................................ 21
25. Write a program to accept the marks of a student for nine subjects and then display the total and
average mark of that student. ...................................................................................................................... 21
26. Create a program that displays the even integers from 2 to 100 and adds a newline after each five
integers. Also find the sum and average of that integers. ............................................................................ 22
27. Write a program to count the number of students who passed or failed without limit. If the mark is
greater than or equal to 40, it can count as a “Pass”. Otherwise, count as a “Fail”. The user will enter the
marks and (-1) to exit. ................................................................................................................................. 23
28. Write a program to count the frequency of each face in rolling a six-sided die 6000 times. ............ 23
29. Write a program to demonstrate using the break statement in a for statement. ................................ 24
30. Write a program to demonstrate using the continue statement in a for statement. ........................... 24
4. Functions .................................................................................................................................................. 28
31. Implement a function to find the area of a circle. User will enter the radius of the circle in the main
program. ...................................................................................................................................................... 31
32. Create a function to calculate the area of a room. User will enter the length and the width of the room
in the main program and use this function to calculate the area and call the function display( ) to print out
the result. ..................................................................................................................................................... 31
33. Write a program to calculate the sum and product of the two integers by calling the functions: sum(
) and product( ). ........................................................................................................................................... 32
34. Create a function to calculate the square of a user-input integer. Write a program to use this function.
33
35. Write a program to perform the following tasks. .............................................................................. 33
36. Write a function to double a given integer by using local variable. Call this function from the main
program ten times and display the values of that integer in each stage. ..................................................... 35
37. Write a function to double a given integer by using a global variable. Call this function from the main
program ten times and display the values of that integer in each stage. ..................................................... 36
38. Write a function to swap the values of two given integers by using local variable. And then implement
a program to call this function..................................................................................................................... 36
39. Write a function to swap the values of two given integers by using global variable. And then
implement a program to call this function. ................................................................................................. 37
40. Create a program to calculate and print the factorials of the integers 0–10 by using a recursive
function........................................................................................................................................................ 38
41. Write a program that demonstrates scoping issues with global variables, automatic local variables
and static local variables. ............................................................................................................................ 38
1. Introduction to C Programming

1. A Simple C Program: Printing a Line of Text


#include <stdio.h>
int main( )
{
printf( "Welcome to C!\n" );
return 0;
}

2. Printing Numbers and Character

#include <stdio.h>
int main() {
int myNum = 5;
float myFloatNum = 5.99;
char myLetter = 'D';
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}

3. Printing one line with two printf statements.


#include <stdio.h>
int main( )
{
printf( "Welcome " );
printf( "to C!\n" );
return 0;
}

4. Printing multiple lines with a single printf.

#include <stdio.h>
int main( )
{
printf( "Welcome\nto\nC!\n" );
return 0;
}
5. Adding Two Integers

#include <stdio.h>
int main( )
{
int integer1;
int integer2;
printf( "Enter first integer\n" );
scanf( "%d", &integer1 );
printf( "Enter second integer\n" );
scanf( "%d", &integer2 );
int sum;
sum = integer1 + integer2;
printf( "Sum is %d\n", sum );
return 0;
}
6. Calculate a + b, a - b, a × b, a ÷ b and its remainder.

#include <stdio.h>
int main( )
{
int ans, a, b;
printf( "Enter the two numbers\n" );
scanf( "%d%d", &a, &b );
ans = a + b;
printf( "Sum is %d\n", ans);
ans = a - b;
printf( "Difference is %d\n", ans);
ans = a * b;
printf( "Product is %d\n", ans);
ans = a / b;
printf( "Division is %d\n", ans);
ans = a % b;
printf( "Remainder is %d\n", ans);
return 0;
}

7. Write a program to calculate the sum and average of the three numbers.
#include <stdio.h>
int main( )
{
float sum, a, b, c, avg;
printf( "Enter the three numbers\n" );
scanf( "%f%f%f", &a, &b, &c );
sum = a + b + c;
printf( "Sum is %f\n", sum);
avg = sum / 3;
printf( "The average is %f\n", avg);
return 0;
}
2. Conditions and If Statements
Types of Conditional Statements
(1) if
(2) if---else
(3) if---else if---else
(4) switch

(1) if

if (condition) {
statements; // block of code to be executed if the condition is true
}

(2) if---else
if (condition)
statement1;
else
statement2;
(3) if---else if---else
if (condition)
statement1;
else if (condition2)
statement2;
else
statement3;
(4) switch
switch( variable )
{
case 1: statement1; break;
case 2: statement2; break;
case 3: statement3; break;
.
.
.
case n: statementn; break;
default: statement;
}
8. Write a program to check whether the user-input number is “Positive” or “Negative”.
#include <stdio.h>
int main()
{
int num;
printf("Enter any Integer.\n");
scanf("%d", &num);
if(num>=0)
printf("The integer is Positive.");
else
printf("The integer is Negative.");
return 0;
}
9. Write a program to check whether the user-input number is “Even” or “Odd”.

#include <stdio.h>
int main()
{
int num;
printf("Enter any Integer.\n");
scanf("%d",&num);
if(num%2==0)
printf("The integer is Even.");
else
printf("The integer is Odd.");
return 0;
}
10. Write a program that will accept two numbers and find the maximum number and minimum
number.
#include<studio.h>
int main()
{
float num1, num2;
printf("Enter two numbers-");
scanf("%f%f", &num1, &num2);

if(num1>num2)
printf("%f is maximum number and %f is minimum number",num1,num2);
else printf("%f is maximum number and %f is minimum number", num2,num1);
return 0;
}
11. Write a program to check whether the user-input number is “Zero”, “Positive” or “Negative”.
#include <stdio.h>
int main()
{
int num;
printf("Enter any Integer.\n");
scanf("%d", &num);
if(num==0)
printf("The integer is Zero.");
else if(num>0)
printf("The integer is Positive.");
else
printf("The integer is Negative.");
return 0;
}
12. You will receive the mark of a student for one subject from user. Write a program to determine the
grade according to the following table.
Mark 90-100 80-89 70-79 60-69 0-59
Grade A B C D F

#include <stdio.h>
int main() {
int num;
printf("Enter the marks\n");
scanf("%d",&num);
if(num>=90)
printf("A\n");
else if(num>=80)
printf("B\n");
else if(num>=70)
printf("C\n");
else if("num>=60")
printf("D\n");
else
printf("F\n");
return 0;
}
13. Write a program that will examine the value of a floating point variable called temp and print one
of the following messages, depending on the value assigned to temp. (a) ICE: if the value of temp is
less than zero. (b) WATER: if the value of temp lies between o and 100. (c) STEAM: if the value of
temp exceeds 100.
#include <stdio.h>
int main()
{
float temp;
printf("Enter the temperature.\n");
scanf("%f", & temp);
if(temp < 0)
printf("ICE");
else if(temp >= 0 && temp <= 100)
printf("WATER");
else
printf("STEAM");
return 0;
}
14. Write a program to accept two numbers and then display the following menu.
(1) sum
(2) subtract
(3) multiply
(4) divide
The program will then calculate and produce the output according to the number selected by the
user from the menu.
#include <stdio.h>
int main() {
float a,b;
int num;
scanf("%f %f",&a,&b);
printf("Enter a and b = %f and %f",a,b);
printf("\nChoose the operation number under these\n1.Sum\t 2.Subtract\n3.Multiply\t 4.Divide\n5.None of
these\n");
scanf("%d",&num);
switch (num) {
case 1: printf("The answer is %f", a+b); break;
case 2: printf("The answer is %f", a-b); break;
case 3: printf("The answer is %f", a*b); break;
case 4: printf("The answer is %f", a/b); break;
default: printf("None of these.");
}
return 0;
}
15. Write a program that reads the grade A, B, C, D or F and then prints “Excellent”, “Good”, “Fair”,
“Poor” or “Fail” according to the grade.
#include <stdio.h>
int main() {
char grade;
printf("Enter a grade: \n");
scanf("%c", &grade);
switch( toupper(grade) )
{
case 'A': printf("Excelllent"); break;
case 'B': printf("Good"); break;
case 'C': printf("Fair"); break;
case 'D': printf("Poor"); break;
case ‘F’: printf("Fail ");break;
default: printf("Invalid Character");
}
return 0;
}

(OR)
#include <stdio.h>
int main() {
char grade;
printf("Enter a grade: \n");
scanf("%c", &grade);
switch( grade )
{
case 'a': case 'A': printf("Excelllent"); break;
case 'b': case 'B': printf("Good"); break;
case 'c': case 'C': printf("Fair"); break;
case 'd': case 'D': printf("Poor"); break;
case 'f': case ‘F’: printf("Fail ");break;
default: printf("Invalid Character");
}
return 0;
}
16. Write a program that reads a character from user and print it is a vowel or not.
#include <stdio.h>

int main() {
char alphabet;
scanf("%c", &alphabet);
switch ( toupper(alphabet) ) {
case 'A': printf("Vowel\n"); break;
case 'E': printf("Vowel\n"); break;
case 'I': printf("Vowel\n"); break;
case 'O': printf("Vowel\n"); break;
case 'U': printf("Vowel\n"); break;
default: printf("Not a vowel\n");
}
return 0;
}
17. Write a program to perform the following calculation. User will choose a number from the following
message and you have to perform an appropriate calculation with the user-input degree according
to this number.
“Please choose a number you want to calculate.
(1) sin (4) cot
(2) cos (5) sec
(3) tan (6) cosec”
#include <stdio.h>
#include<math.h>
int main() {
float degree,ans,val=3.142/180;
int num;
printf("Enter a degree value");
scanf("%f",&degree);
printf ("1.sin\t4.cot\n2.cos\t5.sec\n3.tan\t6.cosec\n");
scanf("%d",&num);
switch(num)
{
case 1: ans=sin(degree*val);break ;
case 2: ans=cos(degree*val);break ;
case 3: ans=tan(degree*val);break ;
case 4: ans=1/tan(degree*val);break ;
case 5: ans=1/cos(degree*val);break ;
case 6: ans=1/sin(degree*val);break ;
default : printf("Invalid number"); break ;

}
printf("The answer is %f",ans);
return 0;
}
3. Looping Statements
Types of Looping Statements
(1) while
(2) do---while
(3) for

(1) while
initialization;
while (condition)
{
statements;
change;
}
(2) do---while
initialization;
do
{
statements;
change;
} while (condition);
(3) for
for (initialization; condition; change) {
statements;
}

NOTES
➢ Statement: code block to be executed
➢ Initialization: is executed (one time) before the execution of the code block.
➢ Condition: defines the condition for executing the code block.
➢ Change: is executed (every time) after the code block has been executed.
18. Write a program to print the successive integers from 1 to 100 using while loop.
#include <stdio.h>
int main()
{
int i=1;
while (i<=100)
{
printf ("%d\t",i);
i=i+1;
}
return 0;
}
19. Write a program to print the successive odd integers between 1 and 100 using do-while loop.
#include <stdio.h>
int main()
{
int i=1;
do{
printf ("%d\t",i);
i=i+2;
} while (i<=100);
return 0;
}
20. Write a program to print the successive integers from 1 to 100 using for-loop.
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
printf ("%d\t",i);
}
return 0;
}
21. Write a program to calculate the sum and average of the first ten numbers.
#include <stdio.h>
int main()
{
int i=1;
float sum=0,avg=0;
while (i<=10)
{
sum=sum+i;
i++;
}
avg=sum/10;
printf("The sum is %f and the average is %f", sum, avg);
return 0;
}
22. Write a program to calculate the sum and average of the user-input numbers and then display the
result. Ask the user to enter the numbers or 0 to exit.
#include <stdio.h>
int main()
{
float sum=0,avg=0, num=1, count=0;
while (num != 0)
{
printf("Enter the numbers or 0 to exit ");
scanf("%f", &num);
sum=sum+ num;
count++;
}
avg=sum/( count -1);
printf("The sum is %f and the average is %f", sum, avg);
return 0;
}
23. Write the programs that print the following sequences of values using for-loop.
(1) 1, 3, 5, 7, 9, 11, 13, 15
(2) 3, 8, 13, 18, 23
(3) 20, 14, 8, 2, -4, -10
(4) 19, 27, 35, 43, 51

(1) 1, 3, 5, 7, 9, 11, 13, 15

#include <stdio.h>
int main()
{
int i;
for(i=1;i<=15;i=i+2)
{
printf ("%d\t",i);
}
return 0;
}
(2) 3, 8, 13, 18, 23

#include <stdio.h>
int main()
{
int i;
for(i=3;i<=23;i=i+5)
{
printf ("%d\t",i);
}
return 0;
}

(3) 20, 14, 8, 2, -4, -10

#include <stdio.h>
int main()
{
int i;
for(i=20;i>=-10;i=i-6)
{
printf ("%d\t",i);
}
return 0;
}
(4) 19, 27, 35, 43, 51
#include <stdio.h>
int main()
{
int i;
for(i=19;i<=51;i=i+8)
{
printf ("%d\t",i);
}
return 0;
}
24. Create a program to find the factorial result of a user-input integer.
n! = 1×2×3×4×…×n
#include <stdio.h>
int main()
{
int i=1, result=1, n;
printf("Enter a number to calculate the factorial result ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
result= result*i;
}
printf ("the factorial result is %d", result);
return 0;
}
25. Write a program to accept the marks of a student for nine subjects and then display the total
and average mark of that student.
#include <stdio.h>
int main()
{
int i=1, mark, total=0;
float avg=0;
while (i<=9)
{
printf("Enter the mark-");
scanf("%d", &mark);
total = total + mark;
i++;
}
avg= total /9;
printf("The total mark is %d and the average mark is %f", total, avg);
return 0;
}
26. Create a program that displays the even integers from 2 to 100 and adds a newline after each five
integers. Also find the sum and average of that integers.
#include <stdio.h>
int main()
{
int i, count=0;
float sum=0,avg=0;
for(i=2; i<=100; i=i+2)
{
printf ("%d\t",i);
if( (i%10) == 0 )
printf ("\n");
sum=sum+i;
count++;
}
avg=sum/count;
printf("The sum is %f and the average is %f ", sum, avg);
return 0;
}
27. Write a program to count the number of students who passed or failed without limit. If the mark is
greater than or equal to 40, it can count as a “Pass”. Otherwise, count as a “Fail”. The user will enter
the marks and (-1) to exit.
#include <stdio.h>
int main()
{
int pass=0, fail=0, mark=0;
while (1)
{
printf("Enter the mark or -1 to exit: ");
scanf("%d", &mark);
if( mark == -1 ) break;
else if( mark >= 40 ) pass++;
else fail++;
}
printf("Fail = %d\nPass = %d", fail, pass);
return 0;
}
28. Write a program to count the frequency of each face in rolling a six-sided die 6000 times.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int face, i, f1=0, f2=0, f3=0, f4=0, f5=0, f6=0;
for(i=1; i<=6000; i++)
{
face = 1 + rand()%6;
switch(face)
{
case 1: f1++;break;
case 2: f2++;break;
case 3: f3++;break;
case 4: f4++;break;
case 5: f5++;break;
case 6: f6++;break;
}
}
printf("Face \t Frequency \n");
printf("1.\t %d\n", f1);
printf("2.\t %d\n", f2);
printf("3.\t %d\n", f3);
printf("4.\t %d\n", f4);
printf("5.\t %d\n", f5);
printf("6.\t %d\n", f6);
return 0;

Break And Continue Statements


➢ The break statement, when executed in a while, for, do…while or switch statement, causes an
immediate exit from that statement.
➢ The continue statement, when executed in a while, for or do…while statement, skips the remaining
statements in that control statement’s body and performs the next iteration of the loop.
29. Write a program to demonstrate using the break statement in a for statement.
#include <stdio.h>
int main(void)
{
unsigned int x;
for (x = 1; x <= 10; ++x) {
if (x == 5) {
break;
}
printf("%u ", x);
}
printf("\nBroke out of loop at x == %u\n", x);
}
30. Write a program to demonstrate using the continue statement in a for statement.
#include <stdio.h>
int main(void)
{
for (unsigned int x = 1; x <= 10; ++x) {
if (x == 5) {
continue;
}
printf("%u ", x);
}
puts("\nUsed continue to skip printing the value 5");
}

Flowcharts
4. Functions

Built-in Library Function


Generally, there are four types of functions according to their return types and the input parameters they accept.
(a) Function (with both inputs/output)
(b) Function (with only inputs)
(c) Function (with only output)
(d) Function (with no input/output)

Function Input Output Behavior


(a) with both inputs/output 1 1 Accept and return values
(b) with only inputs 1 0 Accept values but don’t return any value
(c) with only output 0 1 Don’t accept any value but return a value
(d) with no input/output 0 0 Don’t accept and return any value

(a) Function (with both inputs/output)

return-type fun-name(parameters)
{
Statements;
return( );
}

Calling the funtion

result = fun-name(parameters);

(b) Function (with only inputs)


void fun-name(parameters)
{
Statements;
}

Calling the funtion

fun-name(parameters);
(c) Function (with only output)
return-type fun-name( )
{
Statements;
return( );
}
Calling the function

result = fun-name( );
(d) Function (with no input/output)
void fun-name( )
{
Statements;
}

Calling the funtion

fun-name( );
31. Implement a function to find the area of a circle. User will enter the radius of the circle in the main
program.
#include <stdio.h>
float area(float r);
int main() {
float radius,result;
printf("Enter the radius of the circle");
scanf("%f",&radius);
result = area(radius);
printf("The area of the circle is %f",result);

return 0;
}
float area(float r)
{
return(3.142*r*r);
}
32. Create a function to calculate the area of a room. User will enter the length and the width of the
room in the main program and use this function to calculate the area and call the function display(
) to print out the result.
#include <stdio.h>
int area(int a,int b);
void display(int ans);
int main() {
int a,b,result;
printf("Enter length and width of the room\n");
scanf("%d %d",&a,&b);
result= area(a,b);
display(result);
return 0;
}

int area(int a,int b){


return a*b;
}
void display(int ans){
printf("The area of the room is %d",ans);
}
33. Write a program to calculate the sum and product of the two integers by calling the functions: sum(
) and product( ).
#include <stdio.h>
int sum(int a,int b);
int product(int a,int b);
int main()
{
int a,b;
printf("Enter two Numbers\n");
scanf("%d %d",&a,&b);
printf("The sum = %d\n",sum(a,b));
printf("The product = %d\n",product(a,b));

return 0;
}

int sum(int a,int b){


return a+b;
}
int product(int a,int b){
return a*b;
}
34. Create a function to calculate the square of a user-input integer. Write a program to use this
function.
#include <stdio.h>
int square(int x);
int main ()
{
int num;
printf("Enter a Number\n");
scanf("%d",&num);
printf("The value of the square is %d",square(num));
}
int square(int x)
{
return x*x;
}
35. Write a program to perform the following tasks.
-Request and accept the three numbers from the user.
-Calculate the sum and average values.
-Find the smallest and largest numbers by using the functions Min( ) and Max( ).
-Print out the results (Sum, Average, Smallest, and Largest numbers) with a function called Display( ).
#include <stdio.h>
int Min(int x,int y, int z);
int Max(int x,int y, int z);
void display(int t, float a, int s, int l);
int main() {
int a,b,c, small, large, sum;
float avg;
printf("Enter the three numbers\n");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
avg= sum/3;
small=Min(a,b,c);
large =Max(a,b,c);
display(sum, avg, small, large);
return 0;
}
int Min(int x,int y, int z)
{
if(x<y && x<z)
return x;
else if(y<x && y<z)
return y;
else
return z;
}

int Max(int x,int y, int z)


{
if(x>y && x>z)
return x;
else if(y>x && y>z)
return y;
else
return z;
}
void display(int t, float a, int s, int l)
{
printf("Sum= %d\n",t);
printf("Average= %f\n",a);

printf("Smallest number= %d\n",s);

printf("Largest number = %d\n",l);


}

Local Variables
Those variables which are defined within some function and are accessible to that function only are called
Local Variables.

Global Variables
Those variables which are defined outside of function block and are accessible to entire program are known
as Global Variables.

36. Write a function to double a given integer by using local variable. Call this function from the main
program ten times and display the values of that integer in each stage.
#include <stdio.h>
int Double(int x);
int main ()
{
int num;
printf("Enter a Number\n");
scanf("%d",&num);
for(int i=1; i<=10; i++)
{
num = Double(num);
printf("%d. The doubled number is %d\n",i,num);
}
}
int Double (int x)
{
return x*2;
}
37. Write a function to double a given integer by using a global variable. Call this function from the
main program ten times and display the values of that integer in each stage.
#include <stdio.h>
int num;
void Double( );
int main ()
{
printf("Enter a Number\n");
scanf("%d",&num);
for(int i=1; i<=10; i++)
{
Double( );
printf("%d. The doubled number is %d\n",i,num);
}
}
void Double ( )
{
num= num*2;
}
38. Write a function to swap the values of two given integers by using local variable. And then
implement a program to call this function.
#include <stdio.h>
void Swap(int a, int b);
int main ()
{
int x,y;
printf("Enter the values of x: ");
scanf("%d",&x);
printf("\nEnter the values of y: ");
scanf("%d",&y);
printf("In main before swapping, x=%d and y= %d\n",x,y);
Swap(x,y);
printf("In main after swapping, x=%d and y= %d\n",x,y);
return 0;
}
void Swap(int a, int b)
{
printf("\nIn swap function before swapping, x=%d and y= %d\n",a,b);
int temp;
temp=a;
a=b;
b=temp;
printf("In swap function after swapping, x=%d and y= %d\n\n",a,b);
}

39. Write a function to swap the values of two given integers by using global variable. And then
implement a program to call this function.
#include <stdio.h>
int x,y;
void Swap( );
int main ()
{
printf("Enter the values of x: ");
scanf("%d",&x);
printf("\nEnter the values of y: ");
scanf("%d",&y);
printf("In main before swapping, x=%d and y= %d\n",x,y);
Swap( );
printf("In main after swapping, x=%d and y= %d\n",x,y);
return 0;
}
void Swap( )
{
printf("\nIn swap function before swapping, x=%d and y= %d\n",x,y);
int temp;
temp=x;
x=y;
y=temp;
printf("In swap function after swapping, x=%d and y= %d\n\n",x,y);
}

40. Create a program to calculate and print the factorials of the integers 0–10 by using a recursive
function.
#include <stdio.h>
long factorial(long number);
int main( )
{
int i;
for (int i = 0; i <= 10; ++i)
{
printf("%2d! = %ld\n", i, factorial(i));
}
}

long factorial(long number)


{
if (number <= 1) return 1;
else return (number * factorial(number - 1));
}
41. Write a program that demonstrates scoping issues with global variables, automatic local variables
and static local variables.
#include <stdio.h>
void useLocal();
void useStaticLocal();
void useGlobal();
int x = 1;
int main( )
{
int x = 5; // local variable to main
printf("local x in outer scope of main is %d\n", x);
{
int x = 7; // local variable to new scope
printf("local x in inner scope of main is %d\n", x);
}
printf("local x in outer scope of main is %d\n", x);

useLocal();
useStaticLocal();
useGlobal();

useLocal();
useStaticLocal();
useGlobal();
printf("\nlocal x in main is %d\n", x);
}
void useLocal( )
{
int x=25;
printf("\nlocal x in useLocal is %d after entering useLocal\n", x);
++x;
printf("local x in useLocal is %d before exiting useLocal\n", x);
}
void useStaticLocal( )
{
static int x = 50;
printf("\nlocal static x is %d on entering useStaticLocal\n", x);
++x;
printf("local static x is %d on exiting useStaticLocal\n", x);
}
void useGlobal(void)
{
printf("\nglobal x is %d on entering useGlobal\n", x);
x *= 10;
printf("global x is %d on exiting useGlobal\n", x);
}

You might also like