Fun With Programming for Beginners!

1: 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 time cin>>t will return ‘1’ otherwise it will return ‘0’ that makes ¬†while condition false. The number of inputs are store in variable ‘c’ as it increase by 1 each time while condition is true.

 

2: 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;
 }

 Here If condition becomes true as cout<<c<<endl return 1 because of that value of variable c increase by 1. Same thing can be done in C programming , just use printf in if condition.

#include <stdio.h>
int main() 
{
  int c=0;
  if(printf("Hello World!\n"))
  c++;
  printf("%d",c);
  return 0;
 }