Cpppranaydam
Cpppranaydam
Cpppranaydam
Assignment 1
int main()
int i,fact=1,number;
cin>>number;
for(i=1;i<=number;i++){
fact=fact*i;
return 0;
#include <iostream>
int main() {
int n1=0,n2=1,n3,i,number;
cin>>number;
for(i=2;i<number;++i)
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
return 0;
#include <iostream>
int main()
cin >> n;
m=n/2;
if(n % i == 0)
flag=1;
break;
if (flag==0)
return 0;
Number is Prime
Enter the Number to check Prime: 57
Assignment 2
#include <iostream>
int main()
int n,i=1,sum=0;
cin>>n;
while(i<n)
if(n%i==0)
sum=sum+i;
i++;
if(sum==n)
else
return 0;
6 is a perfect number
#include<iostream>
using namespace std;
int main()
int Arr[100],n,i,small,large;
cin>>n;
for(i=0;i<n;i++)
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];
Enter element 13
Enter element 69
Enter element 30
Enter element 51
Enter element 11
Largest element is : 69
Smallest element is : 11
#include<iostream>
int main()
cin>>n;
cout<<" ";
cin>>arr[i];
cin>>num;
if(arr[i]==num)
{
cnt=1;
pos=i+1;
break;
if(cnt==0)
else
return 0;
Assignment 3
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
cin>>n;
cin>>arr[i];
cin>>num;
if(arr[i]==num)
c=1;
pos=i+1;
break;
if(c==0)
else
getch();
34
56
57
89
56 found at position 3
#include<iostream>
int main()
int n,i,a[100],e,res;
cin>>n;
for(i=0;i<n;++i)
cin>>a[i];
cin>>e;
res=search(a,n,e);
if(res!=-1)
else
return 0;
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
12 39 40 68 77
#include<iostream>
int main()
int a[50],n,i,j,temp;
cin>>n;
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<<" "<<a[i];
return 0;
Assignment 4
4.1 pattern
#include <iostream>
int main()
int rows;
return 0;
$$$$$
$$$$
$$$
$$
4.2 pattern
#include <iostream>
void numpat(int n)
int num = 1;
num = num + 1;
int main()
int n = 5;
numpat(n);
return 0;
Output-
22
333
4444
55555
4.3 pattern
#include <iostream>
int main()
int i,j;
Output-
54
543
5432
54321
Assignment 5
#include<iostream>
int main()
int i,j,n,loc,temp,min,a[30];
cout<<"Enter the number of elements:";
cin>>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;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
return 0;
Enter elements
18 3 10 7 8 4
Sorted list
3 4 7 8 10 18
#include<iostream>
int main()
int i,j,n,temp,a[30];
cin>>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;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
12 4 7 2 15 42 3
Sorted list
2 3 4 7 12 15 42
#include<iostream>
int main()
int n;
cin >> n;
return 0;
int factorial(int n)
{
if(n > 1)
else
return 1;
Factorial 720
Assignment 6
6.1 find length of a given string without using string library function
#include<iostream>
#include<stdio.h>
int main()
char a[30];
int i;
cout<<"Enter a string:";
gets(a);
for(i=0;a[i]!='\0';++i);
return 0;
String length 5
6.2 copy one string into another string without using string library function
#include <stdio.h>
int main()
scanf("%s",s1);
s2[i] = s1[i];
s2[i] = '\0';
return 0;
#include <stdio.h>
#include <string.h>
int main()
char s[1000];
int i;
gets(s);
for(i=0;s[i];i++)
{
s[i]-=32;
return 0;
ASSIGNMENT 7
#include <iostream>
void display(int);
void display(float);
int main() {
int a = 5;
float b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}
void display(float var) {
#include <iostream>
int main()
p(arr1);
p(arr2);
cout<<arr[i]<<"\n";
}
Output- Printing array elements:
10
20
30
40
50
15
25
35
45
include <iostream>
v = v*2;
int main(){
int x = 10;
function(x);
return 0;
}
Assignment 8
#include <iostream>
class Example{
public:
Exp()
Cout<<"Constructor called."<<endl;}
void display(){
~Exp()
{cout<<"Destructor called."<<endl;}
};
int main(){
Example objF
objF.display();
return 0;
Destructorcalled.
#include<iostream>
#include<conio.h>
using namespace std;
class Exp{
int a, b;
public:
Exp(int x, int y) {
a = x;
b = y;
void Display() {
};
int main() {
Object.Display();
getch();
return 0;
Output- values 10 20
#include <iostream>
class construct
{
public:
float area;
construct()
area = 0;
construct(int a, int b)
area = a * b;
void disp()
};
int main()
construct o;
o.disp();
o2.disp();
return 1;
Output- 0
200
Assignment 9
include <iostream>
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
#include<iostream>
#include<string.h>
int main ()
int i, j;
gets(str);
j = strlen(str) - 1;
temp = str[i];
str[i] = str[j];
str[j] = temp;
return 0;
#include <iostream>
class Demo
private:
int a;
public:
void set(int x)
a = x;
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;
size = strlen(str);
s = new char[size];
strcpy(s,str);
void operator+(string);
};
{
size = size+ob.size;
s = new char[size];
strcat(s,ob.s);
void main()
clrscr();
cin>>string1;
ob1.getstring(string1);
cin>>string2;
ob2.getstring(string2);
ob1+ob2;
getch();
#include<iostream>
class complex
{
int real,imag;
public:
void set()
cin>>real>>imag;
void display();
};
void complex::display()
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);
#include<iostream.h>
#include<conio.h>
class Time
public:
Time()
};
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);
class Time
public:
Time()
void main()
{
Time t1(3,15,45);
Time t2(4,15,45);
if(t1 == t2)
else