Tricky and fun programming in C++

– Take input when number of Inputs are not given in C++.This is simple example of problems in which the of inputs are not given and also it can be use as a tricky programming.#include <iostream>
using namespace std;
int main()
{
int t,c=0;
while(cin>>t)
c++;
cout<<c<<endl;
return 0;
}Here when input is given each timecin>>twill return’1’otherwise it will return’0’that makeswhileconditionfalse.
The number of inputs are store in variable’c’as it increase by1each timewhilecondition istrue.Display output without using semicolon “;” .Here it is shown that how to display output without using semicolon “;” at the end of line.#include <iostream>
using namespace std;
int main()
{
int t,c=0;
if(cout<<c<<endl)
c++;
cout<<c<<endl;
return 0;
}HereIfcondition becomes true ascout<<c<<endlreturn1because of that value of variablecincrease by1. Same thing can be done in C programming , just useprintfinifcondition.#include <stdio.h>
int main()
{
int c=0;
if(printf(“Hello World!n”))
c++;
printf(“%d”,c);
return 0;
}

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *