Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

C++ Programs

1. C++ program to calculate average marks scored by a


student

include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int m1,m2,m3,avg;

scanf("%d",&m1);
scanf("%d",&m2);
scanf("%d",&m3);

avg=(m1+m2+m3)/3;

printf("Average = %d",avg);

getch();
}

2. C++ Program to find the area and perimeter of a circle and rectangle.

#incude<stdio.h>
#include<conio.h>
void main()
{
int length, breadth, area, perimeter;
printf("\n Enter the length & breadth of a Rectangle");
scanf("d",&length,&breadth);
area=length*breadth;
perimeter=2*(length+breadth);
printf("\n Area of the Rectangle=%d",area);
printf("\n Breadth of the Rectangle=%d",perimeter);
getch();

3. Program to swap two numbers

// swap two numbers without using a temporary variable


// the numbers can be either integers or floats
 
#include <iostream>
 
using namespace std;
 
int main()
{
float a = 1.7;
float b = -7.1;
 
cout << "a = " << a << " b = " << b << endl;
// swap a with b
a = a + b;
b = a - b;
a = a - b;
cout << "after swapping a with b:" << endl;
cout << "a = " << a << " b = " << b << endl;
 
cin.get(); // wait
return EXIT_SUCCESS;
}

4. Program to find largest of three numbers


//The following is very simple program that will find the largest of any 3
numbers 

#include <iostream> 
using namespace std;
int main()
{
int n1, n2, n3, largest;

cout <<"Enter any 3 numbers ";


cin >>n1;
cin >>n2;
cin >>n3;

if(n1 > n2 && n1 > n3)


largest= n1;
else if(n2 >n1 && n2 >n3)
largest= n2;
else
largest= n3;

return 0;

5. Program to find the maximum number among three numbers

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){int n, numList[3];


int returnval = 0;
if(argc == 4){for(n = 0; n < 3; n++){numList[n] = atoi(argv[n + 1]);}
printf("the biggest one is %i.\n",
biggestInt(numList));}else{printf("Gimme three numbers.\n");
returnval = 1;}
return returnval;}

int biggestInt(int *list, int listSize){ int biggest;


if(listSize < 1) return 0;
listSize--;
biggest = list[listSize];
do{listSize--;
if(list[listSize] > biggest) biggest = list[listSize]; }while(listSize >= 0);
return biggest;} 

6. C++ Program to generate Fibonacci series.

#include <iostream>
using namespace std;

int main(){
long a;
cout << "Enetr Limit you want\n\n";
cin >> a;
cout << "Fibonachi numbers < " << a << endl;
long f = 0, f2, f1 = 1;
while(true) {
f2 = f + f1;
if(f2 > a) {
break;
}
cout << " " << f2 << endl;
f = f1;
f1 = f2;
}
system("pause");
return 0;
}

7. Program to perform string manipulation.

#include <string> 
    using namespace std;

 string name; 
    cin >> name; 
       // read string until the next separator 
       // (space, tab, newline)

    getline (cin, name); 


       // read a whole line into the string name

    if (name == "") 


    { 
        cout << "You entered an empty string, " 
             << "assigning default\n"; 
        name = "John"; 
    }

 string result; 
    string s1 = "hello "; 
    string s2 = "world"; 
    result = s1 + s2; 
        // result now contains "hello world"
string result; 
    string s1 = "hello"; 
       // without the extra space at the end 
    string s2 = "world"; 
    result = s1; 
    result += ' '; 
       // append a space at the end 
    result += s2;

9. Program to find quotient and remainder of 2 numbers

#include<stdio.h>
#include<conio.h>

int remainder, tempdevisor ,i = 0;

int division(int,int);
int main()
{
int dividend, divisor;
int quotient;

printf("Enter the value of Dividend and Divisor\n");


scanf("%d %d",&dividend,&divisor);

tempdevisor = divisor;
quotient = division(dividend,divisor);

printf("The Quotient is %d\n",quotient);


printf("The Remainder is %d\n",remainder);
}

int division(int dividend,int divisor)


{

int quotient = 1;
i++;

if(dividend == divisor)
{
remainder = 0;
return 1;
}
else if (dividend < divisor)
{
remainder = dividend;
return 0;
}

while(divisor <= dividend)


{
divisor = divisor<<1;
quotient = quotient<<1;
}
divisor = divisor>>1;
quotient = quotient>>1;

// printf("Quotient in the %d iteration is


%d\n",i,quotient);
quotient = quotient + division(dividend-
divisor,tempdevisor);

return quotient;
}

11. Program to generate Prime Numbers between 1 and 50

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,i;
cout<<"\n\n\tEnter a number : " ;
cin>>a;

i=2;
while(l<=(a/2))
{
if(a%i==0)
break;
i++;
}
if(i==(a/2)
cout<<"\n\tThe no. is prime ";
else
cout<<"\n\t The no. is not prime ";
getch();
}

12.Program to perform matrix addition and multiplication.

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
 
int **mat1;
int **mat2;
int **result;
int row,col;
 
cout<<"Please enter row/col"<<endl;
cin>>row>>col;
 
mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;
 
for (k=0; i<row; k++)
mat1[k] = new int[col];
mat2[k] = new int[col];
result[k] = new int[col];
 
for (i=0; i<row; i++)
for (j=0; j<col; j++)
for (j=0; j<row; j++)
for(i=0; i<col; i++)
result[i][k] += (mat1 [i][k] * mat2[k][j]);
 
cout<<setw(4)<<result[i][k];

14. c++ program to overload unary minus operator

//****This program illustrates the overloading of unary munus ( - )


operator******
#include
class Minus
{
private:
int a, b, c ;
public:
Minus( ) // Default Constructor
{
}
Minus(int A, int B, int C)
{
a=A;
b=B;
c=C;
}
void display(void);
//********Declaration of the operator function**********
void operator - ( );
}; // End of the Class Definition

//***********Member Function Definitions*************


inline void Minus :: display(void)
{
cout << "\t a = " << a << endl ;
cout << "\t b = " << b << endl ;
cout << "\t c = " << c << endl ;
}
//*********Definition of the operator function***********
inline void Minus :: operator - ( )
{
a = -a ;
b = -b ;
c = -c ;
}
//*************Main Function Definition***************
void main(void)
{
Minus M(5, 10, -15) ;
cout << "\n Before activating the operator - ( )\n" ;
M.display( ) ;
-M ;
cout << "\n After activating the operator - ( )\n" ;
M.display( ) ;
}

Output: 

You might also like