0% found this document useful (0 votes)
17 views23 pages

Assignment No. 05

The document contains a programming assignment for FYBSc Paper V (Programming with C) that includes various programming tasks and questions. It covers outputs of C programs, identification of errors in code, true/false statements about functions, and writing functions for specific tasks such as calculating factorials and prime factors. Additionally, it includes examples of recursive and non-recursive functions for summing digits and converting decimal to binary.

Uploaded by

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

Assignment No. 05

The document contains a programming assignment for FYBSc Paper V (Programming with C) that includes various programming tasks and questions. It covers outputs of C programs, identification of errors in code, true/false statements about functions, and writing functions for specific tasks such as calculating factorials and prime factors. Additionally, it includes examples of recursive and non-recursive functions for summing digits and converting decimal to binary.

Uploaded by

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

Assignment

FYBSc Paper V (Programming with C) No. 05


Roll No: SAP No.:

[A] What would be the output of the following programs:

(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

4. Every called function must contain a return statement.

Ans: False, Not in the case of void returning functions.

5. A function may contain more than one return statements.

Ans: true

6. Each return statement in a function may return a different value.

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

10. It is necessary for a function to return some value.

Ans: False, a function may have a void return function


[D] Answer the following:

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);

fac = fact(n); //function call

printf("The factorial of %d is %d", n, fac);

//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);

printf("\n Calculate sum using recursion: %d",rec);


printf("\n Calculate sum without recursion: %d",non_rec);
}

int rec_func(int num)


{
if (num==0)
{
return 0;
}

return (num%10+rec_func(num/10));
}

int non_rec_func(int num)


{
int res, count=0;
while(num!=0)
{
res=num%10;
count += res; //count=count+res;
num = num/10;
}
return count;
}
A positive integer is entered through the keyboard, write a function to find the binary equivalent of
this number using recursion.
Ans. #include<stdio.h>
int non_rec_bin(int num);
int rec_bin(int num);
void main()
{
int num;
printf("Enter Number: ");
scanf("%d", &num);

printf("Decimal To Binary Using Recursion: %d", rec_bin(num));


printf("\nDecimal To Binary Without Using Recursion: %d", non_rec_bin(num));
}
int non_rec_bin(int num)
{
int x, res=0, pos=1;
while (num!=0)
{
x = num%2;
res = res + (x*pos);
pos = 10*pos;
num = num/2;
}
return res;
}
int rec_bin(int num)
{
if(num==0)
{
return 0;
}
else
{
return ((num%2)+10*rec_bin(num/2));
}
}
Write a recursive function to obtain the running sum of first 25 natural numbers.
Ans. #include<stdio.h>
int sum(int num);
int rec_sum(int num);
void main()
{
int n;

printf("Enter Range: ");


scanf("%d", &n);
printf("\nNon-Recursive: Sum of first %d numbers is: %d",n, sum(n));
printf("\nRecursive: Sum of first %d numbers is: %d",n, rec_sum(n));
}
// This function is for non recursion
int sum(int num)
{
int res=0;
while(num) //we can write this condition as while(num!=0) both are same
{
res = res + num;
num = num-1;
}
return res;
}

int rec_sum(int num)


{
while(num)
{
return (num+sum(num-1));
}
}
Write a function to compute the distance between two points and use it to develop another function
that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3).
Use these functions to develop a function which returns a value 1 if the point (x, y) lines inside
the triangle ABC, otherwise a value 0.

You might also like