0% found this document useful (0 votes)
4 views

assignment 1 c++

The document contains multiple C++ code snippets for various programming tasks, including generating multiplication tables, Fibonacci series, reversing numbers, checking Armstrong numbers, performing arithmetic operations, and creating patterns. Each code snippet is structured with input prompts and outputs relevant results based on user input. The document serves as a collection of examples for basic programming concepts and algorithms.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

assignment 1 c++

The document contains multiple C++ code snippets for various programming tasks, including generating multiplication tables, Fibonacci series, reversing numbers, checking Armstrong numbers, performing arithmetic operations, and creating patterns. Each code snippet is structured with input prompts and outputs relevant results based on user input. The document serves as a collection of examples for basic programming concepts and algorithms.

Uploaded by

Hari Kalu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Set A

1 mulplication

#include<iostream>

using namespace std;

int main() {

int num,sum=0;

// Input the number for which multiplication table is required

cout << "Enter a number to print its multiplication table: ";

cin >> num;

// Print the multiplication table

//cout << "Multiplication Table of " << num << ":\n";

for (int i = 1; i <= 10; i++) {

sum=num*i

// cout << num << " * " << i << " = " << num * i << "\n";

Cout<<sum;

return 0;

}
2 fibonacci series

#include <iostream>

using namespace std;

void fibonacci(int n) {

int a = 0, b = 1;

int nextTerm;

cout << "Fibonacci Series up to " << n << " terms:" << endl;

for (int i = 1; i <= n; ++i) {

cout << a << " ";

nextTerm = a + b;

a = b;

b = nextTerm;

int main() {

int n;

cout << "Enter the number of terms for Fibonacci Series: ";

cin >> n;

fibonacci(n);

return 0;

}
3 Reverse

#include <iostream>

using namespace std;

int reverseNumber(int num) {

int reversedNum = 0;

while (num != 0) {

int remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10;

return reversedNum;

int main() {

int num;

cout << "Enter a number to reverse: ";

cin >> num;

int reversed = reverseNumber(num);

cout << "Reversed number: " << reversed << endl;

return 0;

}
Q4 Armstrong numbers

#include <iostream>

#include <cmath>

using namespace std;

// Function to check if a number is an Armstrong number

bool isArmstrong(int num) {

int originalNum = num;

int sum = 0;

int digits = 0;

// Count the number of digits in the number

while (num != 0) {

num /= 10;

digits++;

num = originalNum; // Restore original number

// Calculate the sum of the digits raised to the power of the number of digits

while (num != 0) {

int digit = num % 10;

sum += pow(digit, digits);

num /= 10;
}

return (sum == originalNum);

int main() {

int lower, upper;

// Prompt the user for the interval

cout << "Enter the lower bound of the interval: ";

cin >> lower;

cout << "Enter the upper bound of the interval: ";

cin >> upper;

// Check and display Armstrong numbers in the given interval

cout << "Armstrong numbers between " << lower << " and " << upper << " are:" << endl;

for (int i = lower; i <= upper; i++) {

if (isArmstrong(i)) {

cout << i << " ";

cout << endl;

return 0;

}
Q5 arthmatic operation

#include <iostream>

using namespace std;

int main() {

int num1, num2;

char op;

// Prompt the user for input

cout << "Enter first integer: ";

cin >> num1;

cout << "Enter second integer: ";

cin >> num2;

cout << "Enter an operator (+, -, *, /): ";

cin >> op;

// Perform the corresponding arithmetic operation

switch(op) {

case '+':

cout << "Result: " << num1 + num2 << endl;

break;

case '-':

cout << "Result: " << num1 - num2 << endl;

break;

case '*':

cout << "Result: " << num1 * num2 << endl;

break;

case '/':
if (num2 != 0) {

cout << "Result: " << num1 / num2 << endl;

} else {

cout << "Error: Division by zero is not allowed." << endl;

break;

default:

cout << "Error: Invalid operator." << endl;

break;

return 0;

Set B

Q1 pattern

#include <iostream>

using namespace std;

int main() {

char ch = 'A'; // Start with the letter 'A'

// Number of rows in the pattern

int rows = 4;

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

cout << ch << " ";

ch++;

}
cout << endl;

return 0;

Q2 star pattern

// C++ code to demonstrate star pattern

#include <iostream>

using namespace std;

// Driver Code

int main()

int n = 4;

//looping rows

for (int i = n; i > 0; i--) {

for (int j = 1; j <= n; j++) // looping columns

if (j >= i) {

cout << "* ";

else {

cout << " ";

cout << endl;

}
return 0;

Q3 series

#include <iostream>

Using namespace std;

int main() {

int n;

int sum = 0;

// Prompt user for input

cout << "Enter the value of n: ";

cin >> n;

// Validate input

if (n < 1) {

cout << "The value of n must be a positive integer." << endl;

return 1;

// Calculate the sum of the series

for (int i = 1; i <= n; ++i) {

sum += i * i;

// Display the result

cout << "The sum of the series is: " << sum << endl;
return 0;

Q4 decimal to binary number

#include <iostream>

using namespace std;

int main()

int a[10], n, i;

cout<<"Enter the number to convert: ";

cin>>n;

for(i=0; n>0; i++)

a[i]=n%2;

n= n/2;

cout<<"Binary of the given number= ";

for(i=i-1 ;i>=0 ;i--)

cout<<a[i];

Set C

Q1 star pattern

#include <iostream>

using namespace std;


int main() {

int rows = 5; // Number of rows in the pattern

for (int i = 1; i <= rows; ++i) {

// Print leading spaces

for (int j = i; j < rows; ++j) {

cout << " ";

// Print stars with spaces in between

for (int k = 1; k <= i; ++k) {

cout << "*";

if (k < i) {

cout << " ";

// Move to the next line

cout << endl;

return 0;

Q2 series

#include<iostream>

using namespace std;

float series_result(int n) {

float denominator = 1;

float res = 0;

for(int i = 1; i<= n; i++) {

denominator *= i;
res += float(i/denominator);

return res;

main() {

int n;

cout << "Enter number of terms: ";

cin >> n;

cout << "Result: " << series_result(n);

You might also like