0% found this document useful (0 votes)
18 views18 pages

Computer Program:11: To Print A Sentence

The document contains 20 computer programs written in C++ to demonstrate basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, classes, and patterns. Each program contains the source code, input/output and description to solve problems like printing text, storing and displaying numbers, addition, swapping values, finding quotient and remainder, reversing a number, calculating sum of natural numbers, finding factors, checking prime numbers, and printing patterns.

Uploaded by

Priyanka Sahu
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)
18 views18 pages

Computer Program:11: To Print A Sentence

The document contains 20 computer programs written in C++ to demonstrate basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, classes, and patterns. Each program contains the source code, input/output and description to solve problems like printing text, storing and displaying numbers, addition, swapping values, finding quotient and remainder, reversing a number, calculating sum of natural numbers, finding factors, checking prime numbers, and printing patterns.

Uploaded by

Priyanka Sahu
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/ 18

COMPUTER PROGRAM:11

TO PRINT A SENTENCE:
#include<iostream>

using namespace std;

class hell_world

public:

void show()

cout<<"HELLO WORLD";

};

int main()

hell_world ob;

ob.show();

return 0;

OUTPUT:
COMPUTER PROGRAM:12
TO STORE AND DISPLAY TWO NUMBERS:
#include<iostream>

using namespace std;

class disp_num

public:

int a,b;

void show()

cout<<"The first number is: "<<a<<endl;

cout<<"The second number is: "<<b<<endl;

};

int main()

disp_num ob;

cout<<"Enter the number"<<endl;

cin>>ob.a>>ob.b;

ob.show();

return 0;

OUTPUT:
COMPUTER PROGRAM:13
TO ADD TWO NUMBERS:
#include<iostream>

using namespace std;

class add_num

public:

int a,b,sum;

void input()

cout<<"Enter the two numbers:"<<endl;

cin>>a>>b;

sum=0;

void add()

sum=a+b;

void display()

cout<<"The sum of the numbers "<<a<<" and "<<b<<" is: "<<sum;

};

int main()

add_num ob;

ob.input();

ob.add();

ob.display();
return 0;

OUTPUT:
COMPUTER PROGRAM:14
TO SWAP THE TWO NUMBERS:
#include<iostream>

using namespace std;

class swap_num

public:

int a,b;

void input()

cout<<"Enter the two numbers: ";

cin>>a>>b;

cout<<"Before swap:"<<endl;

cout<<"a="<<a<<" and b="<<b<<endl;

void change()

a=a+b;

b=a-b;

a=a-b;

void display()

cout<<"After swap: "<<endl;

cout<<"a="<<a<<" and b="<<b;

};

int main()

{
swap_num ob;

ob.input();

ob.change();

ob.display();

return 0;

OUTPUT:
COMPUTER PROGRAM:15
TO PRINT THE QUOTIENT AND REMAINDER:
#include<iostream>

using namespace std;

class quo_rem

public:

int d,div,quo,rem;

void input()

cout<<"Enter the divisor and dividend: ";

cin>>d>>div;

quo=0;

rem=0;

void calculate()

quo=div/d;

rem=div%d;

void output()

cout<<"The quotient and the remainder is: "<<quo<<" and "<<rem;

};

int main()

quo_rem ob;

ob.input();
ob.calculate();

ob.output();

return 0;

OUTPUT:
COMPUTER PROGRAM:16
TO REVERSE A NUMBER:
#include<iostream>

using namespace std;

class rev_num

public:

int num,rev;

void input()

cout<<"Enter the number :";

cin>>num;

rev=0;

void reverse()

while(num>0)

int temp=num%10;

rev=rev*10+temp;

num=num/10;

void display()

cout<<"The reversed number is: "<<rev;

};

int main()
{

rev_num ob;

ob.input();

ob.reverse();

ob.display();

return 0;

OUTPUT:
COMPUTER PROGRAM:17
TO FIND THE SUM OF ‘N’NATURAL NUMBER:
#include<iostream>

using namespace std;

class sum_num

public:

int n,s;

void input()

cout<<"Enter the number of terms :";

cin>>n;

s=0;

void sum()

for(int i=1;i<=n;i++)

s=s+i;

void display()

cout<<"The sum is: "<<s;

};

int main()

sum_num ob;

ob.input();

ob.sum();
ob.display();

return 0;

OUTPUT:
COMPUTER PROGRAM:18
TO FIND FACTORS OF A NUMBER:
#include<iostream>

using namespace std;

class fact_num

public:

int num;

void input()

cout<<"Enter the number: ";

cin>>num;

void show()

for(int i=1;i<=num;i++)

if(num%i==0)

cout<<i<<",";

};

int main()

fact_num ob;

ob.input();

ob.show();

return 0;

}
OUTPUT:
COMPUTER PROGRAM:19
TO CHECK IF THE NUMBER IS A PRIME NUMBER:
#include<iostream>

using namespace std;

class prime_num

public:

int num;

void input()

cout<<"Enter the number to be checked ";

cin>>num;

void check()

int c=1;

for(int i=2;i<=num/2;i++)

if(num%i==0)

c=0;

if (c==1)

cout<<"Prime number";

else

cout<<"Composite number";

};

int main()

{
prime_num ob;

ob.input();

ob.check();

return 0;

OUTPUT:
COMPUTER PROGRAM:20
TO PRINT A PATTERN:
#include<iostream>

using namespace std;

class pattern_1

public:

void display()

for(int i=1;i<=5;i++)

for(int j=1;j<=i;j++)

cout<<i;

cout<<endl;

};

int main()

pattern_1 ob;

ob.display();

return 0;

}
OUTPUT:

You might also like