0% found this document useful (0 votes)
11 views

c++ program copy 12th

The document contains multiple C++ programming tasks, including finding prime numbers, calculating the GCD, and determining the area of geometric shapes. It also includes programs for string manipulation, sorting arrays, and implementing object-oriented programming concepts. Each task is presented with code snippets and comments to guide the implementation.
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)
11 views

c++ program copy 12th

The document contains multiple C++ programming tasks, including finding prime numbers, calculating the GCD, and determining the area of geometric shapes. It also includes programs for string manipulation, sorting arrays, and implementing object-oriented programming concepts. Each task is presented with code snippets and comments to guide the implementation.
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
You are on page 1/ 4

C++ program to test whether Write a program in C++ to find } void area()

the inputted number is prime or the Greatest Common Divisor {


not (GCD) of two natural numbers. // function definition cout<<"Enter radius of
#include <iostream.h> #include <iostream.h> int min (int n1, int n2, int n3, int n4) sphere:";
void main() void main() { cin>>r;
{ { int low; a=4*3.14*r*r;
int prime, C = 0; int n1, n2; if (n1 < n2) cout<<"area of sphere="<<a;
cout << "Enter the number"; cout << "Enter the two natural low = n1; }
cin >> prime; numbers"; else };
for (int i = 2; i < prime; i++) cin >> n1 >> n2; low = n2;
{ while (n1 != n2) if (n3 < low) void main()
if (prime % i == 0) { low = n3; { sphere s;
C = 1; if (n1 > n2) if (n4 < low) s.area();
} n1 = n1 - n2; low = n4; }
if (C == 0) if (n2 > n1) return (low); C++ program to find the
cout << "The number " << n2 = n2 - n1; } smallest element in an array of
prime << " is a prime number"; } Write a C++ program to accept 10 floats using pointers.
else cout << "The GCD is: " << n1; an array of 10 integers and find #include<iostream.h>
cout << "The number " << } the smallest and largest #include<conio.h>
prime << " is not a prime number"; "Write a program in C++ to read element in the array. void main()
} a set of numbers from keyboard #include<iostream.h> {
C++ program to replace every and find out the largest number #include<conio.h> clrscr();
space in a string with a hyphen. in the given array. void main() float a[10], small, *p;
#include <iostream.h> #include <iostream.h> { int i;
#include <string.h> void main() int a[10], i, j, temp; cout << "Enter 10 numbers";
{ cout << "Enter 10 integers"; for (i = 0; i < 10; i++)
int main() int num[10], max; for(i=0; i<10; i++) {
{ cout << "Enter the number"; { cin >> a[i];
int len; for (int i = 0; i < 10; i++) cin >> a[i]; }
char str[80]; { } p = &a[0];
cout << "Enter a string"; cin >> num[i]; for(i=0; i<10; i++) small = *p;
cin.get(str, 80); } { for(i = 0; i < 10; i++)
len = strlen(str); max = num[0]; for(j=0; j<10-i-1; j++) {
for (int i = 0; i < len; i++) for (int j = 1; j < 10; j++) { if(*p < small)
if (str[i] == ' ') { if(a[j] > a[j+1]) {
str[i] = '-'; if (max < num[j]) { small = *p;
cout << "The final string is"; { temp = a[j]; }
cout << str; max = num[j]; a[j] = a[j+1]; p++;
} } a[j+1] = temp; }
Write an object-oriented program in } } cout << "\nsmallest element="
C++ to read an integer number and cout << "The largest number in } << small;
find the sum of digits of integer.
the array is " << max; } getch();
Hint: input 125, output 8, i.e.,
} cout << "\nThe smallest number }
1 + 2 + 5 = 8. Write a program in C++ that in array is=" << a[0]; Write a C++ program to accept a
#include<iostream.h>
inputs and stores 10 numbers in cout << "\nThe smallest number sentence (maximum 50
#include<conio.h>
an array and prints the sum and in array is=" << a[9]; characters) and print the
void main()
average of the array elements. getch(); sentence in reverse."
{
#include <iostream.h> } #include<iostream.h>
int val, num, sum = 0;
void main() Write a class-based program in #include<conio.h>
cout << "Enter the number: ";
{ C++ to find the area of a
cin >> val;
int num[10], sum; triangle. {
num = val;
float avg = 0.0; char S[50];
while (num != 0) #include <iostream.h>
cout << "Enter the 10 int i, L;
{ #include <conio.h>
elements"; cout<<"Enter a sentence(50
sum = sum + num % 10; class triangle
for (int i = 1; i <= 10; i++) chars)";
num = num / 10; {
cin >> num[i]; cin.getline(S,50);
} private:
sum = 0; for(i=1;S[i]!='\0';i++)
cout << "The sum of the digits float a, b, h;
for (i = 1; i <= 10; i++) {
of " << val << " is " << sum; public:
sum = sum + num[i]; L++;
} void area();
avg = sum / 10; }
Implement a circle class. Each };
object of this class will represent a cout << "The sum of numbers cout<<"Sentence in reverse";
circle, accepting its radius value as a is: " << sum << endl; }
void triangle::area()
float. Include an area() function cout << "The average of the Write a program in C++ to
{
which will calculate the area of the array element is: " << avg; accept three integers from
circle. cout << "Enter base and height"
} keyboard and find greatest
#include <iostream.h> << endl;
Write a C++ program to find the number with using condition
cin >> b >> h;
smallest of four given integers control.
class circle { a = 0.5 * b * h;
using the min() function to void main()
float a; cout << "\nArea of triangle = "
return the smallest of four given {
float r; << a;
integers (int min(int a, int b, int int a, b, c;
}
c, int d) cout << "Enter three no. = ";
public: .#include <iostream.h> cin >> a >> b >> c;
void area (void); void main()
void main() int big = a;
}; {
{ if (b < big)
void circle::area (void) { triangle t;
int a, b, c, d, small; big = b;
cout << "Enter radius of circle"; t.area();
int min (int, int, int, int); // if (c < big)
cin >> r; }
Prototype big = c;
a = 3.142 * r * r; class-based C++ program to
cout << "Enter the four cout << "\n Greatest no = " <<
cout << "The area of a circle is"; find the area of a sphere.
numbers:" << endl; big;
cout << a; #include<iostream.h>
cin >> a >> b >> c >> d; getch();
} #include<conio.h>
small = min (a, b, c, d); // }
void main() { class sphere
function call
circle C; {
cout << "The smallest number
C.area(); float r, a;
is:" << small;
} public:
Write a C++ program to #include<iostream.h> void main() for (int i = 1; i <= n; i++) {
overload the add() function #include<conio.h> { f = f * i;
which will add two integers class fibonacci char string[80]; }
[add(int, int)] and three integers { int len, count, i; cout << "Factorial is: " << f <<
[add(int, int, int)]. private: count = 0; endl;
#include<iostream.h> long int f0, f1, fib; cout << "Enter a string: " << }
#include<conio.h> public: endl;
int add(int, int); fibonacci(void); gets(string); void main() {
int add(int, int, int); void process(void); len = strlen(string); fact obj;
void display(void); for (i = 0; i < len; i++) obj.getdata();
void main() void display1(void); { obj.display();
{ }; if (string[i] == 'J') }
int a, b, p, q, r, a1, a2; count++; write a C++ program that inputs
cout << "\nEnter two numbers"; fibonacci::fibonacci(void) } a word (with a maximum length
cin >> a >> b; { cout << "Occurrence of of 15 characters) from the user
a1 = add(a, b); f0 = 0; character 'J' in given string is: " << and prints each of its characters
cout << "\nAddition of two f1 = 1; count; on a new line in reverse order
numbers=" << a1; } } #include <iostream.h>
cout << "\nEnter three Write a program in C++ that #include <string.h>
numbers"; void fibonacci::display1(void) inputs and stores 10 numbers in
cin >> p >> q >> r; { an array and prints numbers in void main()
a2 = add(p, q, r); cout << f0 << "\t" << f1 << "\t"; reverse order. {
cout << "\nAddition of three } #include <iostream> char x[16]; int i;
numbers=" << a2; using namespace std; cout << "Enter a word ";
getch(); void fibonacci::process(void) cin >> x;
} { int main() { i = strlen(x);
fib = f0 + f1; double a[10]; for (i = i - 1; i >= 0; i--)
int add(int n1, int n2) f0 = f1; cout << "Enter 10 numbers:\n"; {
{ f1 = fib; for (int i = 0; i < 10; i++) { cout << x[i] << endl;
return n1 + n2; } cout << "a[" << i << "]: "; }
} cin >> a[i]; }
void fibonacci::display(void) } Write a program in C++ using
int add(int m1, int m2, int m3) { cout << "The array in reverse OOP technique to find the
{ cout << fib << "\t"; order is:\n"; AREA of Circle.
return m1 + m2 + m3; } for (int i = 9; i >= 0; i--) { #include <iostream.h>
} cout << a[i] << " "; class circle
"Write a program in C++ to void main() } {
calculate Fibonacci series of 'n' { cout << endl; private:
int i, n; return 0; int r;
numbers using constructor." fibonacci f; } float A;
// Program to generate cout << "\n Enter number of Write a C++ program to find the public:
Fibonacci series elements" << endl; largest of four given integers void getradius()
#include <iostream.h> cin >> n; using the max() function to {
#include <conio.h> f.display1(); return the largest of four given cin >> r;
class Fibonacci for (i = 3; i <= n; i++) integers: int max(int, int, int, int) }
{ { #include<iostream.h> void print()
private: f.process(); #include<conio.h> {
long int f0, f1, fib; f.display(); void main() { A = 3.14 * r * r;
public: } int max(int, int, int, int); cout << "Area of circle is"
Fibonacci(void); } cout << "Enter four integers:"; << A;
void process(void); Write a program in C++ to find int w, x, y, z; }
void display(void); the sum of the contents of an cin >> w >> x >> y >> z; };
}; array with the help of a pointer. cout << "Maximum = " <<
#include<iostream.h> max(w, x, y, z); void main()
Fibonacci::Fibonacci(void) { void main() } {
f0 = 0; { circle c;
f1 = 1; int a[5] = {1, 2, 3, 4, 5}; int max(int n1, int n2, int n3, int n4) c.getradius();
} int i, s = 0; { c.print();
for (i = 0; i < 5; i++) int max = n1; }
void Fibonacci::process(void) { { if (n2 > max) max = n2; To find Greatest Common
fib = f0 + f1; s = s + *p; p++; if (n3 > max) max = n3; Divisor of two natural numbers.
f0 = f1; } if (n4 > max) max = n4; #include <iostream.h>
f1 = fib; cout << "sum=" << s; return max; void main()
} } } {
Write a C++ Program to find the Write an Object-Oriented int n1, n2;
void Fibonacci::display(void) { factorial of 1 of 5 numbers. Program in C++ that prints the cout << "Enter the two natural
cout << fib << "\t"; #include <iostream.h> factorial of a given number. numbers";
} void main() #include<iostream.h> cin >> n1 >> n2;
{ while (n1 != n2)
void main() { int fact, i, j; class fact { {
int i, n; fact = 1; private: if (n1 > n2)
Fibonacci F; cout << "Number" << "\t" << int n; n1 = n1 - n2;
cout << "\n Enter number of "Factorial"; public: if (n2 > n1)
elements" << endl; for (j = 1; j <= 5; j++) void getdata(); n2 = n2 - n1;
cin >> n; { void display(); }
for (i = 1; i <= n; i++) { fact = fact * j; }; cout << "The GCD is: " << n1;
F.process(); cout << j << "\t" << fact << }
F.display(); endl; void fact::getdata() {
} } cout << "Enter value of n\n";
} } cin >> n;
Write a class based C++ C++ program to count }
program to print 20 terms of occurrence of character 'J'
Fibonacci series. [Hint: #include <iostream.h> void fact::display() {
Fibonacci series 0, 1, 1, 2, 3, 5, #include <string.h> int f = 1;
..]"
write a program in C++ to find variables from user. Include two num = val; using namespace std;
the sum of the first 100 natural more functions in it, one while (num != 0)
numbers. function calculates average and { void main()
#include <iostream.h> other prints it. sum = sum + num % 10; {
void main() #include<iostream.h> num = num / 10; char a[80];
{ class average } int i, bsp;
int d, sum = 0; { cout << "The sum of the digits cout << "Enter a sentence = ";
d = 1; private: of " << val << " is " << sum; gets(a);
while (d <= 100) float a, b, c, s; } bsp = i = 0;
{ public: Write a program in C++ to while (a[i] != '\0')
sum = sum + d; average() declare the array of 10 floats {
d = d + 1; { and find the largest. if (a[i] == ' ')
} cout << "Enter 3 nos."; #include <iostream> {
cout << "sum" << sum; cin >> a >> b >> c; using namespace std; bsp++;
} } }
Write a C++ Program by using void cal() int main() { i++;
swap function to interchange { float arr[10]; }
given two numbers. s = (a + b + c) / 3; cout << "Enter 10 float values: "; cout << "\nTotal Blank space = "
void swap(int &x, int &y); } for(int i = 0; i < 10; i++) { << bsp;
# include <iostream.h> void print() cin >> arr[i]; getch();
void swap (int & x, int & y); { } }
cout << "Average: " << s; Write a program in C++ to
void main() } float largest = arr[0]; accept two integer values in the
{ ~average() { } for(int i = 1; i < 10; i++) { main function, pass them to
int a, b; }; if(arr[i] > largest) { function great() using call by
cout << "Enter values for a and largest = arr[i]; value, and find the greater number.
b"; void main() } The function great() should not return
cin >> a >> b; { } any value.
void great(int, int);
swap(a, b); average ob;
cout << "After swapping" << ob.cal(); cout << "The largest value is: "
<< largest << endl; void main() {
endl; ob.print();
return 0; int a, b;
cout << "a = " << a; }
Implement class GCD which have } cout << "Enter Two Numbers =
cout << "b = " << b;
member function (a/c), which Write a program in C++ to ";
}
calculate greatest common divisor initialize the array to 10 floats cin >> a >> b;
of two number entered during and print all the array elements great(a, b);
void swap (int & x, int & y) program execution. Print() will Print
using pointer. getch();
{ common divisor of two number
void main() }
int temp = x; entered during program execution
x = y; #include <iostream.h> {
float a[10] = {1.2, 2.3, 4.5, 5.6, void great(int p, int q) {
y = temp; #include <conio.h>
7.8, 8.9, 9.1, 6.7, 10.2, 10.4}; if (p > q) {
} class GCD
float *p; cout << p << " is greater";
Implement a class temperature. {
int i; } else {
Include a constructor in it which private:
p = &a[0]; cout << q << " is greater";
accepts value of temperature int a, b;
cout << "\n Array="; }
from user in degree Celsius. public:
for (i = 0; i <= 9; i++) }
Include two functions in it, one void gcd() Write a program in C++ to accept a
of which calculates its { {
string from the keyboard and copy it
equivalent temperature in cout << "\n Enter Two cout << *p << " "; into another string without using the
degree Fahrenheit and other numbers:"; p++; library function
function prints the answer: cin >> a >> b; } void main()
𝑐 32 while (a != b) getch(); {
=𝐹− }
5 9 { char a[80], b[80];
#include<iostream.h> if (a > b) Write a C++ program to find the int i;
class temperature a = a - b; GCD (Greatest Common i = 0;
{ else Divisor) of two numbers entered cout << "Enter a String = ";
private: b = b - a; by the user cin >> a;
float f, t; } #include<iostream.h> while (a[i] != '\0')
public: } void main() {
temperature() void print() { b[i] = a[i];
{ { int a, b, i; i++;
cout << "Enter temp. in cout << "\n GCD=" << a; cout << "Enter Two Numbers"; }
Celsius: "; } cin >> a >> b; b[i] = '\0';
cin >> t; }; int gcd = 1, min; cout << "\n copied string = " <<
} if (a > b) b;
void cal() void main() { getch();
{ { min = a; }
f = 9 * t / 5 + 32; GCD g; } Write a program in C++ to accept two
} g.gcd(); else integer numbers in main() and find
void print() { the sum of those numbers using a
g.print(); function by address.
{ } min = b;
cout << "Temp in } int Add(int *, int *);
Write an object oriented void main()
Fahrenheit: " << f; program in C++ to read an for (i = 1; i <= min; i++)
} { {
integer number and find the int N1, N2;
~temperature() { } sum of digits of integer [Hint: if (a % i == 0 && b % i == 0)
}; gcd = i; cout << "Enter two No";
input 125 output 8 i.e. 1 + 2 + 5 = cin >> N1 >> N2;
8] }
void main() cout << "GCD=" << gcd; int sum = Add(&N1, &N2);
#include <iostream> cout << "Addition = " << sum;
{ #include <conio.h> }
temperature ob; Write a program in C to accept a line }
ob.cal(); from the keyboard and count the
void main() total number of blank spaces in a int Add(int *x, int *y)
ob.print(); { line. The program should print the {
} int val, num, sum = 0; original string and blank spaces. int S = *x + *y;
Implement a class average. cout << "Enter the number: "; #include <iostream> return S;
Include a constructor in it which cin >> val; #include <conio.h> // For getch() }
will accept value of three
Write a program in C++ to accept a if (a > max) cin >> n[i];
terms (use n <= 18 in this
string from the keyboard and find { }
the length of the string without using case)
#include<iostream.h> max = a; for (i = 0; i <= 9; i++)
a library function.
void main() } {
void main()
{ cout << "The largest number is: for (j = i; j <= 9; j++)
{
int f0, f1, f, n; "; {
char S[80];
f0 = 0; cout << max; if (n[i] > n[j])
cout << "Enter a string" << endl;
f1 = 1; } {
cin.getline(S, 80);
clrscr(); Implement a circle class. Each temp = n[i];
int i = 0, L = 0;
cout << "Fibonacci series\n"; object of this class will n[i] = n[j];
while (S[i] != '\0')
cout << "\n" << f0 << "\n" << f1; represent a circle, accepting its n[j] = temp;
{
for (n = 1; n <= 13; n++) radius value as float. Include an }
i++;
{ area() function which will }
L++;
f = f0 + f1; calculate the area of circle. }
}
cout << "\n" << f; cout << "Sorted list is:" << endl;
cout << "Length of the string = "
f0 = f1; // C++ program to implement a for (i = 0; i <= 9; i++)
<< L;
f1 = f; circle class {
}
} #include <iostream.h> cout << n[i] << endl;
What is an Armstrong number?
} }
Write a program in C++ to check
whether the given number is Write a C++ program to find class circle { }
Armstrong or not. factorial of a natural number float a; Write an Object-Oriented
Ans.: Armstrong number: "If the input during program execution. float r; Program in C++ that prints the
sum of the cubes of digits of a // Program to find factorial of a factorial of a given number.
number is equal to the original number public: #include <iostream.h>
number, then the number is said #include<iostream.h> void area (void); class fact
to be an Armstrong number." e.g., #include<conio.h> }; {
153 is an Armstrong number. private:
void main ()
C++ Program to find whether void circle::area (void) { int n;
the number is Armstrong or not: {
int fact, number; cout << "Enter radius of circle"; public:
#include <iostream> cin >> r; void getdata();
#include <conio.h> clrscr();
fact = 1; a = 3.142 * r * r; void display();
void main() { cout << "Enter the number" << cout << "The area of a circle is"; };
int n, dn, temp, d; endl; cout << a;
cout << "Enter a number"; cin >> number; } void fact::getdata()
cin >> n; for (int i = 1; i <= number; i++) {
dn = n; { void main () { cout << "Enter value of n\n";
temp = 0; fact *= i; circle C; cin >> n;
while (dn != 0) { } C.area (); }
d = dn % 10; }
cout << "The factorial of the
temp = temp + (d * d * d); Write C++ program to print the input void fact::display()
inputted number is: " << fact;
dn = dn / 10; string in a reverse order using {
} } function which first locates the end
Write a program in C++ to for (int i = 1, f = 1; i <= n; i++)
if (n == temp) { of string. Then it swaps the first
calculate and print factorial of character with the last character, the
{
cout << n << " is an
first 10 numbers. second character with the second f = f * i;
Armstrong number.";
} else { //C++ program to calculate and last character and so on." }
cout << n << " is not an print factorial of first 10 numbers / /program to reverse the string cout << "Factorial = " << f <<
Armstrong number."; #include<iostream.h> #include <iostream.h> endl;
} #include<conio.h> #include <stdio.h> }
} void main() #include <string.h> Write a program in C++ to
Write a program in C++ to check { void reverse (char str[], int l); initialize the array of 10 integers
whether the given integer is a int fact, n, i; void main () and find the sum of all the
palindrome or not { elements of array.
clrscr();
// C++ program to find whether the char str[80]; #include<iostream.h>
given integer is palindrome or not #include<conio.h>
cout<<"Number"<<"\t"<<"Factorial cout << "Enter the string";
#include<iostream.h> void main()
"; gets (str);
#include<conio.h>
for (n=1; n<=10; n++) int len = strlen (str); {
{ reverse (str, len); fact obj;
void main()
{ fact = 1; } obj.getdata();
int n, dn, temp=0, d; for (i = 1; i<=n; i++) obj.display();
clrscr(); { void reverse (char str[], int l) }
cout << "Enter a number"; fact = fact*i; {
cin >> n; } int mid = l / 2; void main()
dn = n; cout<<endl<<n<<"\t"<<fact; for (int i = 1; i <= mid; i++) {
while (dn != 0) { int a[10], i, sum;
}
{ char temp = str[i]; cout << "Enter 10 Elements ";
}
d = dn % 10; str[i] = str[l]; for (i = 0; i < 10; i++)
temp = (temp * 10) + d; Write a program in C++ that
finds the largest number among str[l] = temp; {
dn = dn / 10; cin >> a[i];
three numbers. l--;
}
// Program to find largest number } }
if (n == temp)
{ #include <iostream.h> cout << "Reverse of string is:"; sum = 0;
cout << "The number " << n void main() puts (str); cout << "\n The Array = ";
<< " is palindrome"; { } for (i = 0; i < 10; i++)
} int a, b, c, max; Write a C++ program to sort 10 {
else cout << "Enter three numbers" integer numbers in ascending cout << a[i] << " ";
{ << endl; order. sum = sum + a[i];
cout << "The number " << n #include<iostream.h> }
cin >> a >> b >> c;
<< " is not palindrome"; #include<conio.h> cout << "\n sum of all element =
if (b > c)
} " << sum;
{
} void main() getch();
Write a program in C++ to max = b;
{ }
display a Fibonacci series of 15 }
int n[10], i, j, temp;
terms. else
cout << "Enter 10 integers" <<
OR {
endl;
Write a program in C++ to max = c;
for (i = 0; i <= 9; i++)
display a Fibonacci series of 20 }
{

You might also like