Close
Close

Practice questions on My functions

Level 1

1.
What would be the output of:
#include <stdio.h>
display(){
    printf("C\n");
    main();
}
int main(){
    printf("Hey\n");
    display();
    return 0;
}

2.
What is main function and why it is used?

3.
What would be the output of:
#include <stdio.h>
int i = 6;
display(){
    i = 5;
}
int main(){
    display();
    printf("%d\n",i);
    return 0;
}

4.
Write the following code using recursion:
#include <stdio.h>
int main(){
    int i;
    for (i = 0; i<10; i++){
        printf("Hello\n");
    }
    return 0;
}

Level 2

1.
Write a function to calculate area and perimeter of a rectangle.
#include <stdio.h>

void rect(int l, int b)
{
  printf("Area is %d\n",l*b);
  printf("Perimeter is %d\n",2*(l+b));
}

int main()
{
  rect(4,5);
  rect(10,3);
  return 0;
}

2.
Write a function to calculate area and circumference of a circle.

3.
Write a function to calculate power of a number raised to other. E.g.- ab.
#include <stdio.h>

int power(int a, int b)
{
    int i,product = 1;
    for(i=1;i<=b;i++)
    {
      product = product*a;
    }
    return product;
}

int main()
{
  printf("%d\n",power(2,4));
  return 0;
}

4.
Write a function to print prime factor of a number

5.
Using recursion, write a function to know nth term of a Fibonacci series.
Nth term of Fibonacci series is
F(n) = F(n-1)+(n-2)
F(0) = 0
F(1) = 1
F(2) = F(1)+(0) = 1+0 = 1
F(3) = F(2)+(1) = 1+1 = 2
F(4) = F(3)+(2) = 2+1 = 3
#include <stdio.h>

int fib(int x)
{
    if(x == 0)
    {
      return 0;
    }
    else if(x==1)
    {
      return 1;
    }
    else
    {
      return fib(x-1)+fib(x-2);
    }
}

int main()
{
  printf("%d\n",fib(0));
  printf("%d\n",fib(1));
  printf("%d\n",fib(2));
  printf("%d\n",fib(3));
  printf("%d\n",fib(4));
  printf("%d\n",fib(5));
  return 0;
}

6.
Write a function to tell user if he/she is able to vote or not.
( Consider minimum age of voting to be 18. )

7.
Print multiplication table of 12 using recursion.
#include <stdio.h>

table(int x, int y)
{
    if(y != 1)
    {
      table(x,y-1);
    }
    printf("%d\n",(x*y));
}

int main()
{
  table(12, 10);
  return 0;
}

8.
Write a function to calculate power of a number raised to other ( ab ) using recursion.
#include <stdio.h>

int power(int a, int b)
{
    if(b==1)
    {
      return a;
    }
    else
    {
      return(a*power(a,b-1));
    }
}

int main()
{
  printf("%d\n",power(2,4));
  return 0;
}

9.
Write a function “perfect()” that determines if parameter number is a perfectnumber. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.
[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].

10.
Write a function to check if a number is even or not.

11.
Write a function to check if a number is prime or not.
#include <stdio.h>

int isPrime(int x){
    int i,prime = 1;
    if(x>1)
    {
      for(i = 2;i<x;i++)
      {
        if(x%i==0)
        {
          prime = 0;
          break;
        }
      }
    }
    else{
      prime = 0;
    }
    return prime;
  }

int main()
{
  printf("%d\n",isPrime(5));
  printf("%d\n",isPrime(10));
  return 0;
}

Level 3

Ask Yours
Post Yours