Archive for 2019
Write a program in C++ to print Fibonacci series upto number entered by user
#include <iostream.h>
#include <conio.h>
int main() {
int n, c, first = 0, second = 1, next;
cout<<"*** FIBONACCI SERIES ***"<<endl;
cout << "Enter number of terms: ";
cin >> n;
cout << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << " ";
}
return 0;
}
Output:
Write a program in C++ to find factorial of entered number
#include <iostream.h>
#include <conio.h>
int main() {
int num,factorial=1;
cout << "Enter Number: ";
cin >> num;
for (int a=1;a<=num;a++) {
factorial=factorial*a;
}
cout<<"Factorial: "<< factorial << endl;
return 0;
}
Output:
Write a program in C++ to check whether the entered number if prime number or not
#include <iostream.h>
#include <conio.h>
int main() {
{
int num, i, count = 0;
cout << "Enter the number to be checked : ";
cin >> num;
if (num == 0)
{
cout << "\n" << num << " is not prime";
exit(1);
}
else
{
for(i=2; i < num; i++)
if (num % i == 0)
count++;
}
if (count > 1)
cout << "\n" << num << " is not prime.";
else
cout << "\n" << num << " is prime.";
return 0;
}
Output:
Write a program in C++ to find area of Triangle, Rectangle or Circle using IF ELSE (must be menu driven)
#include <iostream.h>
#include <conio.h>
int main() {
int shape, a, b;
char ans;
Repeat:
system("CLS");
cout << "Select Shape:" << endl;
cout << "1.Triangle" << endl;
cout << "2.Rectangle" << endl;
cout << "3.Circle" << endl;
cin >> shape;
if(shape==1) {
cout << "Base: ";
cin >> a;
cout << "Altitude: ";
cin >> b;
cout << endl << "Area of Triangle: " << 0.5*a*b;
} else if(shape==2) {
cout << "Length: ";
cin >> a;
cout << "Breadth: ";
cin >> b;
cout << endl << "Area of Rectangle: " << a*b;
} else if(shape==3) {
cout << "Radius: ";
cin >> a;
cout << endl << "Area of Circle: " << 3.14*a*a;
} else {
cout << "Invalid Shape";
}
cout << endl << "Do you want more calculation? (y/n)";
cin >> ans;
if (ans=='y') {
goto Repeat;
}
getch();
return 0;
}
Output:
Write a program in C++ to find whether the entered year is LEAP YEAR or not
#include <iostream.h>
#include <conio.h>
int main() {
int year;
cout << "Enter Year: ";
cin >> year;
(year%4)==0 ? cout<<"Leap Year" : cout<<"NOT Leap Year";
getch();
return 0;
}
Output:
Write a program in C++ to find SIMPLE INTEREST
#include <iostream.h>
#include <conio.h>
int main() {
//p=principle, r=rate, t=time in years
float p, r, t;
cout << "Principle: ";
cin >> p;
cout << "Rate: ";
cin >> r;
cout << "Time: ";
cin >> t;
cout << endl << "Simple Interest:" << p*r*t/100;
getch();
return 0;
}
Output:
Write a program in C++ to find highest of 3 numbers using conditional operator
#include <iostream.h>
#include <conio.h>
int main() {
int a, b, c, big;
cout << "First No: ";
cin >> a;
cout << "Second No: ";
cin >> b;
cout << "Third No: ";
cin >> c;
big = a > b ? ( a > c ? a : c) : (b > c ? b : c) ;
cout << "Highest Number: " << big;
getch();
return 0;
}
Output:
Uses of C++
- C++ is being highly used to write device drivers and other softwares that rely on direct manipulation of hardware under realtime constraints
- C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts
INTRODUCTION to C++
- 1. C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs.
- 2. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
- 3. C++ is a statically typed, compiled, general-purpose, case-sensitive, programming language that supports procedural, object-oriented, and generic programming.
- 4. C++ supports OOPS
