Flow of Control [SET – 3]
Pattern Examples:
i) ************ ii) * iii) * ************ ** ** ************ *** *** ************ **** **** ***** iv) * v) 1 vi) 1 *** 222 212 ***** 33333 32123 ******* 4444444 4321234 ********* 555555555 543212345
#include <iostream> using namespace std; int main() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 12; j++) { cout << "*"; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n = 4; // Number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= (n - i); j++) { cout << " "; } for (int k = 1; k <= (2 * i - 1); k++) { cout << "*"; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { cout << i; } cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { int num = 1; for (int j = 1; j <= i; j++) { cout << num; num++; } cout << endl; } return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { double x, result = 0; int n, sign = 1; cout << "Enter value of x (in radians): "; cin >> x; cout << "Enter number of terms: "; cin >> n; for (int i = 1; i <= n; i += 2) { result += sign * pow(x, i) / tgamma(i + 1); sign *= -1; } cout << "sin(" << x << ") = " << result; return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { double x, result = 1; int n, sign = -1; cout << "Enter value of x (in radians): "; cin >> x; cout << "Enter number of terms: "; cin >> n; for (int i = 2; i <= n; i += 2) { result += sign * pow(x, i) / tgamma(i + 1); sign *= -1; } cout << "cos(" << x << ") = " << result; return 0; }
#include <iostream> using namespace std; int main() { int num, reverse = 0, digit; cout << "Enter an integer: "; cin >> num; while (num != 0) { digit = num % 10; reverse = reverse * 10 + digit; num /= 10; } cout << "Reversed Number: " << reverse; return 0; }