Friday, March 19, 2021

If else conditions in C++

 

Q. Write a program that takes a character as input and prints either 1, 0 or -1 according to the following rules.

1, if the character is an uppercase alphabet (A - Z)

0, if the character is a lowercase alphabet (a - z)

-1, if the character is not an alphabet

Input format :
Single Character
Output format :
1 or 0 or -1
Constraints :
Input can be any character
Sample Input 1 :
v
Sample Output 1 :
0
-------------------------------------------------------------------------------------------------------
Code:
#include<iostream>
using namespace std;

int main() 
{
	char ch;
	cin >> ch;
	if(ch >= 65 && ch <= 90)
		cout << "1";
	else if(ch >= 97 && ch <=122)
		cout << "0";
	else
		cout << "-1";
        return 0;
}
-------------------------------------------------------------------------------------------------------
Output:


Download File: ⚓Click here

Description:
In this program we have declared a character variable "ch" and taken input in it. Next thing 
is we are checking the ASCII values of the inputted character belongs to a particular range or
not. If belongs to that range and fulfill the condition then the respective "0" or 1 will be print-
ed  otherwise else statement will be executed.

Wednesday, March 17, 2021

Hello World Program in C++ - cplusplusexporer

Q. Write and explain Hello World Program in C++.

Code:


//simple C++ program to display "Hello World"
//header / library files
#include <iostream>
using namespace std;

//main program where the execution of program begins
int main()
{
//prints Hello world
cout << "Hello World";
//returning 0 to system and terminating program
return 0;
}


Output:





Download File:
 Click Here

Description:

1//simple C++ program to display "Hello World": This is a comment line in C++. Comment line is used to note additional information about the program. It does not contain any logic and does not considered for compilation of program (no effect on program). We can use /* ...  */  for multiline comment.

2#include: In C++, lines starting with '#' symbol are known as directives and are processed by preprocessor which is a program invoked by the compiler. The #include directive tells the compiler to include a file and #include<iostream> . It tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions.

3using namespace std: This is used to shorten the code line. This is used to import the entirety of the std namespace into the current namespace of the program.

4int main() : This is the called as the main function. The execution of every C++ program starts with main() function. So every program in C++ must contain a main function.

5{ and }: In C++, '{' is the called as the opening braces and '}' is the closing braces of a function. In C++, when a function is called, the line statements between { and } of that function are executed.

6. std::cout: cout tells computer what to print on screen. So here this line tells computer to print Hello world on screen.

7return 0: In C++, the return 0 statement is used to return a value from a function and indicates the finishing of a function.

You may also like:

If else conditions in C++