CCP Unit 3
CCP Unit 3
CCP Unit 3
UNIT-3
Iteration and loops:
Ques 1: Can we use continue statement within the body of switch statement like break?
Ans: No,a continue can appear only in or as a loop body. A switch statement is a branching statement is a
branching statement and not a looping statement .
Ans :
Ans: By definition, the while statement does not end with a semicolon. However, it's legal in C to put a
Semicolon right after the while statement like this: while (expression); which means there is a null
Statement controlled by the while statement. Remember that the result will be quite different from what
you expect if you accidentally put a semicolon at the end of the while statement.
Ques 4. Write a program to generate even number series from 1 to 50 , using while loop.
Ques 5. For every use of a for loop, we can implement an equivalent while loop. So when should
we prefer to use these in different programs?
Ans: A while loop is preferred over a for loop when the number of iterations to be performed is not known
in advance. The termination of the while loop is based on the occurrence of some particular condition, i.e
specific sentinel value. Whereas the for loop is preferred when no. of iterations to performed are known
beforehand .
Output : 1 2 3 4 5 6 7 8 9 10
Ans: OUTPUT: 1 1 , 1 3, 2 1 , 3 3
Ques 9. What do you mean by iteration? Explain entry and exit controlled loops with example. Give
the classification of loops also.
Ans : The versatility of the computer lies in its ability to perform a set of instructions repeatedly. Some
specified portion of code is repeated n times , till a particular condition is satisfied. This repetitive operation
is done through a loop control instruction.
A program loop is the combination of body of the loop and a control statement. Control statement tests
certain conditions and then directs the repeated execution of the statements contained in the body of the
loop.
Depending on the position of control statement inside the loop. Two types of control structures are defined
(a) Entry controlled loop (b) Exit controlled loop.
In Entry controlled loop pre testing is done. In exit controlled loop post testing is done.
Types of loop statements in C language are : (i) while loop (ii) do while loop (iii) for loop.
Classification of Loops : Based on the nature of control variable and the kind of value assigned to it for
testing the control expression, the loops may be classified as :
Ques10. How is for loop executed? Give its syntax and example. What are additional features
of for loop?
Ans : for loop is a entry controlled loop. It provides a most concise structure in program as
compared to while loop and do while- loop. It is used when number of iterations are known before
hand in the program.
(d) After execution the control is transferred back to the for statement after evaluating the last
statement in the loop.
(e) Now control variable is updated again, and test test condition is checked for true/false.
(f) If test condition is true body of loop is executed again , otherwise exit from the loop occurs.
(A) Multiple Initialization: In this more than one variable can be initialized at a time in the for
statement. E.g : p = 1;
for ( n = 0 ; n < 17 ; n++), in this loop we can reconstruct as follows:
Ques 10. (a) Explain break and continue statement with examples.
(b) What are the properties of nested loops?
Syntax of break with while loop Syntax of break with for loop
while (test condition) for( ……)
{ {
…….. ……..
……… ………
if( condition) if( condition)
break ; break ;
……. …….
…….. ……..
} }
Transfers control out of the loop while. Transfers control out of the for loop.
Example : Example :
# include<stdio.h> # include <stdio.h>
void main() void main()
{ {
int i=1; int i=1;
while( i<=10) for ( ; ;)
{ {
if(i= = 5) printf ( “ \n %d ” , i);
break; if( i= = 5)
printf( “ \n %d ” , i); break;
++i;
} }
} }
(B) Continue statement :
(i) This keyword is used inside the loop body
(ii) When compiler encounters a continue statement, then rest of the statements in loop are
skipped and control is unconditionally transferred to the start statement of the loop.
(iii) When it is present inside the nested loop, it only terminates current iteration of the nearest
enclosing loop.
(iv) There is no limitation on the number of continue statements that can be present inside the
loop
Syntax of continue with while loop Syntax of continue with for loop
while (test condition) for( ……)
{ {
…….. ……..
.……… ………
if( condition) if( condition)
continue ; continue ;
……. …….
…….. ……..
}an be udes to control }
Transfers control out of the loop while. Transfers control out of the for loop.
Example : Example:
# include<stdio.h> # include<stdio.h>
void main() void main()
{ {
int i=1; int i;
while( i<=10) for ( i=1; i<=10 ; i++)
{ {
if(i%2 = = 0) if(i%2 = = 0)
continue ; continue;
printf( “ \n %d ” , i); printf( “ \t %d ” , i);
++i;
} }
} }
Output : 1 3 5 7 9
Nested Loops :. Another outer loop is used to control the number of times that a whole loop is repeated. Loops that
can be placed inside other loop. This feature can work with any loop but it is mostly useful in for loop because of
Example : void main() Output :
{ 1 1
int i,j; 1 3
for(i=1; i<3; i++) 2 1
for(i=1; j<4; j++) 2 3
{
if(j= = 2 ) continue ;
printf ( “ %d %d \n” , i,j ) ;
}}
simplicity of for loops. A for loop can be used to control the number of times that a particular set of statements will be
execute . Indentation of nested loops should be proper. Loops can be nested to any level .
1. Write a C program to find the factorial of a 2. Program to generate the table of any number
given number. using loop
#include<stdio.h>
#include<conio.h> #include<conio.h>
#include <stdio.h> void main()
void main() {
{ int t , num , t ;
long int i,fact=1,num; clrscr() ;
clrscr() ; printf (“Enter the number whose table is to be
printf("Enter the number\n"); generated : \n” ) ;
scanf("%ld",&num); scanf(“%d”, & num ) ;
if( num <= 0) printf ( “ Table of %d is :” , num) ;
fact = 1; for ( i=1 ; i<= 10 ; i++ )
else {
{ t = num * i ;
for(i = 1; i <= num; i++) printf( “\n %d x %d = %d ” , num , i , t ) ;
{ fact = fact * i ;} }
} /* End of else */ getch () ;
printf("Factorial of %ld =%ld\n", num, fact ); }
} \* End of main() *\
#include <stdio.h> .
#include <conio.h> #include <stdio.h>
void main() #include <stdio.h>
{ void main()
{
int i, num; int fib1=0, fib2=1, fib3, limit, count=0;
int flag = 1; printf("Enter the limit to generate the
clrscr(); fibonacci sequence\n");
printf(“ Enter the number”) ; scanf("%d", &limit);
scanf(“ %d” , & num) ; printf("Fibonacci sequence is ...\n");
for( i = 2; i< num; i++) printf("%d",fib1);
{ printf("%d",fib2);
if((num % i) = = 0) count = 2; /* fib1 and fib2 are already used */
{ while( count < limit)
prime = 0; {
} fib3 = fib1 + fib2; count ++ ;
} printf("%d\n",fib3);
fib1 = fib2;
if (prime = = 1) fib2 = fib3;
printf("%d is prime number.", num); }
else getch();
printf("%d is not a prime number.", num); } /* End of main() */
getch();
}
5. Program to accept an integer and reverse it. 6. Program to calculate the sum of the series
up to first 100 terms: 14 + 34 + 54 + 74 + … + n
terms.
#include<conio.h>
#include <stdio.h> #include<stdio.h>
void main() #include<conio.h>
{ #include<math.h>
long num, rev = 0, temp, digit; void main ()
printf("Enter the number\n"); { int n , i , sum = 0 ;
scanf("%ld", &num); clrscr();
/*For better programming, choose printf(“ENTER THE NUMBER OF
'long int' */ TERMS\n”) ;
scanf(“%d” , &n) ;
7 .Program to generate the sum of the following 8.Printing the pattern of program
series:- sum = x + x2 / 2! + x4 / 4! + …… + x n / n! *
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h> #include<stdio.h>
#include<math.h> #include<conio.h>
void main ()
void main () {
{ int row, col;
int , x , n , , j ,fact =0, clrscr();
float sum ; for (row=1;row<=5; row++)
clrscr(); {
printf(Enter the no. whose series is to be found: ”) ; for (col=1 ; col <=row; col ++)
scanf(“%d”, &x) ; printf( “ * “);
printf(“Enter the no. of terms up to which series is printf( “\n”);
to be generated”) ; }
scanf(“%d”, &n) ; getch();
sum = x ; }
for (i=1 ; i<=n ; i++)
{
if (i%2 = = 0)
{
fact =1;
for (j=1 ; j<=i ; j++)
{ fact = fact * j ;
}
9.Program to print the following pattern : 10. Program to print the following pattern
5 4 3 2 1 (FLOYD’S TRAINGLE) :
4 3 2 1 1
3 2 1 2 3
4 5 6
37 2 81 29 110
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
11. WAP to detect Armstrong Numbers in three 12. Program to find binary equivalent of a decimal
digits between 100 to 999. number entered by user.
# include<stdio.h> # include<stdio.h>
# include<conio.h> # include<conio.h>
void main() void main()
{ {
int d=3 , n , x=0 ; int r , n ;
int k , i , cube=0; clrscr() ;
printf ( “Enter the value of n :”) ;
clrscr(); scanf ( “ %d ” , &n) ;
printf( “ \n Following number are printf ( “ Binary number”) ;
Armstrong numbers ”) ; while ( n!=0)
for (k =100 ; k<=999 ; k++) {
{ r=n%2;
n=k ; n=n/2;
while (x<=d) printf ( “ %d ” , r ) ;
{ }
i=n %d ; getch();
cube = cube + pow (i, 3) ; }
n = n/10;
x++;
}
if(cube = = k)
printf ( “ \n \t %d ” , k) ;
}
getch();
} // End of main()
13. Program to calculate m to power n. 14. Program to add first seven terms of
#include<conio.h> following series using for loop :
#include<stdio.h> 1 / 1! + 2 / 2! + 3 / 3! +…….7 / 7!.
void main() #include<conio.h
{ int m , n , i=1; #include<stdio.h>
long int pow =1 ; void main()
clrscr(); {
printf( “ Enter two numbers : ”) ; int i=1, j ; float fact , sum = 0.0;
scanf ( “ %d %d ” , &m , &n); clrscr();
while ( i<=n) while(i<=7)
{ {
pow = pow * m ; fact = 1.0 ;
i++ ; for (j=1 ; j <=i ; j++)
} fact = fact * j;
printf ( “ \n %d to power %d = %ld ” , m , sum = sum + i/ fact; }
n, pow); printf( “ Sum of series = %f ” , sum);
getch(); getch() ; }
}
15. WAP to find the number and their sum 16. WAP to find the average of first n natural
between 100 and 200 divisible by 7. numbers using while loop.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{
{
int n , i=1, sum = 0 ;
int num , sum = 0 ; float avg = 0.0;
printf( “ Numbers divisible bt 7 b/w 100 & printf( “ Enter the value of n : ”) ;
200 are : \n ”) ; scanf( “ %d ” , &n) ;
for (num = 101 ; num < 200 ; num++) while( i <=n)
{ {
if (num % 7 = = 0 ) sum = sum + i ;
{ printf ( “ %d ” , num ) ; i++;
sum = sum + num ; }
} avg= (float) sum / n ;
} printf(“\n Sum of first %d numbers = %d “,
printf(“ Sum of all integers divisible by 7 b/w n ,sum ) ;
100 & 200 is = %d ”, sum); printf( “ \n The average of first %d numbers
getch() ; = % f ” , n , avg );
} getch() ;
}
17. Program to print the sum of odd numbers 18. WAP to find the sum of following series:
and even numbers separately from 1 to 50. √𝟏 + √𝟐 + √𝟑 +……..+ √𝒏
getch();
#include<stdio.h>
#include<conio.h>
void main()
{
int i , n , d , rev=0 , num;
clrscr();
printf( “ Enter the number \n”) ;
scanf( “ %d ” , &num);
n= num ;
while(n >0)
{
d=num %10;
rev=rev *10 +d;
n = num/10;
}
if(rev = = num)
printf( “ \n %d is a palindrome” , num);
else
printf(“ %d is not a palindrome”, num);
getch();
}
void main() }
{
int p =1, n ; printf( “ Sum of Series = %f ”, sum) ;
float sum=0.0, term;
printf(“ Enter the value of x \n”); getch();
scanf( “ %d” , &x) ; }
printf(“Enter the power of nth term\n ”);
scanf(“ %d ”, &n);
while( p<=n)
{
sum = sum + term;
21. WAP to check whether a given number is 22. WAP to generate the following pattern:
perfect or not ?
A
void main() A B
{ A B C
int num , sum =0 , i; A B C D
clrscr();
printf(“ Enter the number \n”) ; #include<stdio.h>
scanf(“ %d ”, &num) ; #include<conio.h>
for( i=1 ; i<num ; i++) void main()
{ {
if(num %i = = 0) char i, j ;
sum = sum + i; for ( i=65 ; i<=68 ; i++)
} {
for( j=65;j<=i; j++)
if( num = = sum) {
printf(“ %d is a perfect number”, num); printf( “ %c” , j);
else }
printf(“ %d is not a perfect number”, printf(“ \n”);
num); }
getch(); getch();
}
}
Ans: In our c program, every program starts with main () because it tells the C compiler where the program
starts its execution. It always returns an integer value. It is a well-defined user defined function, because
In different programs the body of main() contains different code written by the programmer.
Ans:The local variables are defined within the body of the function or the block. The variable defined is
local to that function or block only. Other functions can access these variables. The compiler shows errors in
case other functions try to access the variables.
Ques 4. What is the meaning of keyword void before the name of a function?
Ans: Keyword void means that the function should not return any value.
Ques5. What is an argument? Differentiate between formal arguments and actual arguments?
Ans: An argument is an entity used to pass the data from calling function to the called function. Formal
arguments are the arguments available in the function definition. They are preceded by their own data types.
Actual arguments are available in the function call.
Ques 6. What is the purpose of return keyword while programming with functions?
Ans : When a function calculates some value , then after that control returns from a function, values
returned are collected in calling function using the keyword ‘return’. A function can return only one
value at a time. Example : Following return statements are incorrect : return (a , b) , return ( x , 12),
Because both are trying to return two values. In case if we want to use more than one values in return
keyword syntax is : return value 1, value 2 , value 3…….value n. Here left to right evaluation is done
And the right most value is returned to calling function.
So correct statement are : return (a) , return (x).
But a function body can contain more han one return statements.
E.g : if ( x >= 5)
return (x) ;
else return ( 0) ;
Ques 7. Define exit() function with an example.
Ans : C provides a way to leave the program before it is actually terminated using exit() function.
(i)Syntax : exit(status) , where status is an int variable / constant.
(ii)This function is defined in stdlib.h header file
Example :
# include<stdio.h>
#include<conio.h>
# include <tdlib.h>
void main()
{
printf( “ C programming is logical and interesting ”) ;
exit (0);
printf ( “ C can be easily learned”) ;
printf ( “ C is a powerful language invented by Denis Ritchie in AT & T labs Bell”);
}
Ques 8. What are functions and their types ? What are the elements of user defined functions
, explain?
Ans : Functions break large computing tasks into smaller ones, and make the task of coders easy.
A function is a self-contained block of statements that perform a coherent task of some kind.
In C language we use many small functions rather than few big functions. Functions support
modular programming , in which a software is divided into small modules and each module consists
of several fumctions/sub modules.
In a C program, execution starts from the function main(). A function can activate some another
function. It can also call itself recursively , such functions are called recursive functions.
User Defined functions: Definition of these functions are written by the programmers itself .These
are not already defined in the library of C compiler. Advantages of user defined functions are as
follows :
(a) It facilitates top down approach of modular programming.
(b) Provides more structured and easy way to read and understand the large & complex codes.
(c) It is easy to find faulty functions and their testing and debugging.
(d) User defined functions may be used by many other programs (with the help of user defined
Header Files). So repetition of same code again and again can be avoided, which makes less
execution time and reduced code size.
Function declaration: Like variablesin C , all the functions in a C program must be declared before they
are invoked . A function declaration consists of four parts :
(i) Function return type. E.g : int , float , char, void etc.
(ii) Function name (Follows the rules of an identifier.)
(iii) Parameter or argument list. A function can contain one or more parameters with data type of
arguments.
(iv) Termination semicolon.
(v) Prototype definition and declaration of library functions are inside the header files.
Function Call: A function can be called by simply using the function name followed by a list of
actual parameters if any required.
(i) When the compiler encounters a function call, the control is transferred to the function
definition.
(ii) This function is then executed line by line and a value is returned when return statement is
executed.
Function definition: It is the actual implementation of function, where actual logic of program
is written. A function can only be defined once but can be declared many times.
A function can be declared within the body of some other functions but cannot be defined
within the body of other function.
Elements of Function definition: (i) Function name (ii) Function name (iii) List of parameters.
(iv) Local variable declarations (v) function statements (vi) return statements.
Example : #include <stdio.h>
#include<conio.h>
int cal_sum(int , imt , int);
void main()
{
int a , b, c , sum=0 ;
printf(“ Enter the three values :”) ;
scanf( “ %d %d %d”, &a , &b , &c);
sum=cal_sum(a,b,c) ;
printf( “\n Sum = %d ”,sum) ;
}
int cal_sum (int x , int y , int z ) ;
{
int d ;
d= x + y + z ;
return (d) ;
}
Ques 9: Explain Call by Value and Call by Reference methods of parameter passing. In support give
the example programs also.
Ans : Call by Value : In this method values of variables are passed by the calling function to the called
function. New variables are created by the called function to store values of arguments passed to it.
So called function uses a copy of the actual arguments .Changes are reflected only in the called function ,
not the calling function.
Advantage: Arguments passed can be variables (e.g x) , literals (e.g : 6 ) , or expressions (e.g x + 1).
Disadvantage: Copying data consumes additional storage. It can take lot of time to copy , so performance
of function may decrease if it is called several times .
Call by Reference : When a function wants to modify the value of the argument , then we pass arguments
using address of variables. Function parameters are declared as a reference/address. When this is done then
any changes made by the function to the arguments it receives, are visible in the calling function.
For reference an asterisk(*) is placed after the data type in parameter list.
Advantage : Since arguments passed are not copied into new variables , it provides greater time and space
efficiency. We can return multiple values using this method.
Disadvantage : Side effect of this method is whrn an argument is passed using call by address, it becomes
difficult to tell whether that argument is meant for input , output or both.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void swap (int x , int y) ; void swap (int *x , int *y) ;
void main() void main()
{ int a , b ; { int a , b ;
clrscr(); clrscr();
printf ( “ Enter the values of two numbers \n”) ; printf ( “ Enter the values of two numbers \n”) ;
scanf( “% d %d “ , &a, &b); scanf( “% d %d “ , &a, &b);
printf( “Values of a and b before swapping : \n”); printf( “Values of a and b before swapping : \n”);
printf ( “ a= %d\t b=%d” , a ,b); printf ( “ a= %d\t b=%d” , a ,b);
swap (a , b) ; swap (a , b) ;
printf( “Values of a and b after swapping : \n”); printf( “Values of a and b after swapping : \n”);
printf ( “ a= %d\t b=%d” , a ,b); printf ( “ a= %d\t b=%d” , a ,b);
getch(); getch();
} }
void swap(int x , int y) void swap(int *x , int *y)
{ {
int t ; int t ;
t = x; t = *x;
x = y; *x = *y;
y=t; *y = t ;
printf(“ In swap function values are : \n” ); printf(“ In swap function values are : \n” );
printf( \n x = %d \t y = %d, x ,y); printf( \n x = %d \t y = %d, x ,y);
} }
Output : Output :
Enter the values of two numbers: a=10 , b=20 Enter the values of two numbers: a=10 , b=20
Values of a and b before swapping :a=10, b=20 Values of a and b before swapping :a=10, b=20
In swap function values are : x= 20, y=10 In swap function values are : x= 20, y=10
Values of a and b after swapping : a =10, b=20 Values of a and b after swapping : a =20, b=10
Ans : A recursive function is defined as a function that calls itself again and again to solve a smaller
version of its task until a final call is made. Every recursive solution has two major cases :
(a) Base Case : In this the problem is simple enough to be solved directly without making any furthrt
calls to the same function.
(b) Recursive Case : In this first the given problem is divided into simpler sub parts. Then secondly ,
function calls itself but to handle sub parts of first step.
Basic steps of recursion are analyzed as follows :
(i) Initially Specify the base case which will terminate the function call itself.
(ii) Check whether the current value processed matches with the value of the base case. If yes,
then process and return the value.
(iii) Divide the problem into smaller sub parts and call the function from sub part.
(iv) Combine the results of sub parts.
(v) Return the result of the entire problem.
Recursive function for adding 1 + 2 + 3 …n terms.
#include<stdio.h> getch();
#include<conio.h> }
int sum (int) ; // function prototype. int sum (int x) // function definition
void main() {
{ int a = 0 ;
int n , s ; if ( x = = 0)
clrscr(); return 0 ;
printf (“Enter the value of n terms :” ); else
scanf (%d “ , &n) ; a = x + sum(x-1) ; // recursive case.
s = sum( n) ; // function call in main() return(a);
printf (“ Sum of %d natural numbers = %d” , n,s) ; }
Iteration Recursion
1. This uses a set of statements which are This involves selection of a particular structure
repeated. in a function.
2. Repetition is applied to the loop Function calls itself again and again.
constructs.
3. Iteration terminates when loop test Recursion stops when base case is encountered
condition becomes false. in function call , within its definition.
4. Iteration requires a counter variable or a In this base case and recursive case is required.
sentinel value.
5. Less memory consumed More memory is consumed , because each time
a function calls itself , a copy of variables is
created.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int sum (int , int , int ) ; long int rec_fact(long int) ;
float avg (int , int , int ) ; void main ()
// Prototype or declaration of function {
void main() int num , fact ;
{ clrscr() ;
int a , b , c, sum=0 ; printf(“Enter the number”) ;
float av =o ; scanf (“%ld”, &num) ;
s= sum (a , b , c); // function call fact = rec_fact (num) ;
av = avg (a , b , c) ; printf(“Factorial of %ld = %ld”, num , fact) ;
scanf (“%d%d%d”, &a, &b, &c) ; getch() ;
// actual parameters }
printf (“Sum= %d\t Average = %f ” , s , av) ; // recursive definition of factorial
getch() ;
} // End of main () long int rec_fact(long int x) ;
{ int f ;
// Definition of user defined function sum () if(x = = 1 )
int sum (int x , int y , int z ) return 1; // base case
// x , y and z are formal parameters else
{ {
return (x + y + z) ; f = x * rec_fact(x-1) ; // Recursive case
} return (f) ;
/ Definition of user defined function avg () }
float avg (int x , int y , int z ) }
{
float m ;
m = (float) (x + y + z ) / 3;
return m ;
}
3. Program to generate the Fibonacci series 4. WAP to find largest number among the
using recursive function given two numbers using a user defined
function large().
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int fibo( int) ; float large (float m , float n) ;
void main () void main()
{ {
int n , f ; float x , y , max ;
clrscr() ; float large (float m , float n) ;
printf(“Enter the number upto which series is printf(“ Enter the two numbers \n”);
to be generated \n”) ; scanf( “ %f %f ” , &x , &y) ;
scanf (“%d”, &n) ; printf( “ x = %f and y = %f \n” , x , y ) ;
printf(“Fibonacci series up to %d terms is : \n max = large(x , y) ;
\n”, n ) ; printf(“ The largest number = %f \n ”, max);
}
for (int i=1 ; i< = n ; i++) ; float large (float m , float n)
{ {
f = fibo (i) ; if (m > n)
} return m ;
else
int fibo (int n) // Recursive definition return n ;
{ }
if (n = = 1)
return 0 ; // Base case
if ( n = = 2)
return 1 ;
return ( fibo(n - 1) + fibo(n - 2) ) ;
// At a time two recursive function called
so binary
}
5. WAP that accepts the length and width 6. WAP that accepts the three sides of a
of a rectangle and print the area. The triangle and print the area. The area of a
area of a rectangle is calculated by a user triangle is calculated by a user defined
defined function and returns it value to function and returns it value to main
main program where it is printed. The program where it is printed. The values
values length and width are accepted of three sides are accepted from the
from the keyboard as an integer value. keyboard as an integer value
Also draw the flow chart of program.
# include<stdio.h> # include<stdio.h>
#include<conio.h> #include<conio.h
int rect_area(int , int); #include <math.h>
void main() float tri_area(float , float, float);
{ void main()
int length , width ,area=0 ; {
clrscr() ; float s1 ,s2 ,s3, area=0.0 ;
printf(“ Enter the length and width \n ”); clrscr() ;
scanf( “ %d %d ”, length , width); printf(“ Enter the three sides of triangle \n ”);
printf(“ Length =%d width= %d \n”, scanf( “ %f %f %f”, s1 , s2, s3) ;
&length, & width) ; printf(“ side1 =%f side2= %f side3=%f \n”,
area= rect_area (length , width) ; &s1, & s2, &s3) ;
printf(“ Area of rectangle = %d ”, area); area= tri_area (s1 , s2 ,s3) ;
getch(); printf(“ Area of triangle = %f ”, area);
} getch();
int rect_area(int l , int w) }
{ float tri_area (float a , float b , float c)
return (l *w) {
} perim=0.0 , a=0.0 ;
perim = ( a + b + c) /2 ;
a= sqrt( perim * (perim-a) *(perim-b)
*(perim-c)) ;
return (a);
}