View chapters from top of the tab for more information.
Subscribe my youtube channel, link is given below.
click on this ⇒⇒A step Up
Practice program on function |
P1. Program to input any no. after that find sum of all digit by function .
#include<stdio.h>
#inclued<conio.h>
void findsum_digit();
void main()
{
clrscr();
findsum_digit();
getch();
}
void findsum_digit()
{
int a , d , sum = 0
;
printf( “ Enter the
no. ” );
scanf( “ %d “,
&a );
while ( a > 0 )
{
d = a % 10 ;
sum = sum + d;
a= a/10;
}
printf( “ Sum of all
the digits are = %d “, sum );
}
P2. Program to input length and breadth after that find area and perimeter of rectangle by function .
#include<stdio.h>
#inclued<conio.h>
void input();
void findarea();
void findperi();
int l , b ;
void main()
{
clrscr();
input();
findarea();
findperi();
getch();
}
void input()
{
printf( “ Enter the
length and breadth ” );
scanf( “ %d %d “, &l , &b );
}
void findarea()
{
int ar ;
ar = l * b ;
printf( “ Area of
the rectangle = %d \n “ , ar );
}
void findperi()
{
int peri ;
peri = 2 * ( l + b
);
printf( “ Perimeter
of rectangle = %d “ , peri ) ;
}
P3. Program to input any no. after that print the table by implementing extra function.
#include<stdio.h>
#inclued<conio.h>
void input();
void table();
int n ;
void main()
{
clrscr();
input();
findtable();
getch();
}
void input()
{
printf( “ Enter the
length and breadth ” );
scanf( “ %d %d “, &l , &b );
}
void table()
{
int i , table ;
for ( i = 1 ; i
<= 10 ; i ++ )
{
table = n * i ;
printf ( “ table =
%d “ , table );
}
}
P4. Program to create a function as named findsmallest and to input three no. as parameter after that find smallest no.
#include<stdio.h>
#inclued<conio.h>
void findsmallest( int , int , int );
void main()
{
clrscr();
findsmallest ( 20 ,
30 , 5 ) ;
getch();
}
void findsmallest ( int a , int b , int c )
{
int smt ;
smt = a < b
&& a < c ? a : ( b < c ? b : c ) ;
printf ( “ smallest
no. = %d “ , smt ) ;
}
One more process to access parameter
void main ()
{
clrscr ();
int a , b , c ;
printf ( “ enter
the three no. “ );
scanf( “ %d %d %d “
, &a , &b , &c ) ;
findsmallest ( a ,
b , c );
getch () ;
}
P5. Program to create a function as named “ chckpalindrome() “. And to input no. as parameter after that check no. is palindrome or not.
#include<stdio.h>
#inclued<conio.h>
void input();
void checkpalindrome();
void main()
{
clrscr();
int a ;
input();
checkpalindrome();
getch();
}
void input()
{
printf( “ Enter the
number ” );
scanf( “ %d “, &a );
}
void checkpalindrome
( int a )
{
int rev = 0 , d ;
num = a;
while ( a > 0 )
{
d = a % 10 ;
rev = rev * 10 + d
;
a = a / 10 ;
}
if ( num == rev )
printf ( “
Palindrome “ ) ;
else
printf ( “ Not
palindrome “ ) ;
}
P6. Program to input two number as parameter after that check both numbers are amicable by implementing function .
#include<stdio.h>
#inclued<conio.h>
void input();
void checkamicable( int , int );
void main()
{
clrscr();
int a , b ;
input();
checkamicable( a , b
) ;
getch();
}
void input()
{
printf( “ Enter the
two number ” );
scanf( “ %d %d “, &a, &b );
}
void checkamicable ( int a , int b )
{
int i , s1 = 0 , s2 = 0 ;
for ( i = 1 ; i < a ; i ++ )
{
if ( a % i == 0 )
s1 = s1 + i ;
}
for ( i = 1 ; i
< b ; i ++ )
{
if ( b % i == 0
)
s2 += i ;
}
if ( s1 == b
&& s2 == a )
printf ( “ Amicable
no . “ ) ;
else
printf ( “ Not
amicable no. “ );
}
P10. Program to input any binary number after that convert into decimal using return type parametrical function .
#include<stdio.h>
#include<conio.h>
int binarytodecimal( int n )
{
int d , i = 1 ,
sum = 0 ;
while ( n > 0 )
{
d = n % 10 ;
sum = sum + d * i
;
i = i * 2 ;
n = n / 10 ;
}
return ( sum );
}
void main ()
{
clrscr();
int n ;
printf ( “ Enter
any binary number “ ) ;
scanf ( “ %d “ ,
&n ) ;
printf ( “ Decimal
no. = %d “ , binarytodecimal ( n ) ) ;
getch () ;
}
No comments:
Post a Comment