Assignment No. 05
Assignment No. 05
(a) main( ) {
printf ( "\nOnly stupids use C?" ) ;
display( ) ;
}
display( )
{
printf ( "\nFools too use C!" ) ;
main( ) ;
}
Ans: only stupids use c
fools too use c
(b)
main( ) {
printf ( "\nC to it that C survives" ) ;
main( ) ;
}
Ans: C to it that C survives
(c)
main( ) { int i = 45, c ;
c = check ( i ) ;
printf ( "\n%d", c ) ;
}
check ( int ch )
{
if ( ch >= 45 )
return ( 100 ) ;
else
return ( 10 * 10 ) ;
}
Ans: 100
(d)
main( ) {
int i = 45, c ;
c = multiply ( i * 1000 ) ;
printf ( "\n%d", c ) ;
}
check ( int ch )
{
if ( ch >= 40000 )
return ( ch / 10 ) ;
else
return ( 10 ) ;
}
Ans: 40
(e)main( ) {
float area ;
int radius = 1 ;
area = circle ( radius ) ;
printf ( "\n%f", area ) ;
}
circle ( int r )
{
float a ;
a = 3.14 * r * r ;
return ( a ) ;
}
Ans: 3.140000
(f) main( ) {
void slogan( ) ;
int c = 5 ;
c = slogan( ) ;
printf ( "\n%d", c ) ;
}
void slogan( )
{
printf ( "\nOnly He men use C!" ) ;
}
Ans: 5
(g)
main( ) {
int i = 5, j = 2 ;
junk ( i, j ) ;
printf ( "\n%d %d", i, j ) ;
}
junk ( int i, int j )
{
i=i*i;
j=j*j;
}
Output: 5,2
(h)
main( ) {
int i = 5, j = 2 ;
junk ( &i, &j ) ;
printf ( "\n%d %d", i, j ) ;
}
junk ( int *i, int *j )
{
*i = *i * *i ;
*j = *j * *j ;
}
Output: 25,4
(i)
main( ) {
int i = 4, j = 2 ;
junk ( &i, j ) ;
printf ( "\n%d %d", i, j ) ;
}
junk ( int *i, int j )
{
*i = *i * *i ;
j=j*j;
}
Output: 16,4
(j)
main( ) {
float a = 13.5 ;
float *b, *c ;
b = &a ; /* suppose address of a is 1006 */
c=b;
printf ( "\n%u %u %u", &a, b, c ) ;
printf ( "\n%f %f %f %f %f", a, *(&a), *&a, *b, *c ) ;
}
Output: 1006 1006 1006
13.5 13.5 13.5 13.5 13.5
k. main( ) {
int i = 0 ;
i++ ;
if ( i <= 5 )
{
printf ( "\nC adds wings to your thoughts" ) ;
exit( ) ;
main( ) ;
}
}
Output: C adds wings to your thoughts
l.
main( ) {
static int i = 0 ;
i++ ;
if ( i <= 5 )
{
printf ( "\n%d", i ) ;
main( ) ;
}
else
exit( ) ;
}
Output: 1.
2.
3.
4.
5.
(m)
main( ) {
float area ;
int radius = 1 ;
area = circle ( radius ) ;
printf ( "\n%f", area ) ;
}
circle ( int r )
{
float a ;
a = 3.14 * r * r ;
return ( a ) ;
}
Output: 3.1400
(n)
main( ) {
void slogan( ) ;
int c = 5 ;
c = slogan( ) ;
printf ( "\n%d", c ) ;
}
void slogan( )
{
printf ( "\nOnly He men use C!" ) ;
}
Output: slogan cannot be assigned to any variable, as it has a return type of void
[B] Point out the errors, if any, in the following programs:
(a) main( )
{
int i = 3, j = 4, k, l ;
k = addmult ( i, j ) ;
l = addmult ( i, j ) ;
printf ( "\n%d %d", k, l ) ;
}
addmult ( int ii, int jj )
{
int kk, ll ;
kk = ii + jj ;
ll = ii * jj ;
return ( kk, ll ) ;
}
Ans:
Output: 1. A semicolon missing in the prototype declaration of the function.
2. A function cannot return more than one values.
(b)
main( ) {
int a ;
a = message( ) ;
}
message( )
{
printf ( "\nViruses are written in C" ) ;
return ;
}
Ans: message function has return type void, so it cannot be assigned to any variable.
(c)
main( ) {
float a = 15.5 ;
char ch = 'C' ;
printit ( a, ch ) ; }
printit ( a, ch )
{
printf ( "\n%f %c", a, ch ) ;
}
Ans: 1. Function definition argument should have a datatype.
2. The function is not defined before calling, or there should be a prototype declaration of the function.
(d) main( )
{
message( ) ;
message( ) ;
}
message( ) ;
{
printf ( "\nPraise worthy and C worthy are synonyms" ) ;
}
Ans: invalid use of semicolon after function name in the function definition
(e) main( ) {
let_us_c( )
{
printf ( "\nC is a Cimple minded language !" ) ;
printf ( "\nOthers are of course no match !" ) ;
}
}
Ans: Function definition is invalid in other functions.
(f) main( )
{
message( message ( ) ) ;
}
void message( )
{
printf ( "\nPraise worthy and C worthy are synonyms" ) ;
}
Ans:. invalid use of semicolon after function name in the function definition.
(e)
sqr ( a ) ;
int a ;
{
return ( a * a ) ;
}
Ans: Invalid use of semicolon after the function name.
(f)
main( ) {
int i = 135, a = 135, k ;
k = pass ( i, a ) ;
printf ( "\n%d", k ) ;
}
pass ( int j, int b )
int c ;
{
c=j+b;
return ( c ) ;
}
Ans: Invalid use of semicolon after the function name.
(g)
main( ) {
int p = 23, f = 24 ;
jiaayjo ( &p, &f ) ;
printf ( "\n%d %d", p, f ) ;
}
jiaayjo ( int q, int g )
{
q=q+q;
g=g+g;
}
Ans: Invalid use of semicolon after the function name.
(h) main( ) {
int k = 35, z ;
z = check ( k ) ;
printf ( "\n%d", z ) ;
}
check ( m )
{
int m ;
if ( m > 40 )
return ( 1 ) ;
else
return ( 0 ) ;
}
Ans:
(i)
main( ) {
int i = 35, *z ;
z = function ( &i ) ;
printf ( "\n%d", z ) ;
}
function ( int *m )
{
return ( m + 2 ) ;
}
Ans: . A semicolon missing in the prototype declaration of the function.
[C] State whether the following statements are True or False:
1. The variables commonly used in C functions are available to all the functions in a program.
Ans: false, Scope of variables local to a function is not available in other functions.
2. To return the control back to the calling function we must use the keyword return.
Ans: false, After the execution of the last statement of the called function, control goes back to calling
function even without the use of the return statement, if the return type of the function is void.
3. The same variable names can be used in different functions without any conflict.
Ans: true
Ans: true
Ans: true
7. A function can still be useful even if you don’t pass any arguments to it and the function doesn’t return
any value back.
Ans: true
8. Same names can be used for different functions without any conflict.
Ans: False, Functions with the same name are not allowed in C.
9. A function may be called more than once from any other function.
Ans: true
Write a function to calculate the factorial value of any integer entered through the keyboard.
Ans: int main()
{
int n, fac;
printf("Enter an integer: ");
scanf("%d", &n);
//factorial function
int fact(int num) //function definition
{
int res;
res = num;
while(num>1)
{
res = res*(num-1);
num = num-1;
}
return res;
}
A positive integer is entered through the keyboard. Write a function to obtain the prime factors of
this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and
7.
Ans. #include<stdio.h>
int prime(int x);
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
prime(num);
}
int prime(int x)
{
int a;
for(a=2;a<=x;a++)
{
if(x%a==0)
{
printf("%d, ",a);
x = x/a;
a--;
}
}
}
A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits
of the 5-digit number:
(1) Without using recursion
(2) Using recursion
Ans. #include<stdio.h>
int rec_func(int num);
int non_rec_func(int num);
void main()
{
int num, rec, non_rec;
printf("Enter an integer: ");
scanf("%d", &num);
rec = rec_func(num);
non_rec = non_rec_func(num);
return (num%10+rec_func(num/10));
}