Sunday 13 May 2018

To check whether a number is prime or not?

C++ program to find a number is prime or not?

Algorithm:

Input = number

set counter=0;

set i=0 to i<=number

if(number%i==0)
counter=counter+1;
 i=i+1;
if(counter == 2)
print"number is prime";

else
print "number is not prime";
    
exit;
    
Approach to solve:

Input = 5;
number%i=5%1=0
5%2=1
5%3=2
5%4=1
5%5=0

counter=2
so, it will print number is prime.

Input =4
number%i=4%1=0
4%2=0
4%3=1
4%4=0

counter=3
so, it will print number is not prime.

Program:-
#include<iostream>

using namespace std;
//function for prime number
int prime(int num){
 
 int count=0;
 if(num==1){
  return 0;
 }
 
 for(int i=1; i<=num; i++){
  
   if(num%i==0)
     count++;
 }
 
 if(count==2){
  cout<<"Number is Prime ";
 }
 
 else
 {
  cout<<"Number is not prime ";
 }
 
}

int main(){
 
 int num1;
 
 cout<<"Enter any number:";
 cin>>num1;
 
 prime (num1);  //function calling
 
 return 0;
}

Output:-
Enter any number:3
Number is prime

Enter any number:88
Number is not prime


HAPPY CODING...

Sunday 11 February 2018

Types of user-defined functions in C++

In this post, you will learn more about user-defined functions and how can you use user-defined functions in different ways?

Basically, functions are categorized on the basis of return value and arguments, user-defined functions can be categorized as:



  • Function with no argument and no return value
  • Function with no argument but return value
  • Function with argument but no return value
  • Function with argument and return value
Consider a situation in which you have to make a program to find the cube of a number. You can use four different methods to solve this problem:-


Program -1: No arguments passed and no return value

#include<iostream>

using namespace std;

void fun(){   /* function with no arguments and return value */
 
 int a,c;
 
 cout<<"Enter a number: ";
 cin>>a;
 
 c=a*a*a;
 
 cout<<"cube is = "c;
 
}

int main(){
 
 fun();   // function call
 
 return 0;
}
In the above program, fun() have no arguments and also no return value because function return type is void.
function calling in main() function by the name of function i.e fun().

Program - 2: No arguments passed but a return value

#include<iostream>

using namespace std;

int fun(){  /* function with return value but no arguments */
 
 int a,c;
 cout<<"Enter any number: ";
 cin>>a;
 
 c=a*a*a;
 
 return c;
 
}
int main(){
 // function call
 cout<<"cube is = "<<fun();
 
 return 0;
}
In this program, function i.e. fun() have no arguments but return a value.In main() function, we call this function and also print it in main().


Program - 3: Arguments passed but no return value

#include<iostream>

using namespace std;

 void fun(int a){ /* function with argument but no return type */
  
  int c;
  
  c=a*a*a;
  
  cout<<"cube is = "<<c;
  
   }
   
int main(){
  
  fun(2); /* function calling and passing a value*/
  
  return 0;
}
Above program have an argument but no return value because the return type of function is void.
calling of function is in main() function and passing a value in fun().

Program - 4: Arguments passed and a return value

#include<iostream>

using namespace std;

int fun(int a){ /* function with argument and return value */
 
 int c;
 
 c=a*a*a;
 
 return c;
 
}

int main(){
// function call and value passing 
 cout<<"cube is = "<<fun(3); 
 
 return 0;
}
In the above program, fun() have an argument and also return value.

NOTE: C++ have the top to bottom approach that's why no function declaration.

Also, you can choose the above methods according to your need.

HAPPY CODING...



Wednesday 31 January 2018

C++ functions

In this article, you will learn about the functions in C++.why and how we use functions.

What is a function in C++?

A function is a block of code that performs some operation.If we want to use a piece of code again and again in the program we wrap that piece of code in the function and use that piece of code in the program by calling it.

There are two types of functions in C++.
  • Library function or built-in function
  • User-defined function 
Library function:-

Library  function are built-in function in C++.

you can use these function by invoking function directly, you don't need to write these functions.Example sqrt() in cmath header file,strcat() in string header file.

User-defined function:-

C++ allows the programmer to define their own function.

If you want to reuse a piece of code.so you can define that code of piece outside the main() function then you can call it in main() function and it will execute in the piece of code.

Program:

C++  program to add two integers by using user-defined function:-
#include <iostream>
using namespace std;

// Function declaration
int add(int, int);

int main()
{
    int num1, num2, sum;
    cout<<"Enter first number to add: ";
    cin >> num1;
    cout<<"Enter second number to add: ";
    cin >> num2;

    // Function call
    sum = add(num1, num2); //actual parameters
    cout << "Sum = " << sum;
    return 0;
}

// Function definition
int add(int a, int b) //formal parameters
{
    int add;
    add = a + b;

    // Return statement
    return add;
}
Output:-
Enter first number to add: 56
Enter second number to add: 43
sum = 99
Function declaration:-

User-defined functions are unknown to the compiler if we define functions after the main() function, the compiler will show an error.
In C++, declaration of function Without its body to give compiler information about the user-defined function.
// Function declaration
int add(int, int);
Note:

  • No need to write arguments.
  • It is not necessary to declare function if user-defined function exists before main() function.


Function call:-

For the execution of a function, we call it inside the main() function.

    // Function call
    sum = add(num1, num2);
num1 and num2 are actual parameters.

Function definition:-

It is the piece of code or function itself.
// Function definition
int add(int a, int b) //formal parameters
{
    int add;
    add = a + b;

    // Return statement
    return add;
}
int a and int b are formal parameters.
we provide actual parameters to the function, they are copied to the formal parameters and function use these parameters for the result. 

Return statement:

A function can return a single value to the program.
return add;
In this, function returning the sum of a and b;

HAPPY CODING.........













Wednesday 22 November 2017

Sum of digits of a number

C++ program to find the sum of digits of a number.

Algorithm:-

Input=number
number1=0, sum=0;

while loop > 0

(a) number1 equal to remainder of number divide
    by 10
    number1=number%10;

(b) add sum and number1 to sum
    sum=sum+number1;

(c) divide number by 10
    number=number/10;
    
    print sum;
    
    exit;
    
Approach to solve:-

Input=259
number1=number%10=9
sum=sum+number1=9
number=number/10=25

number1=number%10=5
sum=sum+number1=14
number=number/10=2

number1=number%10=2
sum=sum+number1=16
number=number/10=0

Program:-
#include<iostream>
using namespace std;
int main(){
 int num,sum=0,num1=0;
 cout<<"ENTER THE NUMBER:";
 cin>>num;
 while(num>0)
 {
  num1=num%10;
  sum=sum+num1;
  num=num/10;
  
 }
 cout<<"SUM OF DIGITS:"<<sum;
 return 0;
}

Output:-
ENTER THE NUMBER:5990
SUM OF DIGITS:23
           KEEP CALM AND CODE...

if any query email me at:-sushank123456@gmail.com

Saturday 18 November 2017

C++ program to check whether a number is palindrome or not?

what is a palindromic number?
A palindromic number is a number which remains same when its digits are reversed. Example 121,16461 etc.


Let understand the algorithm of the program.
Input=number
reverse_number=0;
temp=number;
while loop number > 0

(a)multiply reverse_number by 10 and add remainder    of number divide by 10 to reverse_number
   reverse_number= reverse_number*10+number%10;

(b)divide number by 10
   number=number/10;

if(rev_num==temp)
print number is palindrome
else
print number is not palindrome

exit;
In the above program, we store the number in temp then reverse the number and at last, we compare the reversed number with temp.

program:-
#include<iostream>
using namespace std;
int main()
{
 int num,temp,rev_num=0;

 cout<<"Enter the  number:";
 cin>>num;
 temp=num;
 while(num>0)
 {
  rev_num=rev_num*10+num%10;
  num=num/10;
 }
 if(rev_num==temp)
     cout<<"number is palindrome";
  
 else
     cout<<"number is not palindrome";

 return 0;
}
output:-

Enter the number:3456
number is not palindrome

Enter the number:121
number is palindrome
Program to print palindrome number between 1 to 50:-
#include<iostream>
using namespace std;
int main()
{
    int n,num,sum,temp;
    cout<<"palindrome numbers are:";
for(num=1;num<=50;num++)
{
    temp=num;
    sum=0;
while(temp)
{
    n=temp%10;
    temp=temp/10;
    sum=sum*10+n;
}
if(num==sum)
    cout<<"\t"<<num;
}
return 0;
}
Output:-
palindrome numbers are: 1  2  3  4  5  6  7 
8  9  11  22  33  44




Thursday 16 November 2017

How to reverse a number in C++?

Write a C++ program to reverse digits of a number 
Reverse number:-
Input=7890
Output=0987

Algorithm:-
Input=number
reverse_number=0;
while loop number > 0

(a)multiply reverse_number by 10 and add remainder    of number divide by 10 to reverse_number
   reverse_number= reverse_number*10+number%10;

(b)divide number by 10
   number=number/10;

print reverse_number.

Approach to solve:-

Input:num=7890
rev_num=0
rev_num=rev_num*10+num%10=0
num=num/10=789
rev_num=rev_num*10+num%10=09
num=num/10=78
rev_num=rev_num*10+num%10=098
num=num/10=7
rev_num=rev_num*10+num%10=0987
num=num/10=0

Program:-
#include<iostream>
using namespace std;
int main()
{

 int num,rev_num=0;
 cout<<"Enter a number:";
 cin>>num;
 while(num>0)
 {
  rev_num=rev_num*10+num%10;
  num=num/10;
 }
 cout<<rev_num<<endl;


 return 0;
}
Output:-
Enter a number:7890
0987
    HAPPY CODING.......

if any query email me at:-sushank123456@gmail.com




Sunday 22 October 2017

Jump Statement:Break,Go To and Continue statement

JUMP STATEMENTS

Jump statements are used to interrupt the normal flow of the program or you can say that the jump statements are used to jump a particular statement or piece of code.

Types of jump statements:-
  • Break
  • Continue 
  • GoTo
BREAK STATEMENT:-

The break statement is used inside the loop or switch case.It is used to come out from the loop or switch case.When we use break statement it terminates the loop or switch case and shows the result.

Syntax:-
.....
.....
break;
To check how break statement works?click here

CONTINUE STATEMENT:-

Continue statement is used inside the loop.This statement is used to skip a particular part of the code and resume the loop.

Syntax:-
while(test Expression) //you can use for loop and do while loop 
{
//codes
if(condition for continue)
{
 continue;
}
//codes
}
Program to print reverse numbers from 10 to 1 except 5:-
#include<iostream>
using namespace std;

 int  main()
{
    for(int n=10;n>=1;n--)
   {
        if(n==5) continue;
        cout<<"\n"<<n;
    }
    return 0;
}
Output:
10
9
8
7
6
4
3
2
1

The above program print numbers from 10 to 1 except 5 because we use continue statement when n==5 that means it will skip 5 and print remaining numbers.

GO TO STATEMENT:-

GoTo statement is another type of jump statement. it can be used to jump from anywhere to anywhere within in the function, sometimes it is also referred as an unconditional jump statement.

Syntax:-
goto label;
.
.
.
label:

Or
label:
.
.
.
goto label;
The label is an identifier.When goto statement is encountered, control of the program jumps to label: and start executing the code.

Program to print the square of positive numbers:-

#include<iostream>
#include<conio.h>
using namespace std;

int main ()
{
    int n;
    float result;
start:
    cout<<"Enter any no:";
    cin>>n;
    if(n<=0)
    {
        goto start;
    }
    else
    {    
        result =n*n;
        cout<<"Square is :"<<result;
   }
    return 0;
}
Output:-
Enter any no:-1
Enter any no:0
Enter any no:2
Square is :4
In the above program, whenever the number is less or equal to zero, it will jump to start statement and ask to enter the number again when the number is greater than zero, it shows the result. 

NOTE: Avoid use of GoTo Statement because it makes the program logic very complex and difficult to modify.

you can use continue and break statement instead of GoTO statement.

Whether you use GoTO statement or not....
If the use of GoTo statement makes your program simpler than you should use it else avoid it.

KEEP CODING....





Friday 20 October 2017

Do While Loop

DO... WHILE LOOP      

Do... while is another type of looping.It is quite different from the for loop but similar to the while loop with one important difference. In do... while loop initialization at the top of the loop, update condition inside the loop and test condition at bottom of the loop.The body of do..while loop is executed once, Before checking the test expression. Hence the do...while loop is executed at least once.

syntax:-
do
{
   // codes
}
while (testExpression);
program to add numbers until the user enters zero:-
#include <iostream>
using namespace std;
int main()
{    

    double number, sum = 0;
    
    do
    {
        cout<<"Enter a number: ";
        cin>>number;
        sum += number;
    }
    while(number != 0.0);

    cout<<"Sum: "<<sum<<endl;

    return 0;
}
Output:-
Enter a number: 5
Enter a number: -5
Enter a number: 21
Enter a number: 0
Sum: 21
Now take a look how do...while loop works?

The code inside the body will be executed once.
Then the test condition is evaluated.If the condition is true then the loop body will be executed again until the test condition becomes false.
when the test condition becomes false, the do...while loop will be terminated.

INFINITE DO... WHILE LOOP:-

#include<iostream>
using namespace std;
int main(){
 
 do{ 
 
  cout<<"hello world"<<endl;
  
  
 }while(1);
 
 return 0
}
Just provide 1 instead of the test condition, the loop will run infinite time.
NOTE:- press CTRL+C to terminate the loop.

NESTED DO...WHILE LOOP:-

#include<iostream>
using namespace std;
int main(){
 int i=0;
 do{
  
  int j=0;
  do{
   
   cout<<j;
   
   j++;
   
  }while(j<=5);
  i++;
  
  cout<<"\n";
  
 }while(i<=5);
 
 return 0;
}
Output:-
012345
012345
012345
012345
012345
012345
In nested do...while another do...while is present inside the body of the loop.It is similar to nested for and while loop.

Let take an example how do...while loop is different from other loops.

#include<iostream>
using namespace std;

int main(){
 
 int i=6;
 do{ 
 
 cout<<i<<endl;
 
 i++; 
  
 }while(i<=5);
 
 return 0;
}
 will this program work? yes,  it will work and print 6 on the screen because in do...while the condition is checked at the end of the program. 
we use do...while loop in game programming because when the game ends, you need to show the scoreboard to the player. we use do...while loop so at least once to render the scoreboard.

HAPPY CODING...




                                            

Saturday 14 October 2017

While Loop

WHILE LOOP

while studying for loop we have seen that all the three parts of the loop are present in a line but in while loop, there is a difference in the syntax.In while loop, we initialize the loop outside the loop then test condition where the loop is defined and update condition in the body of the loop.

syntax:-
initialization expression;
while (test_expression)
{
   // statements
 
  update_expression;
}
program to print even number between 0 to 10:-

#include<iostream>
using namespace std;
int main(){
 int i=0; //initialization 
 while(i<=10)   //test condition
 {
  
  cout<<i<<endl;
  i=i+2;     //update condition
  
 }
 return 0;
}
output:-
0
2
4
6
8
10
In the above program, we declare and initialize i=0, then it will compare the value of i with the test condition, if it is true then the body will execute and give increment to the i and the condition run itself again and again. when i is greater then test condition, the condition became false and the loop will terminate. 

INFINITE WHILE LOOP:-
In this loop, there is no initialization, test condition, and update condition just provide 1 instead of the test condition.

syntax:-
#include<iostream>
using namespace std;
int main(){
 
 while(1)  
 {
 
  cout<<"hello world"<<endl;
 
  
 }
 return 0;
}
Note: you will use CTRL+C to terminate the loop.

NESTED WHILE LOOP:-
In nested while loop, a while loop is present inside the body of the while loop.

syntax:-
 initialization;
 while(test condition)  
 {
  initialization;
      while(test condition)
      {
       //statements
       update condition;
          }
   update condition;
       
 }
HOW TO USE WHILE LOOP IN COMPETITIVE PROGRAMMING:-

program:-
#include<iostream>
using namespace std;
int main()
{
 int t;
 cin>>t;
 int n;
 while(t--)
 {
  cin>>n;
  if(n<10)
   cout<<endl<<"what an obedient servant you are!";
  else
   cout<<endl<<"-1";
 }
 return 0; 
}
Now understand the program, you want to run the program for different test cases, you will use while loop in the above manner.

KEEP CLAM AND CODE C++.....





Tuesday 10 October 2017

How to Install Codeblocks IDE on windows 7 ,8 and 10?

How to install and use codeblocks IDE on windows 7,8 and 10?
hello Folks,
In this post, I will share the steps to download and install codeblocks on windows OS and also share how to use it and compile the program on it.

step by step procedure to install codeblocks

1. As we know codeblocks is a free, open source, cross-platform IDE. To download codeblocks go to  www.codeblocks.org/.



2. Click on downloads and different ways to download will open.

3. Click on Download the binary release. You can download other ways too but for learning C++ binary release is best. 

4.  Different types of files will open then go to codeblocks-16.01mingw-setup.exe file and click on sourceforge.net.your setup will start downloading, the file is about 98Mb.


5. Go to the disk where you download the file.

6.  Double click on the setup file. The system will ask for running permission.click on yes.

7. A setup window will appear. Now click on the NEXT ->I AGREE -> NEXT -> INSTALL.






8. Now it will ask for the permission to run the codeblocks.Click on YES.

9. Now compiler auto detection window will appear.click on OK.

10. Codeblocks will open. Select the environment " yes, associate  code::blocks with c/c++ file types".Your codeblocks will be installed on the system.


How to use it.

1. Click on the codeblocks icon the desktop. it will open.



2. Go to top left corner of the IDE. Click on the FILE  -> NEW -> PROJECT.

3. Now select the application type.In this post, we are making console application so select console application.


4. Console application will open. Select NEXT.


5. Select the language you want to use.For this post, we use C++.


6. Write project name then click on finish.



7. Go to the left side. click on the source. A default main function will open.


Now you can use codeblocks for making C/C++ program.

HAPPING CODING

Friday 29 September 2017

Types Of For Loop

In the previous post, we understand how for loop works and three parts of for loop.In this post, I will discuss the types of for loop or in which way you will use for loop.
For loops are mainly two types:

  • Infinite for loop
  • Nested for loop
THE INFINITE FOR LOOP:-
A loop becomes infinite loop if the condition never becomes false. For loop required three parts to run initialization, test expression, and update. if you leave the conditions empty that will make the endless loop.

Syntax:-
#include <iostream>
using namespace std;
 
int main () {
   for( ; ; ) {
      cout<<"This loop will run forever.\n";
   }

   return 0;
}
when the conditional expression is absent, it is assumed to be true. you may use initialization and update expression but most of the
programmers prefer "for(; ;)" to construct an infinite for loop.

NOTE:- you will use CTRL+C to terminate the infinite for loop.
use printf("This loop will run forever.\n"); instead of cout<<"This loop will run forever.\n"; do it yourself and see what happened.

NESTED FOR LOOP:-
The placing of one loop inside the body of another loop is called nesting.In nested for loop, a for loop is present inside the another for loop. 

Syntax:-
for ( initialization; test condition; increment ) {

   for ( initialization; tset condition; increment ) //using another variable{
      statement(s);
   }
 
   statement(s);
}

program to print number in the right triangular pattern:-

#include <iostream>
using namespace std;
int main()
{
    int i,j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
            cout<<j;
            cout<<"\n"; 
    }
    return 0;
}
Output:-
1
12
123
1234
12345

now take a look how nested loop works? the outer for loop start working,i=1 then it will compare to test condition 1<=5 if the condition is true then it will go to inner for loop and give an increment. In inner for loop j=1, and test condition is 1, i is 1 and j<=i now it will run once and exist from inner for loop and go to outer for loop.Next time i become 2 outer for loop will run once but inner loop will run twice.The process will repeat again and again whenever test condition does not become false.
    
    happy coding....keep learning.