Tuesday, 26 July 2016
Monday, 29 February 2016
Average of Numbers in c++
Source Code:
#include<iostream>
using namespace std;
int main()
{
int n;
double average(0);
cout<<"Enter the number of values : ";
cin >> n;
for(int i = 0; i < n; ++i)
{
int value;
cin >> value;
average += value;
}
average /= n;
cout<<"Average is "<<average;
return 0;
}
OUTPUT
Enter the number of values : 5 12 35 20 45 95
Average is 41.4
#include<iostream>
using namespace std;
int main()
{
int n;
double average(0);
cout<<"Enter the number of values : ";
cin >> n;
for(int i = 0; i < n; ++i)
{
int value;
cin >> value;
average += value;
}
average /= n;
cout<<"Average is "<<average;
return 0;
}
OUTPUT
Enter the number of values : 5 12 35 20 45 95
Average is 41.4
Array of Objects using c++
Source code:
#include <iostream>
using namespace std;
class Demo
{
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main()
{
Demo obj[4];
int i;
for(i=0; i < 4; i++)
obj[i].setX(i);
for(i=0; i < 4; i++)
cout<<"obj["<<i<<"].getX() : "<< obj[i].getX()<<endl;
return 0;
}
OUTPUT
obj[0].getX() : 0
obj[1].getX() : 1
obj[2].getX() : 2
obj[3].getX() : 3
#include <iostream>
using namespace std;
class Demo
{
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main()
{
Demo obj[4];
int i;
for(i=0; i < 4; i++)
obj[i].setX(i);
for(i=0; i < 4; i++)
cout<<"obj["<<i<<"].getX() : "<< obj[i].getX()<<endl;
return 0;
}
OUTPUT
obj[0].getX() : 0
obj[1].getX() : 1
obj[2].getX() : 2
obj[3].getX() : 3
Array Sort using c++
Source code:
#include <algorithm> // for std::swap, use <utility> instead if C++11
#include <iostream>
#define SIZE 10
using namespace std;
int main()
{
cout<<"array values :"<<" 132, 520, 210, 510, 140 ,125,52,96,55,85"<<"\n";
//input array values
int array[SIZE] = { 132, 520, 210, 510, 140 ,125,52,96,55,85 };
cout<<"sorted values : ";
// Step through each element of the array
for (int startIndex = 0; startIndex < SIZE; ++startIndex)
{
// smallestIndex is the index of the smallest element we've encountered so far.
int smallestIndex = startIndex;
// Look for smallest element remaining in the array (starting at startIndex+1)
for (int nowIndex = startIndex + 1; nowIndex < SIZE; ++nowIndex)
{
// If the current element is smaller than our previously found smallest
if (array[nowIndex] < array[smallestIndex])
// This is the new smallest number for this iteration
{
smallestIndex = nowIndex;
}
}
// Swap our start element with our smallest element
swap(array[startIndex], array[smallestIndex]);
}
// Now print our sorted array as proof it works
for (int index = 0; index < SIZE; ++index)
{
cout << array[index] << ' ';
}
cout<<"\n";
return 0;
}
OUTPUT
array values : 132, 520, 210, 510, 140 ,125,52,96,55,85
sorted values : 52 55 85 96 125 132 140 210 510 520
#include <algorithm> // for std::swap, use <utility> instead if C++11
#include <iostream>
#define SIZE 10
using namespace std;
int main()
{
cout<<"array values :"<<" 132, 520, 210, 510, 140 ,125,52,96,55,85"<<"\n";
//input array values
int array[SIZE] = { 132, 520, 210, 510, 140 ,125,52,96,55,85 };
cout<<"sorted values : ";
// Step through each element of the array
for (int startIndex = 0; startIndex < SIZE; ++startIndex)
{
// smallestIndex is the index of the smallest element we've encountered so far.
int smallestIndex = startIndex;
// Look for smallest element remaining in the array (starting at startIndex+1)
for (int nowIndex = startIndex + 1; nowIndex < SIZE; ++nowIndex)
{
// If the current element is smaller than our previously found smallest
if (array[nowIndex] < array[smallestIndex])
// This is the new smallest number for this iteration
{
smallestIndex = nowIndex;
}
}
// Swap our start element with our smallest element
swap(array[startIndex], array[smallestIndex]);
}
// Now print our sorted array as proof it works
for (int index = 0; index < SIZE; ++index)
{
cout << array[index] << ' ';
}
cout<<"\n";
return 0;
}
OUTPUT
array values : 132, 520, 210, 510, 140 ,125,52,96,55,85
sorted values : 52 55 85 96 125 132 140 210 510 520
Array Initialization using c++
Source code:
int main()
{
//If the size is omitted, the compiler uses the number of values
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
// No initialization.
float p1[1000];
//To initialize an array to all zeros, initialize only the first value.
// All 1000 values initialized to zero.
float p2[1000] = {0.0};
// Initial values of pressure(variable) undefined.
float pressure[10];
// Remaining characters zero.
char greeting[100] = "Hello";
// Array size is 6 (final zero on strings).
char goodbye[] = "Adios";
return 0;
}
int main()
{
//If the size is omitted, the compiler uses the number of values
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
// No initialization.
float p1[1000];
//To initialize an array to all zeros, initialize only the first value.
// All 1000 values initialized to zero.
float p2[1000] = {0.0};
// Initial values of pressure(variable) undefined.
float pressure[10];
// Remaining characters zero.
char greeting[100] = "Hello";
// Array size is 6 (final zero on strings).
char goodbye[] = "Adios";
return 0;
}
Array Element Swapping using c++
Source code:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
//variable declaration
int arr[100],i,temp,size;
cout<<"enter array size : ";
cin>>size;
cout<<"enter elements in to array : \n";
for(i=0;i<size;++i)
{
cin>>arr[i];
}
cout<<"\narray before swaping elements : \n";
for(i=0;i<size;++i)
{
cout<<arr[i]<<" ";
}
//element swapping logic
for(i=1;i<size;i+=2)
{
temp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=temp;
}
cout<<"\narray after swapping elements : \n";
for(i=0;i<size;++i)
{
cout<<arr[i]<<" ";
}
}
OUTPUT
enter array size : 4
enter elements in to array : 1 2 3 4
array before swaping elements : 1 2 3 4
array after swapping elements : 2 1 4 3
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
//variable declaration
int arr[100],i,temp,size;
cout<<"enter array size : ";
cin>>size;
cout<<"enter elements in to array : \n";
for(i=0;i<size;++i)
{
cin>>arr[i];
}
cout<<"\narray before swaping elements : \n";
for(i=0;i<size;++i)
{
cout<<arr[i]<<" ";
}
//element swapping logic
for(i=1;i<size;i+=2)
{
temp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=temp;
}
cout<<"\narray after swapping elements : \n";
for(i=0;i<size;++i)
{
cout<<arr[i]<<" ";
}
}
OUTPUT
enter array size : 4
enter elements in to array : 1 2 3 4
array before swaping elements : 1 2 3 4
array after swapping elements : 2 1 4 3
Armstrong Number using c++
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Source code:
#include <iostream>
using namespace std;
int main()
{
int n, n1, rem, num=0;
cout<<"Enter a positive integer: ";
cin>>n;
n1 = n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
{
cout << n << " is an Armstrong number.";
}
else
{
cout << n << " is not an Armstrong number.";
}
return 0;
}
Source code:
#include <iostream>
using namespace std;
int main()
{
int n, n1, rem, num=0;
cout<<"Enter a positive integer: ";
cin>>n;
n1 = n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
{
cout << n << " is an Armstrong number.";
}
else
{
cout << n << " is not an Armstrong number.";
}
return 0;
}
Area of Triangle using c++
| the formula for the area of a triangle is: | |||||
| |||||
| where | |||||
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float base,height;
float area;
cout<<"Enter base of Triangle : ";
cin>>base;
cout <<"Enter height of Triangle : ";
cin>>height;
area = 0.5 * (base * height);
cout<<"Area of Triangle :"<<area;
return 0;
}
Area of a circle using C++
| The area of a circle is the number of square units inside that circle. If each square in the circle to the left has an area of 1 cm2, you could count the total number of squares to get the area of this circle. Thus, if there were a total of 28.26 squares, the area of this circle would be 28.26 cm2 However, it is easier to use one of the following formulas: | |
| where | |
#include <iostream>
using namespace std;
int main()
{
float radius,A;
cout<<"Enter the Radius : ";
cin>>radius;
A=3.14*r*r;
cout<<"Area of Circle : "<<A;
return 0;
}
Alphabet pattern using c++
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
//variable declaration
int x, y, n;
char ch;
cout<<"Enter the number of rows -> ";
cin>>n;
for(x = 1;x<= n;x++)
{
cout<<"\n";
ch = 'A';
for(y = 1;y<=x;y++)
{
cout<<ch;
ch++;
}
}
}
OUTPUT
Number of rows -> 9
A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH
ABCDEFGHI
Accessing Two-Dimensional Array Elements using C++
Source code:
#include <iostream>
using namespace std;
int main ()
{
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
// output each array element's value
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
// output each array element's value
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
Absolute value of a number using C++
The absolute value of 0 is 0. The absolute value of −156 is 156. No Negatives! So in practice "absolute value" means to remove any negative sign in front of a number, and to think of all numbers as positive (or zero).
Source code:
#include<iostream>
using namespace std;
int main()
{
float num;
cout<<"Enter any number:";
cin>>num;
if(num>0)
{
cout<<"The absolute value of number is:"<<num;
}
else
{
cout<<"The absolute value of number is:"<<-(num);
}
return 0;
}
Hellow World using c++
//This is simple programmed to print "hellow world"
//copy and paste into your compiler to see the result..
Source code:
//copy and paste into your compiler to see the result..
Source code:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
Subscribe to:
Posts (Atom)