C++ hello world example
#include<iostream> using namespace std; int main() { cout << "Hello World\n"; return 0; }
Addition
#include <iostream> using namespace std; int main() { int a, b, c; cout << "Enter two numbers to add\n"; cin >> a >> b; c = a + b; cout <<"Sum of entered numbers = " << c << endl; return 0; }
C++ program to reverse a number
#include <iostream> using namespace std; class Operations { long c; public: void inputNumber() { cout << "Input a number\n"; cin >> c; } long reverseNumber() { long invert = 0; while (c != 0) { invert = invert * 10; invert = invert + c%10; c = c/10; } return invert; } }; int main() { long result; Operations t; t.inputNumber(); result = t.reverseNumber(); cout << "Number obtained on reversal = " << result; return 0; }
Program To find prime number
#include<iostream> #include<cmath> using namespace std; int main() { int n, status = 1, num = 3, count, c; cout << "Enter the number of prime numbers to print\n"; cin >> n; if ( n >= 1 ) { cout << "First " << n <<" prime numbers are :-" << endl; cout << 2 << endl; } for ( count = 2 ; count <=n ; ) { for ( c = 2 ; c <= (int)sqrt(num) ; c++ ) { if ( num%c == 0 ) { status = 0; break; } } if ( status != 0 ) { cout << num << endl; count++; } status = 1; num++; } return 0; }
Adding two arrays
include<iostream>
using namespace std;
main()
{
int first[20], second[20], c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
cout << "Enter elements of first array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "Enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << first[c] + second[c] << endl;
return 0;
}
Simple Class Program
#include<iostream> using namespace std; class programming { private: int variable; public: void input_value() { cout << "In function input_value, Enter an integer\n"; cin >> variable; } void output_value() { cout << "Variable entered is "; cout << variable << "\n"; } }; main() { programming object; object.input_value(); object.output_value(); //object.variable; Will produce an error because variable is private return 0; }
Program to Convert Deci to Binary
#include<iostream>#include <stdio.h>
using namespace std;
int main()
{
int n,x,a, c, k;
cout<<"Enter an integer in decimal number system";
cin>>x;
n=x;
cout<<"Binary Value OF Given Number Is: ";
for( a=1;n!=0;a++)
{
n=n/2;
}
a=a-2;
for (c = a; c >= 0; c--)
{
k = x >> c;
if (k & 1)
cout<<"1";
else
cout<<"0";
}
return 0;
}
Program To add 2x2 matrices
#include<iostream>using namespace std;
int main(){
//Using const int for array size
const int row=2,col=2;
// if not use const error found
cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For First Matrix:"<<endl;
int first[row][col], second[row][col];
int i,j;
for( i=0;i<row;i++){
cout<<"Enter value for row number: "<<i+1<<endl;
for( j=0;j<col;j++){
cin>>first[i][j];
}
}
cout<<"\n\n\nEnter Value For Second Matrix:"<<endl;
for( i=0;i<row;i++){
cout<<"Enter value for row number: "<<i+1<<endl;
for( j=0;j<col;j++){
cin>>second[i][j];
}
}
// Resultant Matrix
for( i=0;i<row;i++){
for( j=0;j<col;j++){
first[i][j]=first[i][j]+second[i][j];
}
}
cout<<"\n\n\t\tResultant Matrix:"<<endl;
for( i=0;i<row;i++){
cout<< endl;
for( j=0;j<col;j++){
cout<<"\t\t"<<first[i][j]<<" ";
}
}
return 0;
}






0 comments:
Post a Comment