Computer Concept & Programming In C - Unit III - 16

Q.33 What are functions? What are advantages of using multiple functions in a program ?
Ans. Function: -
A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required. Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind. The name of the function is unique in a C Program and is Global. It neams that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing. We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.
Structure of a Function: -
There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}
Function Header: -
In the first line of the above code int sum(int x, int y)It has three main parts
The name of the function i.e. sum 
The parameters of the function enclosed in paranthesis 
Return value type i.e. int 
Function Body
What ever is written with in { } in the above example is the body of the function.
Function Prototypes: -
The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like int sum (int x, int y);
The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.
The advantages of the functions: -
     - Debugging is easier
     - It is easier to understand the logic involved in the program
     - Testing is easier
     - Recursive call is possible
     - Irrelevant details in the user point of view are hidden in functions
     - Functions are helpful in generalizing the program.

Q.34 Write a program in C to find whether the given number is prime or not.                                                                        (AKTU. 2009-10)
Ans.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define true 1
#define false 0
int isprime(long);
void main()
{
 long num;
 int result;
 clrscr();

 printf("\nEnter a positive integer:\n");
 scanf("%ld",&num);

 result = isprime(num);

 if(result == true )
    printf("\n%d  is a Prime Number", num);
 else
    printf("\n%d is not a Prime Number", num);

 getch();

}

int isprime(long n)
{
 long i, sq_rt;
 sq_rt = sqrt(n);
 for( i = 2; i < sq_rt + 1; i++)
    if(n % i == 0)
       return false;
 return true;
}

Q.35 Write a program in C accept a sentence and then display the frequency of vowels, the total number of words in the given sentence and the vowel with highest frequency.                                                                   (AKTU. 2013-14)
Ans.
  # include <stdio.h>
  # include <conio.h>
  void main ()
    {
  char sentance [100];
  int count, f, hi [5];
  int a, e, i, o, u, max, k;
  a = e = i = o = u = 0;
clrscr ();
printf (“\n enter the sentence”);
gets (sentence);
count = 0;
while (sentence [count]! = ‘\ 0’)
{
if (sentance [count] = = ‘a’ ¦ ¦ sentence [count] = = ‘A’)
a ++;
if (sentance [count] = = ‘e’ ¦ ¦ sentance [count] = ‘E’)
e ++;
   .
   .
   .
if (sentence [count] = = ‘U’ ¦ ¦ sentence [count] = = ‘U’)
u ++;
count ++;
}
printf (“\ n the total numbers o alphabets = %d”, count);
printf (“\ n the frequency of vowels is a = % d, e  % d, i = % d, o = % d, u = % d];
(a, e, i, o, u);
hi [0] = a; hi [1] = e; hi [2] = i;
hi [3] = o; hi [4] = u;
max = 0;
for (k = 0; k < 5, k ++)
if (max < hi [k])
max = hi [k];
if (max = = hi [0])
printf (“\ n highest frequency characters is a”);
if (max = = hi [1])
printy (“\ n highest frequency characters is e”);
if (max = = hi [2])
printf (“\ n highest frequency characters is i”);
if (max = = hi [3])
printy (“\ n highest frequency characters is o”);
if (max = = hi [4])
printy (“\ n highest frequency characters is u”);
}
}