xxxxxxxxxx
int main()
{
std::cout << "hey pallu!";
return 0;
}
To print in different lines: \n or we can use std::endl
xxxxxxxxxx
int main() {
std::cout << "hey pallu!" << "\n"; // Prints "hey pallu!" and moves to a new line
std::cout << "hey pallu!"; // Prints "hey pallu!" without a new line
return 0;
}
xxxxxxxxxx
\#include <iostream>
int main() {
std::cout << "hey pallu!" << std::endl; *// Prints "hey pallu!" and moves to a new line*
std::cout << "hey pallu!"; *// Prints "hey pallu!" without a new line*
return 0;
}
Giving input by user:
xxxxxxxxxx
\#include <iostream>
\#include <string>
int main() {
*// Variables to store user input*
int age;
std::string name;
*// Get user's name and age from input (no prompts)*
std::getline(std::cin, name); *// Use getline for string input (name)*
std::cin >> age; *// Use std::cin for integer input (age)*
*// Output the received information*
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;
return 0;
}
Instead of using std:: every time we can use namespace
xxxxxxxxxx
\#include <iostream>
using namespace std;
int main() {
cout << "hey pallu!" << endl; *// Prints "hey pallu!" and moves to a new line*
cout << "hey pallu!"; *// Prints "hey pallu!" without a new line*
return 0;
}
If you want to take int or anything as a input. We use cin>>:
xxxxxxxxxx
\#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
cout << "value of x: " << x;
return 0;
}
xxxxxxxxxx
\#include <iostream>
using namespace std;
int main() {
int x,y;
cin >> x >> y;
cout << "value of x: " << x << " and y: " << y;
return 0;
}
bits/stdc++.h - it includes all the libraries in c++
Here we are using string datatyp
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
cout << s;
return 0;
}
But if we give input as : hey pallavi
it will only give output as hey
So if we want to get all the hey pallavi as output we have to define another string
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
string s1,s2;
cin >> s1 >> s2;
cout << s1 << " " << s2;
return 0;
}
So if we want to do that in one string we have to use getline
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
getline(cin, str);
cout << str;
return 0;
}
here input : hey pallu
output : hey pallu
-> for char we declare it in single quotes
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
char ch = 'p';
cout << ch;
return 0;
}
Q) write a program that takes input as age and prints if you are adult or not:( if-else)
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
int age;
cin >> age;
if(age>=18){
cout << "you are an adult";
}
else {
cout << "you are not an adult";
}
return 0;
}
Q)
If marks are less than 25, it prints "Grade: F."
If marks are between 25 and 44 (inclusive), it prints "Grade: E."
If marks are between 45 and 49 (inclusive), it prints "Grade: D."
If marks are between 50 and 59 (inclusive), it prints "Grade: C."
If marks are between 60 and 79 (inclusive), it prints "Grade: B."
80 - 100 print grade A
ANSWER:
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
int marks;
cin >> marks;
if (marks < 25) {
cout << "F" ;
} if (marks >= 25 && marks <= 44) {
cout << " E" ;
} if (marks >= 45 && marks <= 49) {
cout << " D" ;
} if (marks >= 50 && marks <= 59) {
cout << " C" ;
} if (marks >= 60 && marks <= 79) {
cout << "B" ;
} if (marks >= 80 && marks <= 100) {
cout << " A" ;
}
return 0;
}
input - 18
output - F
-> But this is not the correct way to write. Because it will execute all the if blocks even if the output is available in first if block.
For this we use else if:
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
int marks;
cin >> marks;
if (marks < 25) {
cout << "F" ;
} else if (marks >= 25 && marks <= 44) {
cout << " E" ;
} else if (marks >= 45 && marks <= 49) {
cout << " D" ;
} else if (marks >= 50 && marks <= 59) {
cout << " C" ;
} else if (marks >= 60 && marks <= 79) {
cout << "B" ;
} else if (marks >= 80 && marks <= 100) {
cout << " A" ;
}
return 0;
}
Here if we get output in the first if condition. It will not go through all the else if
we can make the code more easily:
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
int marks;
cin >> marks;
if (marks < 25) {
cout << "F" ;
} else if ( marks <= 44) {
cout << " E" ;
} else if (marks <= 49) {
cout << " D" ;
} else if ( marks <= 59) {
cout << " C" ;
} else if ( marks <= 79) {
cout << "B" ;
} else if ( marks <= 100) {
cout << " A" ;
}
return 0;
}
Q) if age < 18, print : not eligible for job
if age >= 18, print : eligible for job
if age >= 55 and age <= 57, print : eligible for job, but retirement soon
if age > 18, print : retirement time
Here to solve this we use nested if
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
int age;
cin >> age;
if (age < 18) {
cout << "Not eligible for job" ;
} else if (age <= 57) {
cout << " eligible for job " ;
if(age >= 55){
cout << "retirement soon";
}
} else {
cout << "retirement time" ;
}
return 0;
}
SWITCH STATEMENTS:
Q) Take the day number and print the corresponding day.
ex: for 1 print monday
xxxxxxxxxx
\#include <bits/stdc++.h>
using namespace std;
int main() {
int day;
cin >> day;
switch(day){
case 1:
cout << "monday";
break;
case 2:
cout << "tuesday";
break;
case 3:
cout << "wednesday";
break;
case 4:
cout << "thursday";
break;
case 5:
cout << "friday";
break;
case 6:
cout << "saturday";
break;
case 7:
cout << "sunday";
break;
default:
cout << "invalid";
}
return 0;
}
Input - 2
output - tuesday
Input - 19
output - invalid