/****************************************************************
21. To find sum of ‘n’ +ve no’s if a –ve no is encountered break out
******************************************************************/
#include
#include
void main()
{
int arr[20],n,sum=0;
clrscr();
cout<<"Enter the size of array"<
cin>>n;
cout<<"Enter the array of numbers"<
for (int i=0;i
{
cin>>arr[i];
}
for (i=0;i
{
if (arr[i]<0)
break;
sum=sum+arr[i];
}
cout<<"The required sum is\t"<
getch();
}
OUTPUT:
Enter the size of an array 6
Enter the array of numbers
16 11 7 -9 8 4
The required sum is 34
/***************************************************************
22. To find sum of ‘n’ +ve nos if a -ve is encountered continue
*****************************************************************/
#include
#include
void main()
{
int arr[20],n,sum=0;
clrscr();
cout<<"Enter the size of array"<
cin>>n;
cout<<"Enter the array of numbers"<
for (int i=0;i
{
cin>>arr[i];
}
for (i=0;i
{
if (arr[i]<0)
continue;
sum=sum+arr[i];
}
cout<<"The required sum is\t"<
getch();
}
OUTPUT:
Enter the size of an array 6
Enter the array of numbers
6 9 4 -9 8 7
The required sum is 34
/**********************************************************
23. Program to swap two numbers using call by value
************************************************************/
#include
#include
void swap (int,int);
void main()
{
int a,b;
cout<<"Enter the two numbers"<
cin>>a>>b;
cout<<"\t b="<
swap(a,b);
getch();
}
void swap (int a, int b)
{
int t=a;
a=b;
b=t;
cout<<"\t b="<
}
OUTPUT:
Enter the two numbers 5 9
Before swapping a=5 b=9
After swapping a=9 b=5
/****************************************************************
24. Program to swap two numbers using call by address
*****************************************************************/
#include
#include
void swap(int*& , int*& );
void main()
{
int a, b;
int * pa = &a;
int * pb = &b;
clrscr();
cout<<"Enter the two numbers"<
cin>>a>>b;
cout<<"Before swapping a="<<*pa;
cout<<"\t b="<<*pb<
swap(pa, pb);
cout<<"After swapping a="<<*pa;
cout<<"\t b="<<*pb<
getch();
}
void swap(int*& x, int*& y)
{
int *c;
c=x;
x=y;
y=c;
}
OUTPUT:
Enter the two numbers 5 9
Before swapping a=5 b=9
After swapping a=9 b=5
/***************************************************************
25. Program to swap two numbers using call reference
****************************************************************/
#include
#include
void swap (int&,int&);
void main()
{
int a,b;
cout<<"Enter the two numbers"<
cin>>a>>b;
cout<<"\t b="<
swap(a,b);
cout<<"\t b="<
getch();
}
void swap (int &x, int &y)
{
int t=x;
x=y;
y=t;
}
OUTPUT:
Enter the two numbers 5 9
Before swapping a=5 b=9
After swapping a=9 b=5
/******************************************************************
26. Function overloading to add 2 ints,3 ints,2floats
*******************************************************************/
#include
#include
int add(int x,int y);
int add(int x,int y,int z);
float add(float d,float e);
void main()
{
int a,b,c;
float f,g;
clrscr();
cout<<"\n Enter any three integer values :"<
cin>>a>>b>>c;
cout<<"\n Enter any two floating point values :"<
cin>>f>>g;
cout<<"\n Overloading Two Integer Values :"<
cout<<"\n Overloading Three Integer Values :"<
cout<<"\n Overloading Two Floating Point Values :"<
getch();
}
int add(int x,int y)
{
return (x+y);
}
int add(int x,int y,int z)
{
return (x+y+z);
}
float add(float d,float e)
{
return (d+e);
}
OUTPUT :-
Enter any two integer values :
16 18
Enter any three integer values:
8 9 5
Enter any two floating point values:
2.36 5.693
Overloading Two Integer Values: 34
Overloading Three Integer Values: 22
Overloading Two Floating Point Values: 8.053
/******************************************************************
27. Function overloading to swap 2 ints,2 chars, 2 floats
*******************************************************************/
#include
#include
void swap(int a,int b);
void swap(float x,float y);
void swap(char d,char e);
void main()
{
int a,b;
float x,y;
char d,e;
clrscr();
cout<<"\n Enter Any Two Integer Values :"<
cin>>a>>b;
cout<<"\n Enter Any Two Floating Point Values :"<
cin>>x>>y;
cout<<"\n Enter Any Two Character Values :"<
cin>>d>>e;
cout<<"\n\n Swaped Integer Values Are :"<
swap(a,b);
cout<<"\n\n Swaped Floating Point Values Are :"<
swap(x,y);
cout<<"\n\n Swaped Character Values Are :"<
swap(d,e);
getch();
}
void swap(int a,int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swap(float x,float y)
{
float temp1;
temp1 = x;
x = y;
y = temp1;
cout<<" "< "<
}
void swap(char d,char e)
{
char temp2;
temp2 = d;
d = e;
e = temp2;
cout<<" "< "<
}
OUTPUT:-
Enter Any Two Integer Values :
5 9
Enter Any Two Floating Point Values :
2.36 9.85
Enter Any Two Character Values :
k d
Swaped Integer Values Are :
9 5
Swaped Floating Point Values Are :
9.85 2.36
Swaped Character Values Are :
d k
/******************************************************************
28. To find sum of ‘n’ elements in a single dimensional array
*******************************************************************/
#include
#include
void main()
{
int a[100],n,i;
clrscr();
cout<<"\n Enter The Size Of Array :"<
cin>>n;
cout<<"\n Enter The Array Elements :"<
for(i=0;i
cin>>a[i];
cout<<"\n Array Elements Are :"<
int s = 0;
for(i=0;i
{
s = s+a[i];
}
cout<<"\n Sum Of Array Elements Are : "<
getch();
}
OUTPUT:-
Enter The Size Of Array :
5
Enter The Array Elements :
9 8 7 6 5
Array Elements Are :
9
8
7
6
5
Sum Of Array Elements = 35
/***************************************************************
29 To find greatest element in a single dimensional array
****************************************************************/
#include
#include
void main()
{
int a[20],n,x;
clrscr();
cout<<"Enter the limit"<
cin>>n;
cout<<"Enter the Numbers"<
for(int i=0;i
cin>>a[i];
x = a[0];
cout<<"Greatest of these "<
for(i=0;i
if(a[i] > x)
x = a[i];
cout<
getch();
}
OUTPUT :
Enter the Limit
5
Enter the Numbers
2
4
5
3
1
Greatest of these 5 Numbers is : 5
/****************************************************************
30. To count no of odd, even, +ve and -ve 1-D array
*****************************************************************/
#include
#include
void main()
{
int a[20],n,e=0,o=0,p=0,m=0,z=0;
clrscr();
cout<<"Enter the limit"<
cin>>n;
cout<<"Enter the Numbers"<
for(int i=0;i
cin>>a[i];
for(i=0;i
if(a[i] % 2 == 0)
e++;
else
o++;
for(i = 0;i
if(a[i] > 0)
p++;
else if(a[i] == 0)
z++;
else
m++;
cout<<"Number of Positive Numbers : "<
cout<<"Number of Negative Numbers : "<
cout<<"Number of Zeros : "<
cout<<"Number of ODD Numbers : "<
cout<<"Number of EVEN Numbers : "<
getch();
}
OUTPUT
Enter the Limit
5
Enter the Numbers
2
-1
4
0
5
Number of Positive Numbers : 3
Number of Negative Numbers : 1
Number of Zeros : 1
Number of ODD Numbers : 2
Number of EVEN Numbers : 3
0 comments:
Post a Comment