Programs LMS
Programs LMS
Contents:
/*
std::cin and std::cout always go on the left-hand side of the statement.
std::cout is used to output a value (cout = character output)
std::cin is used to get an input value (cin = character input)
<< is used with std::cout, and shows the direction that data is moving (if std::cout represents
the console, the output data is moving from the variable to the console). std::cout << 5 moves
the value of 5 to the console
>> is used with std::cin, and shows the direction that data is moving (if std::cin represents the
keyboard, the input data is moving from the keyboard to the variable). std::cin >> x moves
the value the user entered from the keyboard into x
*/
Enter a number: 8
Enter number of terms to be printed: 8
8 8 8 8 8 8 8 8
//Program - while
#include <iostream>
int main()
{
int sum=0,val = 1,limit;
std::cout<<"Enter a limit: ";
std::cin>>limit;
while(val<=limit) {
sum+=val;
val++;
}
std::cout<<"Sum of 1 to "<<limit<<" is "<<sum;
return 0;
}
Enter a limit: 5
Sum of 1 to 5 is 15
//Program - do....while
#include <iostream>
using namespace std;
int main(){
int sum=0,num;
do
{
cout<<"Enter a number\n";
cin>>num;
sum+=num;
} while(num!=0);
cout<<"Sum of all the entered values = "<<sum;
return 0;
}
Enter a number
12
Enter a number
34
Enter a number
0
Sum of all the entered values = 46
Enter a limit: 4
This is Iteration 1
This is Iteration 2
This is Iteration 3
This is Iteration 4
Iteration -100
Intermediate sum -100
Iteration -99
Intermediate sum -199
Iteration -98
Intermediate sum -297
Iteration -97
Intermediate sum -394
Iteration -96
Intermediate sum -490
//String length
#include <iostream>
using namespace std;
int main()
{
string txt1,txt2;
cout<<"Enter a string: ";
cin>>txt1;
cout<<"Enter another string: ";
cin>>txt2;
cout<<"The length of the first string is "<<txt1.length()<<". The length of the next string is
"<<txt2.size()<<".";
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"\"Datatypes in C++\"";
int i1,i2,i,j,pos;
cout<<"\nEnter two integer values: ";
cin>>i1>>i2;
cout<<"Entered integer values are "<<i1<<" and "<<i2;
double d1,d2;
cout<<"\n\nEnter two double values: ";
cin>>d1>>d2;
cout<<"Entered double values are "<<d1<<" and "<<d2;
bool isbool;
cout<<"\n\nBoolean Results: ";
isbool=i1>i2;
cout<<"\nInteger: "<<i1<<">"<<i2<<" is "<<isbool;
isbool=d1>d2;
cout<<"\nDouble: "<<d1<<">"<<d2<<" is "<<isbool;
char c;
cout<<"\n\nEnter a character: ";
cin>>c;
cout<<"The ASCII value of "<<c<<" is "<<int(c);
"Datatypes in C++"
Enter two integer values: 2
3
Entered integer values are 2 and 3
Boolean Results:
Integer: 2>3 is 0
Double: 3.3>2.2 is 1
Enter a character: z
The ASCII value of z is 122
String array
#include <iostream>
using namespace std;
int main()
{
string hello = "Hello";
cout<<hello;
string world(hello);
cout<<"\n";
cout<<world;
string msg("Hi.. Good morning");
cout<<"\n";
cout<<msg;
string val(5,'h');
cout<<"\n";
cout<<val;
return 0;
}
Hello
Hello
Hi.. Good morning
hhhhh
#include<iostream>
using namespace std;
int main()
{
string word;
while (cin>>word)
{
if(word=="end")
break;
cout<<word<<endl;
}
return 0;
}
Joseph
Joseph
Kevin
Kevin
Kumar L
Kumar
L
end
#include<iostream>
using namespace std;
int main()
{
string s;
string st("Enjoy well");
cout<<"The size is "<<st.size();
if(st.size()!=0)
cout<<"\nNon-empty string encountered.";
if(s.empty())
cout<<"\nEmpty string encountered.";
cout<<"\nEnter a string: ";
getline(cin,s);
//s="I am not well";
if(s>st)
cout<<"\nNew string is greater. ";
else if(s==st)
cout<<"\nSame strings ";
else
cout<<"\nNew string is lesser. ";
return 0;
}
The size is 10
Non-empty string encountered.
Empty string encountered.
Enter a string: Enjoy well
Same strings
#include<iostream>
using namespace std;
int main()
{
int i;
string s;
cout<<"\nEnter a string: ";
getline(cin,s);
//cout<<s.length();
for(i=0;i<s.size();i++)
cout<<s[i];
return 0;
}
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
int i, punct=0;
string s("Ah!!!\"That's awesome buddy!\"");
cout<<"The string "<<s<<" has "<<s.size()<<" characters.";
cout<<"\nPunctions are ";
for(i=0;i<s.size();i++)
if(ispunct(s[i]))
{
cout<<s[i];
punct++;
}
cout<<"\nTotal punctuation count: "<<punct;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int *val = new int;
int *ival = new int(5);
cout<<*val;
cout<<"\n"<<*ival;
delete val,ival;
return 0;
}
0
5
#include <iostream>
using namespace std;
int main()
{
int n1,n2;
//Function declaration
int findlarger(int,int);
cout<<"Enter two numbers: ";
cin>>n1>>n2;
cout<<"The larger number is: "<<findlarger(n1,n2);//n1 and n2 are Actual parameters
return 0;
}
//Function definition
int findlarger(int val1,int val2)//val1 and val2 are Formal parameters
{
if (val1>val2)
return val1;
else
return val2;
}
Enter two numbers: 44
22
The larger number is: 44
#include <iostream>
#include <string>
using namespace std;
class Animal
{
//Access specifier
private:
//Private data member
string name[30];//Cannot be accessed from outside
public:
//Public data members
int num=5;//can be accessed
void display();//Member function
//Function definition
void display(string name)
{
cout<<"The name of the animal entered by the user in the main function is "<<name;
cout<<"\n";
cout<<"\nThe value of an integer data member is "<<num;//Displays assigned value 10 in
the object not 5 that is used in the class
}
};
int main()
{
int i=0;
char letter;
char animalarr[30];
cout<<"Enter an animal name: ";
while(cin>>letter)
{
if(letter!='x')
{
animalarr[i]=letter;
i++;
}
else
{
class fnoverloading
{
public:
int a, b;
char c, d;
float e, f;
double g,h;
void getinput(int ch)
{
switch(ch)
{
case 1:
cout<<"\nEnter Integers: ";
cin>>a>>b;
break;
case 2:
cout<<"\nEnter Characters: ";
cin>>c>>d;
break;
case 3:
cout<<"\nEnter Float values: ";
cin>>e>>f;
break;
case 4:
cout<<"\nEnter double values: ";
cin>>g>>h;
break;
}
}
void display(int n1, int n2)
{
a=n1;
b=n2;
cout<<"\nIntegers are "<<n1<<" and "<<n2;
sum(1);
}
void display(char n1, char n2)
{
c=n1;
d=n2;
cout<<"\nChar values are "<<n1<<" and "<<n2;
Enter Integers: 3
6
Enter Characters: a
z