C++ PROGRAMS (STAGE-V)


C++ PROGRAMS


// TO INVOKE A FUNCTION TO FIND THE LEAST COMMON DIVISOR OF TWO INTEGERS

#include<iostream.h>
#include<conio.h>
int lcd(int a,int b)
{
int i,j=2,flag=1;
if(a>b)
i=a;
else
i=b;
while((j<=i)&&(flag))
{
if((a%j==0)&&(b%j==0))
flag=0;
else
j++;
}
if(flag)
j=1;
return j;
}
void main()
{
clrscr();
int x, y, z;
cout<<"\nENTER 2 NUMBERS WHOSE LCD IS TO BE FOUND : ";
cin>>x>>y;
z=lcd(x,y);
cout<<"\nTHE LCD OF GIVEN 2 NUMBERS IS : "<<z<<endl;
getch();
}



/*OUTPUT
ENTER 2 NUMBERS WHOSE LCD IS TO BE FOUND : 185
148
THE LCD OF GIVEN 2 NUMBERS IS : 37
*/



_____________http://sundcs.blogspot.com/____________




//TO FIND THE LCM AND HCF OF GIVEN 3 NUMBERS

#include<iostream.h>
#include<conio.h>
void lcm(int, int, int);
void hcf(int, int, int);
void main()
{
char choice;
do
{ int a,b,c;
clrscr();
cin>>a>>b>>c;
lcm(a,b,c);
hcf(a,b,c);
cout<<"\n\nDO YOU WANT TO REPEAT THE PROGRAM?(Y/N): ";
cin>>choice;
}while(choice=='Y'||choice=='y');
}
void lcm(int x,int y, int z)
{
long max,lcom, count, flag=0;
if(x>=y&&x>=z)
max=x;
else if(y>=x&&y>=z)
max=y;
else if(z>=x&&z>=y)
max=z;
for(count=1;flag==0;count++)
{
lcom=max*count;
if(lcom%x==0 && lcom%y==0 && lcom%z==0)
{
flag=1;
cout<<"\nTHE LCM OF "<<x<<","<<y<<","<<z<<" IS "<<lcom;
}
}
}
void hcf(int p, int q, int r)
{
int gcf=1,flag=0, count;
for(count=1; flag==0;count++)
{
if(p%count==0&&q%count==0&&r%count==0)
gcf=count;
if(count>p&&count>q&&count>r)
{
flag=1;
cout<<"\nTHE GCF OF "<<p<<","<<q<<","<<r<<" IS "<<gcf;
}
}
}



_____________http://sundcs.blogspot.com/____________




//TO SUM N NATURAL NUMBERS STARTING FROM A GIVEN NUMBER USING FUNCTION

#include<iostream.h>
#include<conio.h>
int summat(int first,int count);
void main()
{
clrscr();
unsigned long a, b, sum;
cout<<"\nENTER THE FIRST TERM : ";
cin>>a;
cout<<"\nHOW MANY NUMBERS ARE TO BE ADDED : ";
cin>>b;
sum=summat(a,b);
cout<<"THE SUM IS : "<<sum<<"\n";
getch();
}
int summat(int first,int count)
{
unsigned long i, s=0,j=first;
for(i=0;i<count;i++)
s+=j++;
return s;
}



_____________http://sundcs.blogspot.com/____________




//TO ILLUSTRATE THE CALL BY VALUE METHOD OF FUNCTION INVOKING

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int change(int);
int original=10;
cout<<"THE ORIGINAL VALUE IS : "<<original;
cout<<"\nRETURN VALUE OF FUNCTION CHANGE() IS : "
<<change(original);
cout<<"\nTHE VALUE AFTER FUNCTION CHANGE() IS OVER : "
<< original;
getch();
}
int change(int a)
{
a=20;
return a;
}



/*OUTPUT
THE ORIGINAL VALUE IS : 10
RETURN VALUE OF FUNCTION CHANGE() IS : 20
THE VALUE AFTER FUNCTION CHANGE() IS OVER : 10
*/


_____________http://sundcs.blogspot.com/____________




//TO SHOW THE HANDICAP OF CALL BY VALUE METHOD

#include<iostream.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a=7, b=4;
clrscr();
cout<<"THE ORIGINAL VALUES ARE : ";
cout<<"a= " <<a<<" b= "<<b<<endl;
swap(a,b);
cout<<"THE VALUES AFTER SWAP() ARE : ";
cout<<"a= " <<a <<" b= "<<b<<endl;
getch();
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"SWAPPED VALUES ARE : ";
cout<<"a= " << x <<" b= "<<y<<endl;
}



/*OUTPUT
THE ORIGINAL VALUES ARE : a= 7 b= 4
SWAPPED VALUES ARE : a= 4 b= 7
THE VALUES AFTER SWAP() ARE : a= 7 b= 4
*/



_____________http://sundcs.blogspot.com/____________




//A FUNCTION TO SHOW SPECIAL SERIES

#include<iostream.h>
#include<conio.h>
#include<math.h>
void factorial(int);
void series(int);
void main()
{
clrscr();
int num;
cout<<"ENTER NUMBER : ";
cin>>num;
series(num);
cout<<"\n\n";
factorial(num);
getch();
}
void factorial(int n)
{
long float i,result=0, fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
cout<<"+"<<i/fact<<"\t";
result=result+(i/fact);
}
cout<<"\t="<<result;
}
void series(int n)
{
long count, res=0;
for(count=0;count<=n;count++)
{
cout<<"+"<<pow(n,count)<<"\t";
res=res+pow(n,count);
}
cout<<"\t="<<res;
}



/*OUTPUT
ENTER NUMBER : 5
+1 +5 +25 +125 +625 +3125 =3906
+1 +1 +0.5 +0.166667 +0.041667 =2.708333
*/



_____________http://sundcs.blogspot.com/____________




//TO SWAP TWO VALUES USING CALL BY REFERENCE

#include<iostream.h>
#include<conio.h>
void main()
{
void swap(int &, int &);
int a=7, b=9;
clrscr();
cout<<"\nTHE ORIGINAL VALUES ARE : ";
cout<<"a= " <<a<<" b= " <<b <<"\n";
swap(a,b);
cout<<"THE VALUES AFTER SWAP() ARE : ";
cout<<"a= " <<a << " b= " <<b <<"\n";
getch();
}
void swap(int &x, int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"THE SWAPPED VALUES ARE : ";
cout<<"a= " <<x<< " b= "<<y<<"\n";
}



/*OUTPUT
THE ORIGINAL VALUES ARE : a= 7 b= 9
THE SWAPPED VALUES ARE : a= 9 b= 7
THE VALUES AFTER SWAP() ARE : a= 9 b= 7
*/



_____________http://sundcs.blogspot.com/____________




// TO CONVERT DISTANCE IN FEET OR INCHES USING A CALL BY REFERNCE METHOD

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
void convert(float &, char &, char);
float distance;
char choice, type='F';
clrscr();
cout<<"\nENTER DISTANCE IN FEET : ";
cin>>distance;
cout<<"\nYOU WANT THE DISTANCE IN FEETS OR INCHES ? (F/I) : ";
cin>>choice;
switch(choice)
{
case 'f':
case 'F': convert(distance,type,'F');
break;
case 'i':
case 'I': convert(distance, type,'I');
break;
default: cout<<"\nYOU ENTERED A WRONG CHOICE!!!";
exit(0);
}
cout<<"\nDISTANCE = " <<distance<<" " << type << "\n";
getch();
}
void convert(float &d, char &t, char ch)
{
switch(ch)
{
case 'F': if(t=='I')
{
d=d/12;
t='F';
}
break;
case 'I': if(t=='F')
{
d=d*12;
t='I';
}
break;
}
}



/*OUTPUT
ENTER DISTANCE IN FEET : 25
YOU WANT THE DISTANCE IN FEETS OR INCHES ? (F/I) : i
DISTANCE = 300 I
*/



_____________http://sundcs.blogspot.com/____________




//TO SET LARGER OF THE GIVEN INTEGER TO -1 USING CALL BY REFERENCE

#include<iostream.h>
#include<conio.h>
void setlarge(int &a,int &b)
{
if(a>b)
{
cout<<"\n"<<a<<" IS LARGER AND IS SET TO -1.";
a=-1;
cout<<"\nTHE NEW VALUES ARE : "<<a<<"\t"<<b;
}
else
{
cout<<"\n"<<b<<" IS LARGER AND IS SET TO -1.";
b=-1;
cout<<"\nTHE NEW VALUES ARE : "<<a<<"\t"<<b;
}
}
void main()
{
int a,b;
clrscr();
cout<<"ENTER TWO NUMBER : "<<endl;
cin>>a>>b;
setlarge(a,b);
getch();
}



/*OUTPUT
ENTER TWO NUMBER :
34
45
45 IS LARGER AND IS SET TO -1.
THE NEW VALUES ARE : 34 -1
*/



_____________http://sundcs.blogspot.com/____________




// TO INVOKE A FUNCTION TAKING NO ARGUMENTS AND RETURNING NO VALUE

#include<iostream.h>
#include<string.h>
#include<conio.h>
void func(void);
void main()
{
clrscr();
func();
getch();
}
void func(void)
{
char name[25];
cout<<"\nENTER YOUR NAME : ";
cin.getline(name,25);
int len=strlen(name);
cout.write("HELLO ",7).write(name,len);
return;
getch();
}



_____________http://sundcs.blogspot.com/____________




/* TO INVOKE A FUNCTION THAT TAKES TWO INTEGERS AND AN ARITHMETIC OPERATOR THEN DISPLAYS THE CORRESPONDING RESULT.*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
void calc(int,int, char);
int a,b;
char ch;
cout<<"\nENTER TWO INTEGERS : "<<endl;
cin>>a>>b;
cout<<"\nENTER ATHE ARITHMETIC OPERATOR (+,-,*,%) : \n";
cin>>ch;
calc(a,b,ch);
getch();
}
void calc(int x, int y, char c)
{
switch(c)
{
case '+': cout<<"\nSUM OF " <<x<<" AND "<<y<< " IS " <<(x+y);
break;
case '-': cout<<"\nDIFFERENCE OF " <<x<<" AND " << y;
cout<<" IS " << (x+y);
break;
case '*': cout<<"\nPRODUCT OF " <<x<<" AND "<<y;
cout<<" IS "<<(x*y);
break;
case '/': if(x<y)
{
cout<<"\nFIRST INTEGER SHOULD BE ";
cout<<"GREATER THAN THE SECOND.";
exit(0);
}
cout<<"\nQUOTIENT " <<x<<" / " <<y<<" IS "<<(x/y);
break;
case '%': if(x<y)
{
cout<<"\nFIRST INTEGER SHOULD BE ";
cout<<"GREATER THAN THE SECOND.";
exit(0);
}
cout<<"\nREMAINDER : "<<x<<" % " <<y<<" IS "<<(x%y);
break;
default : cout<<"\nWRONG OPERATOR!!!";
break;
}
return;
}



_____________http://sundcs.blogspot.com/____________




// TO SORT AN INTEGER ARRAY USING FUNCTION

#include<iostream.h>
#include<conio.h>
void sort(int num[], int size)
{
int swap=1, temp;
do
{
swap=0;
for(int i=0;i<size-1;i++)
{
if(num[i]>num[i+1])
{
temp=num[i];
num[i]=num[i+1];
num[i+1]=temp;
swap=1;
}
}
}while(swap==1);
cout<<"\n";
}
void main()
{
void sort(int[],int s);
int n[100], s=10, temp, count;
clrscr();
cout<<"\nENTER 10 NUMBERS FOR AN ARRAY TO BE SORTED :\n";
for(count=0;count<s;count++)
cin>>n[count];
temp=count;
sort(n,count);
for(count=0;count<temp;count++)
cout<<n[count]<<"\t";
getch();
}



/*OUTPUT
ENTER 10 NUMBERS FOR AN ARRAY TO BE SORTED :
98
56
768
435
43
54
3245
56
45
67
43 45 54 56 56 67 98 435 768 3245
*/



_____________http://sundcs.blogspot.com/____________




//STUDENT MARKSHEET USING FUNCTIONS

#include<iostream.h>
#include<conio.h>
void main()
{
int getdata(void);
float calcperc(int,int,int);
void printresults(int,int,int,float,char);
char calcgrade(float);
int m1,m2,m3,tot;
float perc;
char grade;
clrscr();
m1=getdata();
m2=getdata();
m3=getdata();
perc=calcperc(m1,m2,m3);
grade=calcgrade(perc);
printresults(m1,m2,m3,perc,grade);
getch();
}
int getdata(void)
{
int marks;
void err_msg(void);
do
{
cout<<"ENTER MARKS : ";
cin>>marks;
if(marks<0||marks>100)
err_msg();
}while(marks<0||marks>100);
return marks;
}
void err_msg(void)
{
cout<<"MARKS ARE OUT OF 100"<<endl;
cout<<"\nPLEASE ENTER MARKS BETWEEN 0 TO 100";
cout<<"\nTRY AGAIN";
getch();
}
float calcperc(int s1,int s2,int s3)
{
float per;
per=(s1+s2+s3)/3;
return per;
}
char calcgrade(float per)
{
char grade;
if(per>80)
grade='A';
else if(per>60)
grade='B';
else if (per>40)
grade='C';
else
grade='D';
return grade;
}
void printresults(int s1,int s2,int s3,float p,char g)
{
cout<<"MARKS IN SUBJECT 1 : "<<s1;
cout<<"\nMARKS IN SUBJECT 2 : "<<s2;
cout<<"\nMARKS IN SUBJECT 3 : "<<s3;
cout<<"\nPERCENTAGE SCORE : " << p;
cout<<"\nGRADE SECURED : "<<g;
}



/*OUTPUT
ENTER MARKS : 98
ENTER MARKS : 89
ENTER MARKS : 78
MARKS IN SUBJECT 1 : 98
MARKS IN SUBJECT 2 : 89
MARKS IN SUBJECT 3 : 78
PERCENTAGE SCORE : 88
GRADE SECURED : A
*/



_____________http://sundcs.blogspot.com/____________




// PALINDROME STRING

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch[10];
int scount,ocount,read=1, len;
clrscr();
cout <<"ENTER A WORD : ";
cin>>ch;
len=strlen(ch);
for(scount=1, ocount=len; scount<=len/2 || ocount>=len/2; scount++, ocount--)
{
if(ch[scount]!=ch[ocount])
read=0;
}
if(read==1)
cout<<"YES! THE WORD IS PALINDROME.";
else
cout<<"NO! THE WORD IS NOT PALINDROME.";
getch();
}



_____________http://sundcs.blogspot.com/____________




//FIND CHARACTER POSITION IN A STRING

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int findpos(char s[],char c);
char string[80],ch;
int y=0;
cout<<"\nENTER MAIN STRING :\n";
cin.getline(string, 80);
cout<<"\nENTER CHARACTER TO BE SEARCH FOR : ";
cin.get(ch);
y=findpos(string,ch);
if(y==-1)
cout<<"\nSORRY!! THE CHARACTER IS NOT IN STRING.";
getch();
}
int findpos(char s[], char c)
{
int flag=-1;
for(int i=0; s[i]!='\0';i++)
{
if(s[i]==c)
{
flag=0;
cout<<"\nTHE CHARACTER IN THE STRING IS AT POSITION : "<<i+1;
}
}
return (flag);
}



/*OUTPUT
ENTER MAIN STRING :
RACHIT IS WATCHING A FREE ZEBRA IN THE PARK
ENTER CHARACTER TO BE SEARCH FOR : Z
THE CHARACTER IN THE STRING IS AT POSITION : 27
*/



_____________http://sundcs.blogspot.com/____________




// PROGRAM TO FIND WHETHER TWO STRINGS CONTAIN EQUAL NUMBER OF CHARACTERS.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50], str2[50];
clrscr();
cout<<"ENTER THE FIRST STRING : ";
cin.getline(str1, 49);
cout<<"ENTER THE SECOND STRING : ";
cin.getline(str2, 49);
if(strlen(str1)==strlen(str2))
{
cout<<"\nBOTH STRINGS CONTAIN EQUAL NUMBER OF CHARACTERS.";
}
else
{
cout<<"\nBOTH STRINGS CONTAIN DIFFERENT NUMBER OF CHARACTERS.";
}
getch();
}



/*OUTPUT
ENTER THE FIRST STRING : GOOD EVENING
ENTER THE SECOND STRING : GOOD MORNING
BOTH STRINGS CONTAIN EQUAL NUMBER OF CHARACTERS.
ENTER THE FIRST STRING : HELLO! CAN I SPEAK TO RAM
ENTER THE SECOND STRING : OF COURSE! HERE HE IS..
BOTH STRINGS CONTAIN DIFFERENT NUMBER OF CHARACTERS.
*/



_____________http://sundcs.blogspot.com/____________




// CHECK WHETHER A CHARACTER IS ALPHANUMETRIC OR NOT

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
clrscr();
char ch;
int a;
cout<<"ENTER A CHARACTER : ";
cin>>ch;
a=ch;
if(isalnum(a))
{
cout<<"\nIT IS AN ALPHANUMERIC ";
if(isdigit(a))
cout<<"AND DIGIT CHARACTER. ";
else
cout<<"AND ALPHABETIC CHARACTER.";
}
else
cout<<"\nIT IS SOME OTHER NON-ALPHANUMERIC CHARACTER.";
getch();
}



/*OUTPUT
ENTER A CHARACTER : R
IT IS AN ALPHANUMERIC AND ALPHABETIC CHARACTER.
ENTER A CHARACTER : 7
IT IS AN ALPHANUMERIC AND DIGIT CHARACTER.
ENTER A CHARACTER : %
IT IS SOME OTHER NON-ALPHANUMERIC CHARACTER.
*/



_____________http://sundcs.blogspot.com/____________




// CHANGE THE CASE OF A CHARACTER

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
char ch;
cout<<"\nENTER A CHARACTER : ";
cin>>ch;
if(ch=='\n')
{
ch=getchar();
}
if(isalpha(ch))
{
if(islower(ch))
{
cout<<"\nYOU INPUT A LOWERCASE ALPHABET.";
ch=ch-32;
cout<<"\n\nTHE UPPERCASE ALPHABET IS : "<<ch;
}
else if(isupper(ch))
{
cout<<"\nYOU INPUT AN UPPERCASE LETTER.";
ch=ch+32;
cout<<"\n\nTHE LOWERCASE ALPHABET IS : "<<ch;
}
}
else
cout<<"\nYOU INPUT A NON-ALPHABETICAL CHARACTER.";
getch();
}



/*OUTPUT
ENTER A CHARACTER : R
YOU INPUT AN UPPERCASE LETTER.
THE LOWERCASE ALPHABET IS : r
ENTER A CHARACTER : r
YOU INPUT A LOWERCASE ALPHABET.
THE UPPERCASE ALPHABET IS : R
*/



_____________http://sundcs.blogspot.com/____________




// COPY SMALLER STRING INTO THE BIGGER STRING

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[50], str2[50];
int len1, len2;
cout<<"\nENTER THE FIRST STRING: \n";
cin.getline(str1, 50);
cout<<"\nENTER THE SECOND STRING :\n";
cin.getline(str2, 50);
if(strlen(str1)>strlen(str2))
{
strcpy(str1, str2);
cout<<"\nSECOND STRING IS COPIED INTO THE FIRST STRING. \n";
cout<<str1;
}
else if(strlen(str2)>strlen(str1))
{
strcpy(str2,str1);
cout<<"\nFIRST STRING IS COPIED INTO THE SECOND STRING.\n";
cout<<str2;
}
else if(strlen(str1)==strlen(str2))
{
cout<<"\nSTRINGS ARE OF EQUAL SIZE. \n";
cout<<"\nSTRING1 IS : ";
cout<<str1;
cout<<"\nSTRING2 IS : ";
cout<<str2;
}
getch();
}



/*OUTPUT
ENTER THE FIRST STRING:
RACHIT IS GOING TO SCHOOL
ENTER THE SECOND STRING :
KHUSHAL IS GOING FOR MATCH
FIRST STRING IS COPIED INTO THE SECOND STRING.
RACHIT IS GOING TO SCHOOL
*/



_____________http://sundcs.blogspot.com/____________




// APPEND THE FIRST STRING TO THE SECOND

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[25], str2[50];
cout<<"\nENTER FIRST STRING : ";
cin.getline(str1, 25);
cout<<"\nENTER THE SECOND STRING : ";
cin.getline(str2, 25);
strcat(str2, str1);
cout<<"\n"<<str2;
getch();
}



/*OUTPUT
ENTER FIRST STRING : MORNING
ENTER THE SECOND STRING : GOOD
GOOD MORNING
ENTER FIRST STRING : AGRAWAL
ENTER THE SECOND STRING : ABHISHEK
ABHISHEK AGRAWAL*/



_____________http://sundcs.blogspot.com/____________




//TO JUSTIFY A GIVEN STRING

#include<iostream.h>
#include<conio.h>
char str1[80],str2[80];
int count1=0,count2=0,count3=0,count4=0,count5=0, count=0;
void white_spaces(char a[]);
void justify(int);
void main()
{
clrscr();
cout<<"ENTER A STRING : ";
cin.getline(str1, 80);
white_spaces(str1);
justify(count4);
cout<<"\nTHE JUSTIFIED STATEMENT IS :\n";
for(count=0;count<80;count++)
cout<<str2[count];
getch();
}
void white_spaces(char a[])
{
for(count=0;count<80;count++)
{
if(a[count]==' ')
count1++;
else
count2++;
if(a[count]=='\0')
{
count3=80-count2-count1;
break;
}
}
count4=count3/count1;
}
void justify(int b)
{
for(count=0, count1=0;count1<80;count++, count1++)
{
str2[count1]=str1[count];
if(str1[count]==' ')
{
for(count5=0;count5<b;count5++)
{
count1++;
str2[count1]=' ';
}
}
}
}



_____________http://sundcs.blogspot.com/____________

No comments:

Post a Comment