About  Contact


Program Using Function

Q64.Write a program to find simple interest and compound interest using functions.
Q65.Write a program to find roots of a quadratic equation using function
Q66.Write function to to search a number in an array of 10 integers, the function takes the array and the number to be searched as parameters and returns 1 if the search is success otherwise it returns -1. Write main function and call the function as well.
Q67.Write a function to check is a given number is prime or not. The function takes the number as parameter and returns 1 if prime otherwise returns -1. Write the main function and call the function.
Q68. Write a function to check if a given word is palindrome or not. The function takes the word as parameter and returns 'y' if yes otherwise it returns 'n'. Write the main function also and call the function.

Program-64
#include <iostream.h>
#include <math.h>


float simpleInt(float p, float r, float t){
   float si;
   si=P*r*t/100;
   return (si);
}

float compoundInt(float p, float r, float t){
   float ci;
   ci=P*pow(1 + r/100, t) - p;
   return (ci);
}
void main(){
  float P, R, T, Si, CI;
  cout<<"Enter Principal:";
  cin>>P;
  cout<<"Enter the Rate:";
  cin>>R;
  cout<<"Enter the Time:";
  cin>>T;
  SI=simpleInt(P,R,T);
  cout<<"The Simple interest="<<SI<<"\n";
  CI=compoundInt(P, R, T);
  cout<<"Compound Interest="<<CI;
}
        
Program-65
#include <iostream.h>
#include <math.h>


int roots(float a, float b, float c, float &r1, float &r2 ){
   float d, k;
   d=b*b-4*a*c;
   if(d<0){
      k=-1;
   }
   else{
      r1=(-b+sqrt(d))/(2*a);
      r2=(-b+sqrt(d))/(2*a);
      k=1;
   }
   return(k);
}


void main(){
  float A, B, C, R1, R2;
  int P;
  cout<<"Enter coefficient of square term:";
  cin>>A;
  cout<<"Enter coefficient of x:";
  cin>>B;
  cout<<"Enter the constant term:";
  cin>>C;
  P=roots(A, B, C, R1, R2);
  if(P==-1){
     cout<<"Roots are imaginary";
  }
  else{
     cout<<"Root-1="<<R1<<"\n";
     cout<<"Root-2="<<R2;
  }
}
        
In the above program see that in the function there are two reference parameters r1 and r2, they are taken to transfer two roots to the main function. The function returns -1 is the roots are imaginary, otherwise it calculates the roots and returns 1. The return value is checked in the main function and proper action is taken.
Program-66
#include <iostream.h>

int search(int no[10], int s){
  int i, f=0;
  for(i=0; i<=9; i++){
    if(no[i]==s){
      f=1;
      break;
    }
  }
  return (f);
}

void main(){
  int a[10], i, p, k;
  for(i=0; i<=9; i++){
    cout<<"Enter the element-"<<i;
    cin>>a[i];
  }
  cout<<"\n\n\nEnter the numbers to be searched=";
  cin>>p;
  k=search(a, p);
  if(k==1){
     cout<<"The number is found";
  }
  else{
     cout<<"The number is not found";
  }
}
In the above program the function search() takes the array(no[10]) and the number to be searched (s) as parameter. The returned value from the function is caught in the variable "k" in the "main()" function, based on the value of "k" proper message is displayed by the "if" statement in the "main()" function.
Program-67
#include <iostream.h>

int checkPrime(int p){
  int c=0;
  for(i=1; i<=p; i++){
    if(p%i==0){
      c++;
    }
  }
  return (c);
}

void main(){
  int P, k;
  cout<<"Enter the numbers=";
  cin>>P;
  k=checkPrime(P);
  if(k==2){
     cout<<"The number is Prime";
  }
  else{
     cout<<"The number is not Prime";
  }
}
In the above program the returned value is 2 if the number is prime. That is why the value of "k" is checked and proper message is displayed in the "main()" function.
Program-68
#include <iostream.h>
#include <string.h>
char checkPalindrome(char a[20]){
  int len, sm=0, em;
  char ch='y';
  len=strlen(a);
  em=len-1;
  while(sm<em){
    if(a[sm] != a[em]){
       ch='n';
       break;
    }
    sm++;
    em--;
  }
  return (ch);
}

void main(){
  char P[20], k;
  cout<<"Enter the word=";
  cin>>P;
  k=checkPalindrome(P);
  if(k=='y'){
     cout<<"The word is palindrome";
  }
  else{
     cout<<"The word is not palindrome";
  }
}
In the above program see that "ch" is assigned to 'y' initially then we are comparing the characters from the two ends (first & last; second & second last) at any step is the characters are not equal we make ch='n' and have exited the loop. Lastly the value of "ch" ('y' or 'n') is returned. The returned value is checked and proper message is displayed in the "main()" function.



Disclaimer: We have taken otmost care to keep the information error free. The site is not responsible for any kind of damage of whatsoever nature.
©Copy right reserved