Sie sind auf Seite 1von 4

*C++ program to multiply and display the product of two numbers entered by user.

*/
#include <iostream>
using namespace std;
int main( )
{
float n1, n2, product;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
product = n1*n2;
cout << "Product = " << product;
return 0;
}

C++ program to check whether a number is palindrome or not */


#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num%10;
rev = (rev*10) + digit;
num = num/10;
}while (num!=0);
cout << " The reverse of the number is: " << rev << endl;
if (n==rev)
cout << " The number is a palindrome";
else
cout << " The number is not a palindrome";
}

return 0;

EX. OF LOOP
#include <iostream>
using namespace std;
int main() {
int i, n, factorial = 1;
cout<<"Enter a positive integer: ";
cin>>n;
for (i = 1; i <= n; ++i) {

factorial *= i;

// factorial = factorial * i;

}
cout<< "Factorial of "<<n<<" = "<<factorial;
return 0;
}

*C++ program to multiply and display the product of two numbers


#include <iostream>
using namespace std;
int main( )
{
float n1, n2, product;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
product = n1*n2;
cout << "Product = " << product;
return 0;
}

Example Program For If Statement In C++


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int a;
//Get Input Value
cout<<"Enter the Number :";
cin>>a;
//If Condition Check
if(a > 10)
{
// Block For Condition Success
cout<<a<<" Is Greater than 10";
}
getch();
return 0;
}

program to check count the no. Of common words


#include
#include
#include
#include
#include

<algorithm>
<iostream>
<iterator>
<sstream>
<string>

// Count clearly separated occurrences of `word` in `text`.


std::size_t count ( const std::string& text, const std::string& word )
{
std::istringstream input(text);
return (std::count(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(), word));
}
int main ( int, char ** )
{
const char text[] = "this is a book , and this book is about book .";
const char word[] = "book";
std::cout << count(text, word) << std::endl;
}

Output:
3

for fibonnacci series


#include<iostream>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;

}
cout << next << endl;

return 0;
}

++ program for prime numbers: print first n prime numbers.

C++ programming code


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

Das könnte Ihnen auch gefallen