0% found this document useful (0 votes)
106 views4 pages

Check If A Year Is Leap Year or Not: C++ Program To Display Fibonacci Series

This C++ program contains code snippets that demonstrate how to: 1) Check if a year is a leap year using modulo operators to check for divisibility by 4, 100, and 400. 2) Display the Fibonacci series up to a given number of terms by initializing two starting values and calculating subsequent terms as the sum of the previous two. 3) Check if a number is a palindrome by reversing it and comparing to the original. 4) Find the transpose of a matrix by storing the elements of the original matrix in the transpose with the rows and columns swapped.

Uploaded by

Mitesh Vyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views4 pages

Check If A Year Is Leap Year or Not: C++ Program To Display Fibonacci Series

This C++ program contains code snippets that demonstrate how to: 1) Check if a year is a leap year using modulo operators to check for divisibility by 4, 100, and 400. 2) Display the Fibonacci series up to a given number of terms by initializing two starting values and calculating subsequent terms as the sum of the previous two. 3) Check if a number is a palindrome by reversing it and comparing to the original. 4) Find the transpose of a matrix by storing the elements of the original matrix in the transpose with the rows and columns swapped.

Uploaded by

Mitesh Vyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Check if a year is leap year or not

#include <iostream>
using namespace std;

int main()
{
int year;

cout << "Enter a year: ";


cin >> year;

if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";

return 0;
}

C++ Program to Display Fibonacci Series

#include <iostream>
using namespace std;

int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

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


{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";


}
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;
}
Example: Find Transpose of a Matrix
#include <iostream>
using namespace std;

int main() {
int a[10][10], transpose[10][10], row, column, i, j;

cout << "Enter rows and columns of matrix: ";


cin >> row >> column;

cout << "\nEnter elements of matrix: " << endl;

// Storing matrix elements


for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}

// Printing the a matrix


cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}

// Computing transpose of the matrix


for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}

// Printing the transpose


cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}

return 0;
}

You might also like