#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if(b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
int main()
{
int a,b;
cout << "Input first number: ";
cin >> a;
cout << "Input second number: ";
cin >> b;
cout << "Greatest common divisior (GCD) is " << gcd(a,b) << endl;
return 0;
}
Showing posts with label code. Show all posts
Showing posts with label code. Show all posts
Program to find GCD(Greatest common divisior) of two numbers using recursion
Programe to Check Palindrome using recursion
#include<iostream>
using namespace std;
int palindrome(int num);
int main() {
int num,number, val;
cout<<"Enter a number: ";
cin>>num;
number=num; //save orignal number
val = palindrome(num); //function call
if(val==number) c//check returned value with orignal value/number
cout<<"Number is Palindrome";
else
cout<<"Number is not Palindrome";
return 0;
}
int palindrome(int num)
{
int rem;
static int temp=0;
if (num != 0)
{
rem =num%10;
temp=(temp*10)+rem;
palindrome(num/10); // recursive function call
}
return temp;
}
using namespace std;
int palindrome(int num);
int main() {
int num,number, val;
cout<<"Enter a number: ";
cin>>num;
number=num; //save orignal number
val = palindrome(num); //function call
if(val==number) c//check returned value with orignal value/number
cout<<"Number is Palindrome";
else
cout<<"Number is not Palindrome";
return 0;
}
int palindrome(int num)
{
int rem;
static int temp=0;
if (num != 0)
{
rem =num%10;
temp=(temp*10)+rem;
palindrome(num/10); // recursive function call
}
return temp;
}
Programe to calculate sum of digits using recursion
#include<iostream>
using namespace std;
int calsum(int num);
int main() {
int num, val;
cout<<"Enter a number: ";
cin>>num;
val = calsum(num);
cout<<"\nSum of the digits of "<<num<<" is "<<val;
return 0;
}
int calsum(int num)
{
if (num != 0)
{
return num%10 + calsum(num/10);
}
}
using namespace std;
int calsum(int num);
int main() {
int num, val;
cout<<"Enter a number: ";
cin>>num;
val = calsum(num);
cout<<"\nSum of the digits of "<<num<<" is "<<val;
return 0;
}
int calsum(int num)
{
if (num != 0)
{
return num%10 + calsum(num/10);
}
}
Output |
Program to Check Prime Number Using Recursion
#include <iostream>
using namespace std;
int checkPrimeNumber(int);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
if(checkPrimeNumber(n) == 0)
cout << n << " is a prime number.";
else
cout << n << " is not a prime number.";
return 0;
}
int checkPrimeNumber(int n)
{
bool flag = false;
for(int i = 2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = true;
break;
}
}
return flag;
}
using namespace std;
int checkPrimeNumber(int);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
if(checkPrimeNumber(n) == 0)
cout << n << " is a prime number.";
else
cout << n << " is not a prime number.";
return 0;
}
int checkPrimeNumber(int n)
{
bool flag = false;
for(int i = 2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = true;
break;
}
}
return flag;
}
Program of class Person using constructor, destructor, resolution operator in OOP
#include<iostream>
#include<conio.h>
using namespace std;
class person
{
public :
char
name[30], fname[30], contact[13], address[40];
person() //Constructor
{
cout<<"Enter
Person Name: ";
gets(name);
cout<<"Enter
person's FName: ";
gets(fname);
cout<<"Enter
Contact No: ";
gets(contact);
cout<<"Enter
Address: ";
gets(address);
}
void show();
void sleep()
{
cout<<"Person
is Sleeping"<<endl;
}
void wake_up()
{
cout<<"Person
is Wakeup"<<endl;
}
void walk()
{
cout<<"Person is Walking"<<endl;
}
void run()
{
cout<<"Person
is Running"<<endl;
}
~person() //destructor
{
cout<<"\nAt
the End Person Die";
}
};
void person::show()//Resolution operator
Method
{
cout<<"\nPerson
Name = "<<name<<endl;
cout<<"Father
Name ="<<fname<<endl;
cout<<"Contact=
"<<contact<<endl;
cout<<"Address=
"<<address<<endl<<endl;
}
int main()
{
person s;
s.show();
s.sleep();
s.wake_up();
s.walk();
s.run();
getch();
}Output |
Subscribe to:
Posts (Atom)