You are on page 1of 11

ASSIGNMENT :1

QUESTION:

Write a C++ program to print “Hello World”.

ALGORITHM:

1. START
2. INCLUDE ALL THE NECESSARY PACKAGES.
3. PRINT Hello World.
4. STOP

CODE:

#include<iostream>

int main()

std::cout<<"93.1.1------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------n";


std::cout<<"\n Hello World";

return 0;

OUTPUT:
QUESTION:

Write a C++ program to add two numbers.

ALGORITHM:

1. START
2. INITIALIZE TWO NUMBERS
3. ADD TWO NUMBERS
4. PRINT OUTPUT
5. STOP

CODE:

#include<iostream>
class addition
{
public:
int add(int a,int b)
{
int c;
c=a+b;
return c;
}
};
int main()
{
addition c;
int x,y,z;
std::cout<<"93.1.2------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------n";
std::cout<<"\n Enter the values of X and Y:";
std::cin>>x>>y;
z=c.add(x,y);
std::cout<<"The Sum of Two Number is:"<<z;

OUTPUT:
QUESTION:

Write a C++ program to count total prime numbers within a given range.

ALGORITHM:

1. START
2. TAKE RANGE FROM USER
3. RUN A LOOP
4. CHECK NUMBER IS PRIME OR NOT
5. IF YES INCREASE COUNT
6. PRINT TOTAL NO.OF PRIMES
7. STOP

CODE:
#include <iostream>
using namespace std;
int main()
{
int low, high, i, flag,count;
count=0;
std::cout<<"93.1.3------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------n";
cout << "\n Enter two numbers(intervals): ";
cin >> low >> high;
cout << "\n Prime numbers between " << low << " and " << high << " are: ";
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
cout << low << " ";
count++;
}
++low;
}
cout << "\n Total prime within the given range is:" << count;
return 0;
}
OUTPUT:
QUESTION:

Write a C++ program to calculate m^n without using any pre defined function.

ALGORITHM:

1. START
2. TAKE INPUT FROM USER
3. CALCULATE m^n
4. PRINT THE RESULT
5. STOP

CODE:

#include<iostream>
class power
{
public:
int pow(int a,int b)
{
int c=1;
for(int i=1;i<=b;i++)
c=c*a;
return c;
}
};
int main()
{
power o;
int x,y,z;
std::cout<<"93.1.4------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------";
std::cout<<"Enter the values of X and power Y:";
std::cin>>x>>y;
z=o.pow(x,y);
std::cout<<"The Answer:"<<z;
}

OUTPUT:
QUESTION:

Write a C++ program to calculate the factorial of a number.

ALGORITHM:

1. START
2. TAKE INPUT FROM USER
3. FIND THE FACTORIAL
4. PRINT THE OUTPUT
5. STOP

CODE:

#include<iostream>
using namespace std;
int main() {
int num,factorial=1;
std::cout<<"93.1.5------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------";
cout<<" \nEnter Number To Find Its Factorial: ";
cin>>num;
for (int a=1;a<=num;a++) {
factorial=factorial*a;
}
cout<<"\nFactorial of Given Number is ="<<factorial<<endl;
return 0;
}

OUTPUT:
QUESTION:

Write a C++ program to swap two numbers without using third variable and “+” & “-“ sign.

ALGORITHM:

1. START
2. TAKE TWO NUMBER AS INPUT
3. USE TERNARY OPERATOR TO SWAP TWO NUMBER
4. PRINT THE RESULT
5. STOP

CODE:

#include<iostream>

int main() {
int num1, num2;

std:: cout<<"93.1.6------Name: Saurav Chatterjee ,Roll:93, Section: 3C------- ";

std::cout<<"\nEnter First Number : ";


std::cin>>num1;

std::cout<<"\nEnter Second Number : ";


std::cin>>num2;
std::cout<<"NUMBERS BEFORE EXCHANGE num1: "<<num1<<" num2: "<<num2;

num1 = num1 ^ num2;


num2 = num1 ^ num2;
num1 = num1 ^ num2;
std::cout<<"\n NUMBERS AFTER EXCHANGE num1: "<<num1<<" num2 "<<num2;

return(0);
}

OUTPUT:
QUESTION:

Write a C++ program to generate a Fibonacci series in C++. Take the range from user.

ALGORITHM:

1. START
2. TAKE THE RANGE FROM USER
3. GENERATE THE FIBONACCI SERIES UPTO THE RANGE
4. PRINT THE RESULT
5. STOP

CODE:

#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
std:: cout<<"93.1.7------Name: Saurav Chatterjee ,Roll:93, Section: 3C------- ";
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i)
{
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
return 0;
}

OUTPUT:
QUESTION:

Write a C++ program to check whether a year is leap year or not.

ALGORITHM:

1. START
2. TAKE YEAR AS INPUT FROM USER
3. CHECK WHETER IT IS LEAP YEAR OR NOT
4. DELIVER AN APPROPRIATE MESSAGE
5. STOP

CODE:

#include <iostream>
using namespace std;
int main()
{
int year;
std:: cout<<"93.1.8------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------n";
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:
QUESTIONS:

Write a C++ program to check whether a number is palindrome or not.

ALGORITHM:

1. START
2. TAKE INPUT FROM USER
3. CHECK PALINDROME
4. DISPLAY A RELEVANT MESSAGE
5. STOP

CODE:

#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
std:: cout<<"93.1.9------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------n";
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;
}

OUTPUT:
QUESTION:

Write a C++ program to check whether a number is perfect or not.

ALGORITHM:

1. START
2. TAKE INPUT FROM USER.
3. CHECK FOR PERFECT NUMBER
4. DISPLAY RELEVANT MESSAGE TO USER
5. STOP
CODE:

#include<iostream>
using namespace std;
int main ()
{
int i, num, div, sum=0;

std:: cout<<"93.1.10------Name: Saurav Chatterjee ,Roll:93, Section: 3C-------n";


cout << "Enter the number to be checked : ";
cin >> num;
for (i=1; i < num; i++)
{
div = num % i;
if (div == 0)
sum = sum + i;
}
if (sum == num)
cout << "\n" << num <<" is a perfect number.";
else
cout << "\n" << num <<" is not a perfect number.";
return 0;
}

OUTPUT:

You might also like