0% found this document useful (0 votes)
53 views5 pages

CP Assignment 1

This C++ program contains 3 tasks to demonstrate different programming concepts: 1) Check if a year is a leap year using nested if-else statements. 2) Print all prime numbers between two integers specified by the user using nested loops. 3) Convert a decimal number to binary using loops and modulus operator.

Uploaded by

urooj tariq
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)
53 views5 pages

CP Assignment 1

This C++ program contains 3 tasks to demonstrate different programming concepts: 1) Check if a year is a leap year using nested if-else statements. 2) Print all prime numbers between two integers specified by the user using nested loops. 3) Convert a decimal number to binary using loops and modulus operator.

Uploaded by

urooj tariq
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/ 5

task 1: Write a C++ program to check whether the year (4 digit integer) entered by the user is a “leap year”

or “Not a
leap year”. (Hint: All years which are completely divisible by 4 are leap years (e.g. 2012, 2004 are leap years but 1971,
2006 are not). However a century year (year ending with 00 e.g. 1800, 2000 etc.) is not a leap year unless it is completely
divisible by 400. For instance 1200, 2400 are leap years but 1700, 1900 etc. are not. ) *Nested selection structure will be
applied.

In this program we use if and else statement inorder to find wheather the year we input is leap year or not

Code :
#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;
}
Output :

Task 2: Write a C++ program to print all prime numbers between two integers specified by the user. For instance if a
user enters two integers e.g. 4 and 10 then the program prints all prime numbers between 4 and 10 i.e. 5, 7. (Hint: A
prime number is a positive integer which is only divisible by 1 and itself e.g. 13 is a prime number but 15 is not.) *You
will require nested loop and if-else structures to solve this problem.

In order to print all prime number between two integers, we did it with both by usingr loop and if statement

Code:

#include<iostream>
using namespace std;
int main()
{
int i, k = 0, j;
cout << "Prime Numbers Between 1 to 100 are:\n";
for (i =1; i <= 100; i++)
{
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
k++;
}
}
if (k == 0 && i != 1)
cout << i << endl;
k = 0;
}
cout << endl;
return 0;
}

Output :

Task 3: Write a C++ program to convert a decimal number entered by the user into its binary equivalent. (Hint: Use loops
and modulus operator.)

In order to convert decimal numbers into binary number we use for loops

Code :

#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];
}
return 0;
}

Output :

#include <iostream>
using namespace std;
void DecimalToBinary(int n) {
int binaryNumber[100], num = n;
int i = 0;
while (n > 0) {
binaryNumber[i] = n % 2;
n = n / 2;
i++;
}
cout << "Binary form of " << num << " is ";
for (int j = i - 1; j >= 0; j--)
cout << binaryNumber[j];
cout << endl;
}
int main() {
DecimalToBinary(75);
DecimalToBinary(100);

return 0;
}

You might also like