week 3: swapping using function over loading
#include<iostream>
using namespace std;
void swap(int &ix,int &iy);
void swap(float &fx,float &fy);
void swap(char &cx,char &cy);
int main()
{
int ix,iy;
float fx,fy;
char cx,cy;
cout<<"Enter 2 integers:";
cin>>ix>>iy;
cout<<"Enter 2 floating point no:s:";
cin>>fx>>fy;
cout<<"Enter 2 characters:";
cin>>cx>>cy;
cout<<"\nIntegers:";
cout<<"\nix="<<ix<<"\niy="<<iy;
swap(ix,iy);
cout<<"\nAfter swapping";
cout<<"\nix="<<ix<<"\niy="<<iy;
cout<<"\nFloating point no:s";
cout<<"\nfx="<<fx<<"\nfy="<<fy;
swap(fx,fy);
cout<<"\nAfter swapping";
cout<<"\nfx="<<fx<<"\nfy="<<fy;
cout<<"\nCharacters";
cout<<"\ncx="<<cx<<"\ncy="<<cy;
swap(cx,cy);
cout<<"\nAfter swapping";
cout<<"\ncx="<<cx<<"\ncy="<<cy;
return 0;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swap(float &a, float &b)
{
float temp;
temp=a;
a=b;
b=temp;
}
void swap(char &a, char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}
week 3: reverse number and double of a number
#include <iostream>
using namespace std;
int reverseDigits(int num);
void dourevDig(int dou);
/* Iterative function to reverse digits of num*/
int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
/*Driver program to test reverseDigits*/
int main()
{
int num,dou;
cout<<"enter the number :";
cin>>num;
dou=reverseDigits(num);
cout << "Reverse of no. is "<<dou;
dourevDig(dou);
getchar();
return 0;
}
void dourevDig(int dou)
{
int dubble= dou * 2;
cout << "double of no. is " <<dubble;
}
week 3: find palindrome or not
#include <iostream>
using namespace std;
int main(){
char string1[20];
int i, length;
int flag = 0;
cout << "Enter a string: ";
cin >> string1;
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
cout << string1 << " is not a palindrome" << endl;
}
else {
cout << string1 << " is a palindrome" << endl;
}
return 0;
}
week 3: print cube using inline function
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{ int s,r;
cout << "enter the number: ";
cin>>s;
r=cube(s);
cout << "The cube of "<<s<<" is: " << r << "\n";
return 0;
}
week 3: print cube using class
//printing cube squar using class and inline function
#include<iostream.h>
#include<conio.h>
class power
{
public:
inline int square(int n)
{
return n*n;
}
inline int cube(int n)
{
return n*n*n;
}
};
void main()
{
int n,r;
power p;
clrscr();
cout<<“\nEnter the Number: \n” ;
cin>>n;
r=p.square(n);
cout<<“\nSquare of “<<n<<” = “<<r<<endl;
r=p.cube(n);
cout<<“\nCube of “<<n<<” = “<<r<<endl;
getch();
}
Comments
Post a Comment