4th sem oopc programs usinng function in cpp

Ø Write the following programs using FUNCTION in CPP language.


1.      Write a program to find sum of three numbers using functions.1.      Write a program to find sum of three numbers using functions.

#include<iostream.h>
#include<conio.h>
int sum(int a,int b,int c){
    return(a+b+c);
}
int main(){
    int x,y,z;
    clrscr();
    cout<<"Enter three value:"<<endl;
    cin>>x>>y>>z;
    cout<<"Sum is "<<sum(x,y,z);
    getch();
    return 0;
}

2.    Write a function big to find largest of two numbers and use this function in the main program to find largest of three numbers.


 #include<iostream.h>
#include<conio.h>
int max(int,int);
int max(int ,int ,int);
int main(){
    int a,b,c;
    clrscr();
    cout<<"Enter a value : \n";
    cin>>a>>b>>c;
    cout<<"Max from three value.\n"<<max(a,b,c)<<endl;
    cout<<"Max from a and b.\n"<<max(a,b);
    getch();
    return 0;
}
int max(int x,int y){
    if(x>y){
        return x;
    }else{
        return y;
    }
}
int max(int x,int y,int z){
    if(x>y){
        if(x>z){
            return x;
        }else{
            return z;
        }
    }else if(y>z){
        return y;
    }else{
        return z;
    }
}

3.      To find factorial of given number using function.

#include<iostream.h>
#include<conio.h>
int fact(int n){
    if(n==0){
        return 1;
    }else{
    return (n*fact(n-1));
    }
}
int main(){
    int num;
    clrscr();
    cout<<"Enter value of n:"<<endl;
    cin>>num;
    cout<<"factorial num of "<<num<<" = "<<fact(num)<<endl;
    getch();
    return 0;
}


4.      Write a recursive function to determine the sum of the integers 1, 2, 3..………+n that is recursively compute.


#include<iostream.h>
#include<conio.h>
int add(int n){
    if(n==0){
        return 0;
    }else{
        return(n+add(n-1));
    }
}
void main(){
    int num;
    clrscr();
    cout<<"Enter value of n:";
    cin>>num;
    cout<<"Addition is : "<<add(num);
    getch();
}

Comments

Post a Comment

Popular posts from this blog

5th sem OOPJ Write an interactive program to print a diamond shape. For example, if user enters the number 3, the diamond will be as follows:

SQL Lab 3

SQL Lab1: Create a Database Table for DayCare