0% found this document useful (0 votes)
3 views1 page

Simple Programs Set5

The document contains five C++ code snippets demonstrating basic programming concepts. The snippets include checking for a leap year, printing natural numbers, calculating the sum of natural numbers, printing a star triangle pattern, and printing squares of numbers. Each snippet is accompanied by its expected output.

Uploaded by

Abhilash Alshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Simple Programs Set5

The document contains five C++ code snippets demonstrating basic programming concepts. The snippets include checking for a leap year, printing natural numbers, calculating the sum of natural numbers, printing a star triangle pattern, and printing squares of numbers. Each snippet is accompanied by its expected output.

Uploaded by

Abhilash Alshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) Check Leap Year

#include <iostream>
using namespace std;
int main() {
int year = 2024;
if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
cout << "Leap Year";
else
cout << "Not Leap Year";
return 0;
}
Output: Leap Year

2) Print Natural Numbers (1–N)


#include <iostream>
using namespace std;
int main() {
int n = 5;
for(int i = 1; i <= n; i++)
cout << i << " ";
return 0;
}
Output: 1 2 3 4 5

3) Print Sum of N Natural Numbers


#include <iostream>
using namespace std;
int main() {
int n = 5, sum = 0;
for(int i = 1; i <= n; i++)
sum += i;
cout << "Sum = " << sum;
return 0;
}
Output: Sum = 15

4) Print Pattern (Star Triangle)


#include <iostream>
using namespace std;
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++)
cout << "* ";
cout << "\n";
}
return 0;
}
Output: * * * * * * * * * * * * * * *

5) Print Squares of Numbers (1–N)


#include <iostream>
using namespace std;
int main() {
int n = 5;
for(int i = 1; i <= n; i++)
cout << i << "^2 = " << i*i << "\n";
return 0;
}
Output: 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25

You might also like