15.9.15

find prime and check with the sum of all primes using c++

/* Print Prime List of the given range and do the sum then check weather the number is equal to the given number */

#include<iostream>
#include<conio.h>
using namespace std;
int prime_equal(int n)
{
    int c,sum=0;
    for(int i=1;i<=n;i++)
    {
        c=1;
        for(int j=2;j<=i/2;j++)
        {
            if(i%j==0)
            {
                c=0;
                break;
            }
        }
        if(c==1)
        {
            cout<<i<<"\t";
            sum+=i;
        }
    }
    cout<<endl;
    cout<<"The sum is:"<<sum<<endl;
    if(sum==n)
    {
        cout<<"The sum is equal to "<<n<<endl;
    }
    else
    {
        cout<<"The sum is not equal to "<<n<<endl;
    }
 return 0;
}
int main()
{
    int n;
    cout<<"Enter Range:"<<endl;
    cin>>n;
    cout<<"prime List:"<<endl;
    prime_equal(n);
    return 0;
}

find prime numbers and make sum of all primes using c++

/* Find all primes and do the sum of those primes between a given range */

#include<iostream>
#include<conio.h>
using namespace std;
void prime_check(int first,int last)
{
    int c,j,count=0,sum=0;
    cout<<"The Number of Primes:"<<endl;
    for(int i=first;i<=last;i++)
    {
        c=1;
        for(j=2;j<=i/2;j++)
        {
            if(i%j==0)
            {
                c=0;
                break;
            }
        }
        if(c==1)
        {
            count++;
            sum+=i;
        }
    }
        cout<<count<<"\t"<<endl;;
        cout<<"The sum of the prime number is:"<<sum<<endl;
}
int main()
{
    int first,last;
    cout<<"Range Start from:"<<endl;
    cin>>first;
    cout<<"End of Range:"<<endl;
    cin>>last;
    prime_check(first,last);
    getch();
    return 0; }

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;
}

conditional operator in c++

/* Use of Conditional Operator */

#include<iostream>
using namespace std;
int Multiple(int a,int b);
int main()
{
int a,b;
cout<<"Enter two integer:"<<endl;
cin>>a>>b;
Multiple(a,b);
}
int Multiple(int a,int b)
{
if(a>b)
{
if(a%b==0)
cout<<"Multiple"<<endl;
else
cout<<"Not Multiple"<<endl;
}
else if(a<b)
{
if(b%a==0)
cout<<"Multiple"<<endl;
else
cout<<"Not Multiple"<<endl;
}
return 0;
}

comparison operator in c++

/* Use of Comparison Operator */

#include<iostream>
using namespace std;
string comparison(int age)
{
  string str;
  if(age<18)
    str="You are a child";
  else if(age>=18 && age<65)
    str="You are an adult";
  else if(age>=65)
    str="You are a senior citizen";
  return str;
}
int main()
{
    int age;
    cout<<"Enter Your Age to know who You are:"<<endl;
    cin>>age;
    cout<<comparison(age);
}

array programming in c++

/* Fill up an array,print the array,count number of values,count the number of odd,count the number of even,sum  of odd ,sum of even,average,find largest value,find smallest */

#include<iostream>
using namespace std;
int fill_up(int array[],int size);
int main()
{
    int array[10],size;
    cout<<"Enter the size of an array:"<<endl;
    cin>>size;
    fill_up(array,size);
}
int fill_up(int array[],int size)
{
    int count=0,even=0,odd=0,even_sum=0,odd_sum=0,largest,smallest;
    float avg=0.0,avg_sum=0.0;
    cout<<"Enter the element:"<<endl;
    for(int i=0;i<size;i++)
    {
        cin>>array[i];
        if(array[i]%2==0)
        {
           even++;
           even_sum+=array[i];
        }
        else
        {
            odd++;
            odd_sum+=array[i];
        }
        count++;
        avg_sum+=array[i];
    }
    avg=avg_sum/size;
    largest=array[0];
    smallest=array[0];
    for(int i=0;i<size;i++)
    {
        if(array[i]<smallest)
        {
            smallest=array[i];
        }
    }
    for(int i=0;i<size;i++)
    {
        if(array[i]>largest)
        {
            largest=array[i];
        }
    }
    cout<<"The value of the array is:"<<endl;
    for(int i=0;i<size;i++)
    {
        cout<<array[i]<<"\t";
    }
    cout<<endl;
    cout<<"The number of value into the array:"<<count<<endl;
    cout<<"The number of even Value is:"<<even<<endl;
    cout<<"The number of Odd value is:"<<odd<<endl;
    cout<<"The sum of the even number is:"<<even_sum<<endl;
    cout<<"The sum of the Odd number is:"<<odd_sum<<endl;
    cout<<"The Avarage is:"<<avg<<endl;
    cout<<"The Largest value is:"<<largest<<endl;
    cout<<"The Lowest value is:"<<smallest<<endl;
    return 0;
}

simple interest using c++

/* Write a C program to find the Simple Interest. Take input for Principle Amount, Rate of Interest and Time from terminal. */
  
#include<iostream>
#include<conio.h>
using namespace std;
int simple_interest(int a,int t,float i)
{
    float si=a*t*i/100;
    return si;
}
int main()
{
    int principle_amount,Time;
    float Interest_rate;
    cout<<"Enter the Principle Amount:"<<endl;
    cin>>principle_amount;
    cout<<"Enter the Time:"<<endl;
    cin>>Time;
    cout<<"Interest_rate"<<endl;
    cin>>Interest_rate;
    cout<<"Interest is:"<<simple_interest(principle_amount,Time,Interest_rate)<<endl;
}
 

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);
}

27.8.15

Class template to represent a generic vector

Write a class template to represent a generic vector. Include member functions to perform the following tasks:
1)  To create the vector.

2)  To modify the value of a given element.

3)  To multiply the vector by a scalar value.

4)  To display the vector in the form (10, 20, 30,…..)

#include<iostream.h>

#include<string.h>
#include<conio.h> template<class T> class vector
{

T *v; int size; public:

vector(int m); void create(T *a); void modify(int); void multiply(int); void display();
};

template<class T>

vector< T > :: vector(int m)
{

v=new T[size=m]; for(int i=0;i<size;i++) v[i]=0;

}

template<class T>

void vector<T>::create(T*a)
{
for(int i=0;i<size;i++)
{
cin>>a[i];
v[i]=a[i];
}
}

template<class T>

void vector<T>::modify(int k)
{
v[k]=v[k]+10;
}

template<class T>
void vector<T>::multiply(int k)

{

for(int i=0;i<size;i++) v[i]=v[i]*k;
}

template<class T>

void vector<T>::display()
{
cout.setf(ios::showpoint);
cout<<"\n(";
for(int i=0;i<size;i++) cout<<v[i]<<", "; cout<<")\n";
}

int main()

{

vector <float> v1(5); vector <int> v2(5); float *x;

int *y; int i; int s;

cout<<"Enter the float vector element :\n"; v1.create(x);
cout<<"Enter the Interger vector element :\n"; v2.create(y);
cout<<"enter the element u want to modify in float vector :"; cin>>i;
v1.modify(i);

v1.display();

cout<<"\nenter the element u want to modify in int vector :"; cin>>i;
v2.modify(i);

v2.display();

cout<<"\nenter the number to calculate the scalar product :"; cin>>s;
v1.multiply(s);

v2.multiply(s);

cout<<"\nthe Float vector after scalar product is as follows :"; v1.display();
cout<<"\nthe integer vector after scalar product is as follows :"; v2.display();
getch();

return(0);
}

***********************************OUTPUT*******************************

Enter the float vector element : 1.1 2.2 3.3 4.4 5.5




Enter the Integer vector element : 10 10 30 40 50





enter the element u want to modify in float vector :3

(1.100000, 2.200000, 3.300000, 14.400000, 5.500000, ) enter the element u want to modify in int vector :2

(10, 10, 40, 40, 50, )

enter the number to calculate the scalar product :2

the Float vector after scalar product is as follows : (2.200000, 4.400000, 6.600000, 28.799999, 11.000000, )

the integer vector after scalar product is as follows : (20, 20, 80, 80, 100, )