CodeISM CR-1 Class 1
CodeISM CR-1 Class 1
Compilers
- Computers understand only 0101010…. (machine or
binary code)
- We write code in a programming language
- So, we need to use a compiler to translate our
programming language code to the 010101.. (binary
or machine code) form
- Compiler also checks for errors and tells you, if there
is any error, along with the line number, in which the
error is present
#include <bits/stdc++.h>
using namespace std;
int main()
{
// You write your code here
return 0;
}
1. #include <bits/stdc++.h>
// This line imports all the standard
// necessary libraries
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b,result;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
result=a*b;
cout<<"Answer is: "<<result<<'\n';
return 0;
}
Program to use multiple inputs in same line
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b,result;
cout<<"Enter 2 numbers:";
cin>>a>>b;
result=a*b;
cout<<"Answer is: "<<'\n'<<result;
return 0;
}
Data Types
1. int // integer
Range of integer in C++ is -2^31 to +2^31
(-2,147,483,648 to 2,147,483,647)
2. long int // long integer
Range increases
3. long long int // long long integer
Range is -10^(18) to +10^(18)
Exact range of long long int
(No need to remember this. Just remember
approx range from above)
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
int main()
{
long double abc=23.2929;
cout<<abc*2;
return 0;
}
Program to print a word taken from user
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<"str="<<str<<'\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str,str2;
cin>>str>>str2;
cout<<"words are "<<str<<" "<<str2<<'\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin,str);
cout<<"Full sentence is "<<str;
return 0;
}
Constants
#Functions in C++ -
- A function is a group of statements that together perform a task.
- A function may take some inputs (or arguments) and may or may not
return a value.
Like a sin(x) function may return a float value but a function which just
prints something, may not return a value
#include <bits/stdc++.h>
using namespace std;
int main()
{
string name;
int age;
double height;
cin>>name;
cin>>age;
cin>>height;
return 0;
}
Homework Questions :
1. Write a program to print the cube of a number (take user
input)
2. Write a program to take a temperature in degree Celsius and
print the equivalent temperature in degree Fahrenheit.
3. Write a program to take the radius of a circle as input and
print the area and perimeter of the circle in different lines
4. Write a program to take a First Name and Last Name of a
user and print the full name. Also take college name (with
spaces) as input and print it.
5. Implement your own subtraction, multiplication, and division
functions which take two numbers as an input and return the
answer after performing the given task.
6. Write a program to swap the value of 2 variables.
[ Hint: Maybe, using a third variable helps? ]
7. Write a program to swap the value of 2 variables without
using any 3rd variable
8. Write a program using a function to swap the values of 2
variables using a function. The values swapped inside the
function, should also be swapped outside that function.
9. Write a program to return the remainder when a number is
divided by another (Take both numbers as input)
Solution of Q.4
#include <bits/stdc++.h>
using namespace std;
/*
Write a program to take a First Name and Last Name of a user
and print the full name.
Also take college name (with spaces) as input and print it.
*/
int main()
{
string fname;
string lname;
cout<<" Enter first name: ";
cin>> fname;
cout<<" Enter last name: ";
cin>>lname;
cout<<"Full name is : "<<fname<<" "<<lname<<'\n';
string college;
cout<<" Enter college name: ";
getline(cin, college);
getline(cin, college); // getline stops reading input when
"Enter key" is pressed
cout<<"College name is :"<<college;
return 0;
}
6.
#include <bits/stdc++.h>
using namespace std;
/*
Swap 2 variables.
*/
int main()
{
// = operator moves RHS value to LHS
int a=3;
int b=5;
int t;
cout<<"Earlier values \n";
cout<< a<< " "<< b;
t = b;
b = a;
a = t;
cout<<"Values now \n";
cout<<a<<" "<<b;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*
Swap 2 variables without a third new variable.
*/
int main()
{
// = operator moves RHS value to LHS
int a=3;
int b=5;
cout<<a<<" "<<b<<'\n';
a = a + b;
b = a - b;
a = a - b;
cout<<a<<" "<<b<<'\n';
return 0;
}