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,...

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...

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...

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...

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...

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...

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...

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...

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 ...

Tuesday, 10 October 2017

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,...