Showing posts with label arithmetic operator. Show all posts
Showing posts with label arithmetic operator. Show all posts

15.9.15

sum of all digits from an integer number using c++

/* A Program that reads a six digit integer and prints the sum of its six digit using Arithmatic operators. */

#include<iostream>
using namespace std;
int print_sum_of_digit(int number)
{
    int a,i,sum=0,count=0;
    if(number!=0)
    {
        while(number>0)
        {
            i=number%10;
            number=number/10;
            sum+=i;
            count++;
        }
         cout<<"Number of Digit:"<<count<<endl;
         return sum;
    }
}
int main()
{
    int number,i;
    cout<<"Enter the digit of six integer:"<<endl;
    cin>>number;
    cout<<"The sum of the digit is:"<<print_sum_of_digit(number)<<endl;
    return 0;
}

Calculator using C++

/* Calculator that performing as calculator which allows all Arithmetic Operators with operands as input */
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int calculator(int a,char c,int b)
{
    int h,w,x,z;
    float y,m=0.0;
    z=(a+b);
    x=(a*b);
    w=(a-b);
    h=(a%b);
    y=(a/b);
    m=y;
    switch(c)
    {
    case '+':
        cout<<a<<"+"<<b<<"="<<z<<endl;
        break;
    case '-':
        cout<<a<<"-"<<b<<"="<<w<<endl;
        break;
    case '*':
        cout<<a<<"*"<<b<<"="<<x<<endl;
        break;
    case '/':
        cout<<a<<"/"<<b<<"="<<m<<endl;
        break;
    case '%':
        cout<<a<<"-"<<b<<"="<<x<<endl;
        break;
    default:
        cout<<"Error Input Program Terminated"<<endl;
        break;
    }
    getch();
    return 0;
}
int main()
{
    int a,b,x;
    char c,y;
    cout<<"Welcome To My Calculator:"<<endl;
    cout<<"Enter to calculate:"<<endl;
    start:
    cin>>a>>c>>b;
    calculator(a,c,b);
    cout<<"If you want to calculate more then press Y else N"<<endl;
    cin>>y;
    if(y=='y' || y=='Y')
    goto start;
    else if(y=='N' || y=='n')
    exit(0);
}