Sunteți pe pagina 1din 34

Pranay Dam ( MCA A)

Assignment 1

1.1 #include <iostream>

using namespace std;

int main()

int i,fact=1,number;

cout<<"Enter any Number: ";

cin>>number;

for(i=1;i<=number;i++){

fact=fact*i;

cout<<"Factorial of " <<number<<" is: "<<fact<<endl;

return 0;

Output- Enter any Number: 5

Factorial of 5 is: 120

1.2 Fibonacci series

#include <iostream>

using namespace std;

int main() {

int n1=0,n2=1,n3,i,number;

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

cin>>number;

cout<<n1<<" "<<n2<<" ";

for(i=2;i<number;++i)

n3=n1+n2;

cout<<n3<<" ";
n1=n2;

n2=n3;

return 0;

1.3 find prime or not

#include <iostream>

using namespace std;

int main()

int n, i, m=0, flag=0;

cout << "Enter the Number to check Prime: ";

cin >> n;

m=n/2;

for(i = 2; i <= m; i++)

if(n % i == 0)

cout<<"Number is not Prime."<<endl;

flag=1;

break;

if (flag==0)

cout << "Number is Prime."<<endl;

return 0;

Enter the Number to check Prime: 17

Number is Prime
Enter the Number to check Prime: 57

Number is not Prime

Assignment 2

2.1 find perfect no.

#include <iostream>

using namespace std;

int main()

int n,i=1,sum=0;

cout<<"Enter a number: ";

cin>>n;

while(i<n)

if(n%i==0)

sum=sum+i;

i++;

if(sum==n)

cout << i << " is a perfect number\n";

else

cout << i << " is not a perfect number\n";

return 0;

Output – Enter a number 6

6 is a perfect number

2.2 find largest and smallest elements in array

#include<iostream>
using namespace std;

int main()

int Arr[100],n,i,small,large;

cout<<"Enter number of elements";

cin>>n;

for(i=0;i<n;i++)

cout<<"Enter element "<<i+1<<":";

cin>>Arr[i];

small=Arr[0];

large=Arr[0];

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

if(Arr[i]<small)

small=Arr[i];

if(Arr[i]>large)

large=Arr[i];

cout<<"\nLargest element is :"<<large;

cout<<"\nSmallest element is :"<<small;


return 0;

Output- Enter number of elements 5

Enter element 13

Enter element 69

Enter element 30

Enter element 51

Enter element 11

Largest element is : 69

Smallest element is : 11

2.3 search an element in an array

#include<iostream>

using namespace std;

int main()

int arr[10], i, num, n, cnt=0, pos;

cout<<"\n Enter Array Size : ";

cin>>n;

cout<<"\n Enter Array Elements : \n";

for(i=0; i<n; i++)

cout<<" ";

cin>>arr[i];

cout<<"\n Enter Element to be Searched : ";

cin>>num;

for(i=0; i<n; i++)

if(arr[i]==num)
{

cnt=1;

pos=i+1;

break;

if(cnt==0)

cout<<"\n Element Not Found..!!";

else

cout<<"\n Element "<<num<<" Found At Position "<<pos;

return 0;

Output- Enter array size 5

Enter array elements 5

Enter element to be search 8

Element 8 found at position 3

Assignment 3

3.1 find location of a given Using linear search

#include<iostream.h>

#include<conio.h>
void main()

clrscr();

int arr[10], i, num, n, c=0, pos;

cout<<"Enter the array size : ";

cin>>n;

cout<<"Enter Array Elements : ";

for(i=0; i<n; i++)

cin>>arr[i];

cout<<"Enter the number to be search : ";

cin>>num;

for(i=0; i<n; i++)

if(arr[i]==num)

c=1;

pos=i+1;

break;

if(c==0)

cout<<"Number not found";

else

cout<<num<<" found at position "<<pos;


}

getch();

Output- Enter the array size 5

Enter array elements 23

34

56

57

89

Enter the no. to be search 56

56 found at position 3

3.2 binary search

#include<iostream>

using namespace std;

int main()

int search(int [],int,int);

int n,i,a[100],e,res;

cout<<"How Many Elements:";

cin>>n;

cout<<"\nEnter Elements of Array in Ascending order\n";

for(i=0;i<n;++i)

cin>>a[i];

cout<<"\nEnter element to search:";

cin>>e;
res=search(a,n,e);

if(res!=-1)

cout<<"\nElement found at position "<<res+1;

else

cout<<"\nElement is not found.";

return 0;

int search(int a[],int n,int e)

int f,l,m;

f=0;

l=n-1;

while(f<=l)

m=(f+l)/2;

if(e==a[m])

return(m);

else

if(e>a[m])

f=m+1;

else

l=m-1;

return -1;

}
Output- How Many Elements:5

Enter Elements of Array in Ascending order

12 39 40 68 77

Enter element to search:40

Element found at position 3

3.3 bubble sort

#include<iostream>

using namespace std;

int main()

int a[50],n,i,j,temp;

cout<<"Enter the size of array: ";

cin>>n;

cout<<"Enter the array elements: ";

for(i=0;i<n;++i)

cin>>a[i];

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

for(j=0;j<(n-i);++j)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

cout<<"Array after bubble sort:";


for(i=0;i<n;++i)

cout<<" "<<a[i];

return 0;

Output- enter size of array 5

Enter the array elements 15 7 12 3 9

After bubble sort 3 7 9 12 15

Assignment 4

4.1 pattern

#include <iostream>

using namespace std;

int main()

int rows;

cout << "Enter number of rows: ";

cin >> rows;

for(int i = rows; i >= 1; --i)

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

cout << "$ ";

cout << endl;

return 0;

Output- enter no of rows 5

$$$$$

$$$$
$$$

$$

4.2 pattern

#include <iostream>

using namespace std;

void numpat(int n)

int num = 1;

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

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

cout << num << " ";

num = num + 1;

cout << endl;

int main()

int n = 5;

numpat(n);

return 0;

Output-

22

333
4444

55555

4.3 pattern

#include <iostream>

using namespace std;

int main()

int i,j;

for (int i=5; i>5; i--)

for (int j=5; j>=i; j--)

cout << j<< " ";

cout << endl;

Output-

54

543

5432

54321

Assignment 5

5.1 selection sort

#include<iostream>

using namespace std;

int main()

int i,j,n,loc,temp,min,a[30];
cout<<"Enter the number of elements:";

cin>>n;

cout<<"\nEnter the elements\n";

for(i=0;i<n;i++)

cin>>a[i];

for(i=0;i<n-1;i++)

min=a[i];

loc=i;

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

if(min>a[j])

min=a[j];

loc=j;

temp=a[i];

a[i]=a[loc];

a[loc]=temp;

cout<<"\nSorted list is as follows\n";

for(i=0;i<n;i++)

cout<<a[i]<<" ";
}

return 0;

Output- enter the no. Of elements 6

Enter elements

18 3 10 7 8 4

Sorted list

3 4 7 8 10 18

5.2 insertion sort

#include<iostream>

using namespace std;

int main()

int i,j,n,temp,a[30];

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

cin>>n;

cout<<"\nEnter the elements\n";

for(i=0;i<n;i++)

cin>>a[i];

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

temp=a[i];

j=i-1;

while((temp<a[j])&&(j>=0))

a[j+1]=a[j];
j=j-1;

a[j+1]=temp;

cout<<"\nSorted list ";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

return 0;

Output- enter the no. Of elements 7

Enter the elements

12 4 7 2 15 42 3

Sorted list

2 3 4 7 12 15 42

5.3 factorial of given number using recursion

#include<iostream>

using namespace std;

int factorial(int n);

int main()

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Factorial of "<< factorial(n);

return 0;

int factorial(int n)
{

if(n > 1)

return n * factorial(n - 1);

else

return 1;

Output- enter a positive integer 6

Factorial 720

Assignment 6

6.1 find length of a given string without using string library function

#include<iostream>

#include<stdio.h>

using namespace std;

int main()

char a[30];

int i;

cout<<"Enter a string:";

gets(a);

for(i=0;a[i]!='\0';++i);

cout<<"\n string length'"<<a<<"' is "<<i;

return 0;

Output- Enter a string hello

String length 5
6.2 copy one string into another string without using string library function

#include <stdio.h>

int main()

char s1[100], s2[100], i;

printf("Enter string s1: ");

scanf("%s",s1);

for(i = 0; s1[i] != '\0'; ++i)

s2[i] = s1[i];

s2[i] = '\0';

printf("String s2: %s", s2);

return 0;

Output- enter string s1: hello

String s2: hello

6.3 convert lowercase letter into uppercase letter

#include <stdio.h>

#include <string.h>

int main()

char s[1000];

int i;

printf("Enter the string in lower case: ");

gets(s);

printf("string in lowercase ='%s'\n",s);

for(i=0;s[i];i++)
{

if(s[i]>=97 && s[i]<=122)

s[i]-=32;

printf("string in uppercase ='%s'\n",s);

return 0;

Output- enter string in lowercase : hello

String in uppercase: HELLO

ASSIGNMENT 7

7.1 DEMONSTRATE FUNCTION OVERLOADING

#include <iostream>

using namespace std;

void display(int);

void display(float);

void display(int, float);

int main() {

int a = 5;

float b = 5.5;

display(a);

display(b);

display(a, b);

return 0;

void display(int var) {

cout << "Integer number: " << var << endl;

}
void display(float var) {

cout << "Float number: " << var << endl;

void display(int var1, float var2) {

cout << "Integer number: " << var1;

cout << " and float number:" << var2;

Output- Integer number: 5

Float number: 5.5

Integer number: 5 and float number:5.5

7.2 demonstrate passing array as an argument

#include <iostream>

using namespace std;

void p(int arr[5]);

int main()

int arr1[5] = { 10, 20, 30, 40, 50 };

int arr2[5] = { 5, 15, 25, 35, 45 };

p(arr1);

p(arr2);

void p(int arr[5])

cout << "Printing array elements:"<< endl;

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

cout<<arr[i]<<"\n";

}
Output- Printing array elements:

10

20

30

40

50

Printing array elements:

15

25

35

45

7.3 demonstrate constant as an argument

include <iostream>

using namespace std;

void function (const int v = 0){

v = v*2;

cout << v << endl;

void function (const int v = 0){

cout << v * 2 << endl;

int main(){

int x = 10;

cout << x << endl;

function(x);

return 0;

}
Assignment 8

8.1 demonstrate constructor and destructor

#include <iostream>

using namespace std;

class Example{

public:

Exp()

Cout<<"Constructor called."<<endl;}

void display(){

cout<<"display function called."<<endl;

~Exp()

{cout<<"Destructor called."<<endl;}

};

int main(){

Example objF

objF.display();

return 0;

Output- Constructor called.

display function called.

Destructorcalled.

8.2 demonstrate parameterized constructor

#include<iostream>

#include<conio.h>
using namespace std;

class Exp{

int a, b;

public:

Exp(int x, int y) {

a = x;

b = y;

cout << "Im Constructor\n";

void Display() {

cout << "Values :" << a << "\t" << b;

};

int main() {

Exp Object(10, 20);

Object.Display();

getch();

return 0;

Output- values 10 20

8.3 demonstrate constructor overloading using this pointer

#include <iostream>

using namespace std;

class construct

{
public:

float area;

construct()

area = 0;

construct(int a, int b)

area = a * b;

void disp()

cout<< area<< endl;

};

int main()

construct o;

construct o2( 10, 20);

o.disp();

o2.disp();

return 1;

Output- 0
200

Assignment 9

9.1 demonstrate copy constructor

include <iostream>

using namespace std;

class A

public:

int x;

A(int a)

x=a;

A(A &i)

x = i.x;

};

int main()

A a1(20);

A a2(a1);

cout<<a2.x;

return 0;

Output- 20

9.2 reverse a string without using strrev

#include<iostream>
#include<string.h>

using namespace std;

int main ()

char str[50], temp;

int i, j;

cout << "Enter a string : ";

gets(str);

j = strlen(str) - 1;

for (i = 0; i < j; i++,j--)

temp = str[i];

str[i] = str[j];

str[j] = temp;

cout << "\nReverse string : " << str;

return 0;

Enter a string: hello

Reverse string: olleh

9.3 demonstrate object as function argument

#include <iostream>

using namespace std;

class Demo

private:

int a;
public:

void set(int x)

a = x;

void sum(Demo ob1, Demo ob2)

a = ob1.a + ob2.a;

void print()

cout<<"Value of A : "<<a<<endl;

};

int main()

Demo d1;

Demo d2;

Demo d3;

d1.set(10);

d2.set(20);

d3.sum(d1,d2);

d1.print();

d2.print();

d3.print();
return 0;

Output- value of A 10

Value of A 20

Value of A 30

Assignment 10

10.1 Write a C++ program to design a class string and overloaded operator + for concatenation of two
strings

#include<conio.h>

#include<string.h>

#include<iostream>

class string {

public:

char *s;

int size;

void getstring(char *str)

size = strlen(str);

s = new char[size];

strcpy(s,str);

void operator+(string);

};

void string::operator+(string ob)

{
size = size+ob.size;

s = new char[size];

strcat(s,ob.s);

cout<<"\concatenated String is: "<<s;

void main()

string ob1, ob2;

char *string1, *string2;

clrscr();

cout<<"\nEnter First String:";

cin>>string1;

ob1.getstring(string1);

cout<<"\nEnter Second String:";

cin>>string2;

ob2.getstring(string2);

ob1+ob2;

getch();

Output- enter first string: hello

Enter second string: world

Concatenated string: helloworld

10.2 Write a program to add 2 complex numbers using friend function

#include<iostream>

using namespace std;

class complex
{

int real,imag;

public:

void set()

cout<<"enter real and imag part";

cin>>real>>imag;

friend complex sum(complex,complex);

void display();

};

void complex::display()

cout<<"the sum of complex num is"<<real<<"+i"<<imag;

complex sum(complex a,complex b)

complex t;

t.real=a.real+b.real;
t.imag=a.imag+b.imag;

return t;

int main()

complex a,b,c;

a.set();

b.set();

c=sum(a,b);

c.display();

return(0);

Output- enter real and imag part 5 i7

Enter real and imag part 3 i5

The sum of complex number is 8+i12

10.3 write a C++ program to overload <<, >>, >= , !=

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

class Time

int hr, min, sec;

public:

Time()

hr=0, min=0; sec=0;

Time(int h, int m, int s)

hr=h, min=m; sec=s;

friend ostream& operator << (ostream &out, Time &tm);

};

ostream& operator << (ostream &out, Time &tm)

out << "Time is: " << tm.hr << " hour : " << tm.min << " min : " << tm.sec << " sec";

return out;

Output- 1:20:30

2:15:25

3:35:55

//OVERLOADING ==

void main()
{

Time tm(3,15,45);

cout << tm;

class Time

int hr, min, sec;

public:

Time()

hr=0, min=0; sec=0;

Time(int h, int m, int s)

hr=h, min=m; sec=s;

friend bool operator==(Time &t1, Time &t2);

bool operator== (Time &t1, Time &t2)

return ( t1.hr == t2.hr && t1.min == t2.min && t1.sec == t2.sec );

void main()

{
Time t1(3,15,45);

Time t2(4,15,45);

if(t1 == t2)

cout << "Both the time values are equal";

else

cout << "Both the time values are not equal";

Output- Both the time values are not equal

S-ar putea să vă placă și