Powered by Blogger.

Breaking News

Formulir Kontak



C++ Programs

Q-write a program to find the sum of natural number?

Program Code:

#include<iostream>
using namespace std;
int sum (int m)
{
     if (m<1)
return 0;
else return m+sum(m-1);
}
int main()
{
    int m;
    cin>>m;
    cout<<"Sum of natural number is ="<<sum(m)<<endl;
    system ("pause");



Q-Write a program for standard deviation?
Program Code:

 #include <iostream>
#include <cmath>
using namespace std;

float calculateSD(float data[]);

int main()
{
    int i;
    float data[5];

    cout << "Enter 5 elements: ";
    for(i = 0; i < 5; i++)
        cin >> data[i];

    cout << endl << "Standard Deviation = " << calculateSD(data);
     system("PAUSE");
    return 0;
}

float calculateSD(float data[])
{
    float sum = 0.0, mean, standardDeviation = 0.0;

    int i;

    for(i = 0; i < 5; i++)
    {
        sum += data[i];
    }

    mean = sum/10;

    for(i = 0; i < 5; i++)
        standardDeviation += pow(data[i] - mean, 2);
       

    return sqrt(standardDeviation / 10);
}

Q-Write a program that uses array for making table of 4?
Program Code: 
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
              string table[20];
              int size=sizeof(table)/sizeof(string);
              for(int j=0 ;j<11 ;j++)
              {
                             int table=4*j;
                             cout<<"4*"<<j<<"="<<table<<endl;
                            
              }
              system("pause");
              return 0;
}

Q-Write a program to get date as input data and print on the screen. Define all the functions outside the class.

Program Code:

#include<iostream>

#include<conio.h>
class data

{
private:
inty,m,d;
public:
voidgdata();
void display();
};

voiddata::gdata()
{
cout<< "enter year ?"; cin>>y;

cout<<"enter month?"; cin>>m;

cout<<"enter day?"; cin>>d;
}

voiddata::display()
{
cout<<"date is:"<<endl; cout<<y<<m<<d;

}
int main()
{
dataabc;
abc.gdata();
abc.display();
return 0;

}

Q-Write a program to find the sum of two integers by initializing data using constructors.

Program Code:

#include<iostream.h>
#include<conio.h>
class sum
{private : int n, m ,s;
public:
sum() {
n=2; m=3;
s=n+m;
cout<<"sum of "<<n<<"and"<<m<<"is "<<s<<endl;
}
};
int main()
{
sum a;
return 0;
}

Q-Write a program to define two constructors with two input parameters and three input parameters respectively to find out the maximum values.
Program Code:

#include<iostream>
#include<conio.h>
using namespace std;
class sum
{
private : int x,y,z,s;
public:
sum(int x, int y)
 {
s=x+y;

if (x>y)
{

cout<<"greater number in 2 input aurgument constructor is "<<x<<endl;
}
else
{

cout<<"greater number in 2 input aurgument constructor is"<<y<<endl;
}

}
sum (int x, int y,int z)
{

            s=x+y+z;
            if (x<y&&x<z)
            {
                        cout<<"greater number in 3 input aurgument constructor is   "<<x;
            }
            else if(y>x&&y>z)
            {
                        cout<<"greater number in 3 input aurgument constructor is   "<<y;
                       
            }
            else
            {
                        cout<<"greater number in 3 input aurgument constructor is "<<z;
            }
           

}
};

int main()
{
sum a(3,4);
sum b(4,2,9);
return 0;
}