Pages

Showing posts with label FUNCTION. Show all posts
Showing posts with label FUNCTION. Show all posts

Monday, November 9, 2020

Program to check if the given Value is of type Int in CPP

 

#include <iostream>
using namespace std;

bool isNumber(string s)
{
    for (int i = 0; i < s.length(); i++)
        if (isdigit(s[i]) == false)
            return false;
 
    return true;
}

int main() {
    // Saving the input in a string
    string str = "";
    
    cout << "Enter Value.\n";
    cin >> str;
 
    // Function returns 1 if all elements
    // are in range '0-9'
    if (isNumber(str))
        cout << "Entered Value is Integer";
 
    // Function returns 0 if the input is
    // not an integer
    else
        cout << "Entered Value is String";
        
    return 0;
}

Friday, March 29, 2013

Template Function

Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters.

#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
    T temp=a;
    a=b;
    b=temp;
}
void func(int x,int y,char w,char z)
{
    cout<<"x and y before swap: "<<x<<y<<"\n";
    swap(x,y)
    cout<<"x and y after swap: "<<x<<y<<"\n";
    cout<<"w and z before swap: "<<w<<z<<"\n";
    swap(w,z)
    cout<<"w and z after swap: "<<w<<z<<"\n";

}
int main()
{
    fun(10,20,’s’,’S’);
    return 0;
}



You might also like:-

Saturday, December 8, 2012

C++ PROGRAM - FACTORIAL USING RECURSION.

Write a C++ program which calculates the factorial of a given number using recursion.

#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
    if (a > 1)
        return (a * factorial (a-1));
    else
        return (1);
}
int main ()
{
    long ln;
    clrscr();
    cout << "Enter a number: ";
    cin >> ln;
    cout << ln << "!" << " = " << factorial (ln);
    getch();
    return 0;
}


OUTPUT








You might also like:-

Wednesday, August 8, 2012

PASS BY REFERENCE

Program to swap two numbers using pass by reference

#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y);  // function declaration
void main()
{
  int a,b;
  clrscr();
  cout<<"Enter two numbers a & b"<<endl;
  cin>>a>>b;
  cout<<"\nBefore swapping:\n";
  cout<<"a="<<a;
  cout<<"\nb="<<b;
  swap(a,b);     //function call
  cout<<"\nAfter swapping:\n";
  cout<<"a="<<a;
  cout<<"\nb="<<b;
  getch();
}
void swap(int &x,int &y)  //  arguments by reference
{
  int t;
  t=x;
  x=y;
  y=t;
}


OUTPUT



Tuesday, August 7, 2012

PASS BY ADDRESS

Program to swap two numbers using pass by address.

#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
  int a,b;
  clrscr();
  cout<<"Enter two numbers a & b"<<endl;
  cin>>a>>b;
  cout<<"\nBefore swapping:\n";
  cout<<"a="<<a;
  cout<<"\nb="<<b;
  swap(&a,&b);
  cout<<"\nAfter swapping:\n";
  cout<<"a="<<a;
  cout<<"\nb="<<b;
  getch();
}
void swap(int *x,int *y)
{
  int t;
  t=*x;
  *x=*y;
  *y=t;
}


output