Optimised bubble-sort algorithm for use with small vectors of any type:
template<typename T>
void bubble_sort(std::vector<T>& v)
{
unsigned size = v.size();
if (1<size)
{
do
{
int n = 0;
for(int i=1; i<size; ++i)
{
if (v[i]<v[i-1])
{
std::swap (v[i-1], v[i]);
n=i;
}
}
size=n;
}
while (size);
}
}
Note that an insert sort is more efficient for small subsets, but std::sort handles subsets of any size extremely efficiently using a hybrid sorting algorithm.
#include <iostream.h> main() { int a; cout<<"enter a number : "; cin>>a; cout<<endl; if (a%2-1) cout<<"it is a prime number"; else cout<<"it is not a prime number" return 0; } ------------------------------------------ output: enter a number : 30 it is a not a prime number
Use the C++ getline() function from the standard library.
#include<iostream> int main() { int num1, num2; std::cout << "C++ addition program" << std::endl; std::cout << "Enter a number: "; std::cin >> num1; std::cout << "Enter another number: "; std::cin >> num2; std::cout << "The sum is " << num1 + num2 << std::endl; }
any number
#include <iostream> int main() { int num1=0, num2=0; printf( "Enter a number: " ); scanf_s( "%d", num1 ); printf( "Enter another number: " ); scanf_s( "%d", num2 ); printf( "Total is: %d\n", num1 + num2 ); return( 0 ); }
#includeusing namespace std;int main(){int pods,beans;float total;cout>beans;total=pods+beans;cout
bghjg
#include <iostream> int main() { int num; std::cout << "Enter a number: "; std::cin >> num; if(num % 2 == 0) { std::cout << "Number is even"; } else { std::cout << "Number is odd"; } return 0; }
dim a,b,c a=cint(inputbox("enter value for a")) b=cint(inputbox("enter value for b")) c=cint(inputbox("enter value for c")) if((a>b)and(a>c)) then msgbox "greatest number is a="&a else if ((b>a)and(b>c)) then msgbox "greatest number is b="&b else msgbox "greatest number is c="&c end if end if
cout<<"______": cin >>__>>__; [example cout<<"enter no."; cin>>a>>b; ]
Yes.
If the sum of squares of digits of a number equals to the number itself, then that number is called an aram strong number.