Factorial program
#include <iostream>
using namespace std;
int main()
{
int x, num, fa = 1;
cout << "Type positive number: ";
cin >> num;
for (x = 1; x <= num; ++x) {
factorial *= x; // factorial = factorial * x;
}
cout << "Factorial of " << num << " = " <<
factorial;
return 0;
}
Assending and decending of natural number, odd , even sorting
Sum of first 10 number
#include <iostream>
using namespace std;
int main()
{
int i,sum=0;
for (i = 1; i <= 10; i++)
{
cout << i << " ";
sum=sum+i;
}
cout << "\n The sum of first 10 natural numbers: "<<sum <<
endl;
}
1. #include <iostream>
2. using namespace std;
3. int main () {
4. for(int i=1;i<=3;i++){
5. for(int j=1;j<=3;j++){
6. cout<<i<<" "<<j<<"\n";
7. }
8. }
9. }
Display Multiplication table up to 10
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
Display multiplication table up to a given range
#include <iostream>
using namespace std;
int main()
{
int n, range;
cout << "Enter an integer: ";
cin >> n;
cout << "Enter range: ";
cin >> range;
for (int i = 1; i <= range; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
int i; //for outer loop counter
int j; //for inner loop counter
for (i = 1; i <= 5; i++)
for (j = 1; j <= 10; j++)
cout << j;
cout << "\n";
return 0;
}
Output:
Program to check whether a given number is prime or not using loop in C++:
#include <iostream>
using namespace std;
int main() {
int i, n;
bool is_prime = true;
cout << "Enter a positive integer: ";
cin >> n;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
is_prime = false;
}
// loop to check if n is prime
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
}
C++ Program to Display Fibonacci Series up to N Number
of terms
// Fibonacci Series up to n number of terms
// C++ program to Display Fibonacci Series
#include<iostream>
using namespace std;
int main()
{
// declare variables
int n, i, a=0, b=1, c;
// take input
cout << "Enter the number of terms: ";
cin >> n;
// display Fibonacci Series
cout << "Fibonacci Series is: " << endl;
for (i=a; i<=n; i++)
{
cout << a << " ";
c=a+b;
a=b;
b=c;
}
return 0;
}
Output:-
Enter the number of terms: 10
Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34 55
C++ Program to Find Power of a Number Using For Loop
// C++ program to calculate power of a Number
#include<iostream>
using namespace std;
int main()
{
// declare variables
int base, exponent;
long result=1;
// take input
cout << "Enter base and exponent: ";
cin >> base >> exponent;
// calculate power value
for (int i = exponent; i >= 1; i--)
{
result *= base;
}
// display result
cout << "Result = " << result << endl;
return 0;
}
Output :
Enter base and exponent: 5 4
Result = 625
// C++ program to calculate power of a Number
#include<iostream>
using namespace std;
int main()
{
// declare variables int base, exponent; long result;
// take input cout << "Enter base and exponent: ";
cin >> base >> exponent;
// calculate power value result = pow(base, exponent);
// display result cout << "Result = " << result << endl; return 0;
}
Infinite Loop??????????????
[Link]
#include <iostream>
using namespace std;
int main(){
int arr[]={21,9,56,99, 202};
/* We have set the value of variable i
* to 0 as the array index starts with 0
* which means the first element of array
* starts with zero index.
*/
for(int i=0; i<5; i++){
cout<<arr[i]<<endl;
}
return 0;
}
// C++ program to display a pattern
// with 5 rows and 3 columns
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int columns = 3;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= columns; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
* * *
* * *
* * *
* * *
* * *
The following program uses a nested for loop to find the prime numbers from
2 to 100 −
#include <iostream>
using namespace std;
int main () {
int i, j;
for(i = 2; i<100; i++) {
for(j = 2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout << i << " is prime\n";
}
return ;
}
#include <iostream>
using namespace std;
int main()
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
cout << "(" << i << "," << j << ") ";
cout << endl;
}
Output: