Wednesday, September 28, 2016

Decimal To Octal Conversion

Program To Convert Decimal Number To Octal Equivalent:


#include<iostream>
using namespace std;
main()
{
int num,octal[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
octal[i]=num%8;
num=num/8;
i++;
}
cout<<"\nOctal Equivalent is ";
for(int j=i-1;j>=0;j--)
cout<<octal[j];
}

Also see our program to convert Decimal To Binary Click Here
Output:


Decimal To Binary Conersion

Program to Convert Decimal Number To Binary Equivalent:


#include<iostream>
using namespace std;
main()
{
int num,binary[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
binary[i]=num%2;   //finding remainder
num=num/2;
i++;
}
cout<<"\nBinary Equivalent is ";
for(int j=i-1;j>=0;j--)  //from down to top moving
cout<<binary[j];
}
Also see program to evaluate postfix expression Click Here

Output:


Tuesday, September 27, 2016

Postfix Evaluation

Program To evaluate Postfix Expression:

#include<iostream>
#include<ctype.h>  //for isalnum function
using namespace std;
int stack[100];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{

return (stack[top--]);
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
if(isdigit(c))
push(c-'0');  //for converting datatype
else
{
x=pop();
y=pop();
switch(c)
{
case'+':
push(x+y);
    break;
    case '-':
    push(y-x);
    break;
   case '*':
push(y*x);
break;
case '/':
push(y/x);
break;
}
}
}
cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}

PALLINDROME PRODUCTS:

This C++ Program Displays all the Products of two 2-digit numbers that are PALLINDROME.

#include <iostream>
using namespace std;
long int pal(long int);
int main()
{
    int i,j;
    long int num;
    cout<<"Pallindrome Products are:\n";

start:
    for(i=10; i<100; i++)
    {
        for(j=10; j<100; j++)
        {
            num=i*j;
            if(pal(num)!=0)
            {
                cout<<num<<" = "<<i<<" * "<<j;
                cout<<endl;
            }
        }
    }
    return 0;
}

long int pal(long int x)
{
    int rem,tmp,rev=0;
    tmp=x;
    while(tmp!=0)
    {
        rem=tmp%10;
        tmp/=10;
        rev=rev*10+rem;
    }
    if(x==rev)
        return x;
    else
        return 0;
}


MAX MOD 5:

Given two integer values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0.

#include <iostream>
using namespace std;
int func(int,int);
int main()
{
int a,b;
cout<<"Enter First Number: ";
cin>>a;
cout<<"\nEnter Second Number: ";
cin>>b;
func(a,b);
return 0;
}
int func(int x,int y)
{
int rem1,rem2;
rem1=x%5;
rem2=y%5;
if(x==y)
return 0;

else
{
cout<<"\nAnswer is: ";
if(rem1==rem2)
x>y? cout<<y:cout<<x; //Conditional Operator

else
x>y? cout<<x:cout<<y;
}
}




Sunday, September 25, 2016

Static Members Both Data And Function

Program To Illustrate The Concept Of Static Members :


#include<iostream>
using namespace std;
class datatype
{
    int a;
    static int count;
    public:
        datatype()
        {
            a=++count;
        }
        void show()
        {
            cout<<"\nValue of Variable is "<<a;
        }
        static void showcount()
        {
            cout<<"\nNo of Objects cretaed "<<count;
        }
};
int datatype::count;

main()
{
    datatype a,b,c;
    a.show();
    b.show();
    c.show();
    datatype::showcount();
}

Number Of Words In a Text

Program to Calculate Number Of Words In a Text:


#include<iostream>
#include<string.h>
using namespace std;
main()
{
char string[100];
int l,ans=1;
    cout<<"Enter Text\n";
    cin.getline(string,100);
    l=strlen(string);
    cout<<"Total No of Words ";
    for(int i=0;i<l;i++)
    {
    if(string[i]==' ')
    {
    ans++;
}
}
    cout<<ans;
}
Special Thanks To Mr.Nikhil Aggarwal for this idea