Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label do while loop. Show all posts
Showing posts with label do while loop. Show all posts

Saturday, December 20, 2014

Program to find given number is palindrome number or not by do while loop in C.


Palindrome number checker in C


Code Box
---------------------------------------------

/*Program to find given number is palindrome number or not by do while loop in C*/

#include<stdio.h>
#include<conio.h>

void main()
{
int num,num1,rem,newnum;
clrscr();

newnum=0;
printf("\n\tPalindrome Number Checker ");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number
do
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem;
}while(num1>=1);

if(num==newnum)
{
printf("\nNumber %d is palindrome number ",num);
}
else
{
printf("\nNumber %d is not a palindrome number ",num);
}

getch();

}

Friday, December 19, 2014

Program to find reverse of a number by do while loop in C.


Reverse number by While loop


Code Box
---------------------------------------------

/*Program to find reverse of a number by do while loop in C*/

#include<stdio.h>
#include<conio.h>

void main()
{
int num,num1,rem,newnum;
clrscr();

newnum=0;
printf("\n\tReverse Number maker");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number

printf("\nBy Method 1:");
while(num1>=1)
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem;
}

printf("\nReverse of number is : %d",newnum);
printf("\n-------------------------");

printf("\nBy Method 2:");
while(num1!=0)
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem; //formula to reverse
}

printf("\nReverse of number is : %d",newnum);

getch();

}



Program to find reverse of a number by do while loop in C.



Reverse of number by do while in C


Code Box
------------------------------------------

/*Program to find reverse of a number by do while loop in C*/

#include<stdio.h>
#include<conio.h>

void main()
{
int num,num1,rem,newnum;
clrscr();

newnum=0;
printf("\n\tReverse Number maker");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number
do
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem;
}while(num1>=1);

printf("\nReverse of number is : %d",newnum);
getch();
} 


----Explanation------------------------------------------------------
By reverse of any number means just reversing the sequence of digits of given number.

For example: 1234 is a given number than it's reverse is 4321.

In above program we use do while loop to find reverse of number.


  • we use num1=num to protect value of num in our program because while calculating reverse we divide it til it becomes 1.
  • rem=num1%10; gives us remainder each time means the last digit because of modulus operator.
  • num1=num1/10; reduce the value of num1 by 10 each time. means removes last single digit every time.
  • newnum=newnum*10+rem; creates the reverse value by multiplying 10 to previous remainder.

On initial we take num1=1234 as input and newnum=0.
at do while loop :

first time

rem=1234%10 =4
num1=1234/10= 123
newnum= 0*10+4 = 4
test condition: num1>=1 => true as 123>=1

--------------------------

second time

rem=123%10 =3
num1=123/10= 12
newnum= 4*10+3 = 43


test condition: num1>=1 => true as 12>=1

-------------------------

Third time

rem=12%10 =2
num1=12/10= 1
newnum= 43*10+2 = 432


test condition: num1>=1 => true as 1>=1

-------------------------

Forth time

rem=1%10 =1
num1=1/10= 0

newnum= 432*10+1 = 4321



test condition: num1>=1 => false as 0>=1

So, we get the required reversed value. 

Code Box C is a blog specially for all who want to learn C language or any other programming language. I just try to get you the basic concept behind the given problem because the concept to solve any given problem in all languages is always same. It will help you to speedup or boost your learning speed as well as your strategy building skills if you know the actual concept. 

Thursday, December 18, 2014

Program to find place value of all digits of any number given by user using do while loop in C.


Place value of number


Code Box
-------------------------------------------------

/*Program to find place value of all digits of any number given by user using do while loop in C. */

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int num,num1,rem,pval,rev,len;
clrscr();
len=0;
rev=0;
printf("\n\tSum of Digits Finder");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number

/*Finding the length of number & Number reverse */ 
do
{
rem=num1%10;
num1=num1/10;
rev=rev*10+rem;
len++;
}while(num1>=1);



/*Calculation the place value */
len=len-1;
do
{
rem=rev%10;
rev=rev/10;
pval=rem*pow(10,len);
printf("\nPlace value of %d is = %d",rem,pval);
len--;
}while(rev>=1);

getch();

}

Wednesday, December 17, 2014

Program to find sum of digits of any number given by user using do while loop in C.


Sum of all digits


Code Box
------------------------------------------------

/*Program to find sum of digits of any number given by user max. 5 digit number */

#include<stdio.h>
#include<conio.h>

void main()
{
int num,num1,rem,sum;
clrscr();

sum=0;
printf("\n\tSum of Digits Finder");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num; //to save actual number

do
{
rem=num1%10;
num1=num1/10;
sum=sum+rem;
}while(num1>=1);


printf("\nSum of all digits = %d",sum);

getch();

}



Tuesday, December 16, 2014

Program to print number pattern using do while loop in C.(Pattern 10)

Number Pattern 10 codeboxc


Code Box 
-------------------------------------------

/*program to print number pattern by do while loop

            1
          21
        321
      4321
    54321

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
i=1;
clrscr();

printf("\nEnter a number: ");
scanf("%d",&n);

do
{j=n;
 k=i;
        do
        {
        printf(" ");
        j--;
}
while(j>=i);

        do
        {
        printf("%d",k);
        k--;
}
while(k>=1);

printf("\n");
i++;
}
while(i<=n);

getch();

}

Program to print number pattern using do while loop in C.(Pattern 9)

Code Box
----------------------------------------------

/*program to print number pattern  by do while loop

            1
          12
        123
      1234
    12345

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
i=1;
clrscr();

printf("\nEnter a number: ");
scanf("%d",&n);

do
{j=n;
k=1;
        do
        {
        printf(" ");
        j--;
}
while(j>=i);

        do
        {

        printf("%d",k);
k++;
        }
while(k<=i);
printf("\n");
i++;
}
while(i<=n);
getch();
}



Monday, December 15, 2014

Program to print number pattern using do while loop in C.(Pattern 8)

Code Box
------------------------------------------

/*program to print number pattern by do while loop

  5
  54
  543
  5432
  54321

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();

printf("\nEnter a value: ");
scanf("%d",&n);

i=n;
do
{j=n;
        do
        {
printf("%d",j);
        j--;
}
while(j>=i);
printf("\n");
i--;
}
while(i>=1); 

getch();

}

Program to print number pattern using do while loop in C.(Pattern 7)


Number Pattern 7 codeboxc


Code Box 
---------------------------------------------

/*program to print number pattern by do while loop

  1
  21
  321
  4321
  54321

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();

printf("\nEnter a value: ");
scanf("%d",&n);

i=1;

do
{j=i;
        do
        {
printf("%d",j);
        j--;
}
while(j>=1);
printf("\n");
i++;
}
while(i<=n);

getch();

}

Program to print number pattern using do while loop in C.(Pattern 6)


Number Pattern 6 codeboxc


Code Box
--------------------------------------------

/*program to print star pattern square box by do while loop

  55555

  44444
  33333
  22222
  11111
  
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;

clrscr();

printf("\nEnter a value: ");
scanf("%d",&n);

i=n;

do
{j=1;
        do
        {
        printf("%d",i);
        j++;
}
while(j<=n);
printf("\n");
i--;
}
while(i>=1);

getch();

}


Program to print number pattern using do while loop in C.(Pattern 5)


Number Pattern 5


Code Box
---------------------------------------------

/*program to print number pattern square box by do while loop

  54321
  54321
  54321
  54321
  54321
  
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();

printf("\nEnter a value: ");
scanf("%d",&n);

i=1;

do
{j=n;
        do
        {
        printf("%d",j);
        j--;
}
while(j>=1);

printf("\n");
i++;
}
while(i<=n);

getch();
}

Sunday, December 14, 2014

Program to print number pattern using do while loop in C.(Pattern 4)


Code Box
----------------------------------------

/*program to print star pattern square box by do while loop

  11111
  22222
  33333
  44444
  55555

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
i=1;
clrscr();

printf("\nEnter value of n :");
scanf("%d",&n);

do
{j=1;
        do
        {
        printf("%d",i);
        j++;
}
while(j<=n);
printf("\n");
i++;
}
while(i<=n);

getch();

}


Thursday, December 11, 2014

Program to print number pattern square box by do while loop in C. (Pattern 2)


Number Pattern 2


Code Box
---------------------------------------

/*program to print number pattern square box by do while loop

  1
  12
  123
  1234
  12345

*/ 155223 

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
i=1;
clrscr();

printf("\nEnter a number: ");
scanf("%d",&n);

do
{j=1;
        do
        {
        printf("%d",j);
        j++;
}
while(j<=i);

printf("\n");
i++;
}
while(i<=n);

getch();
}



Program to print number pattern square box by do while loop in C.




Code Box
-----------------------------------------

/*program to print Number pattern square box by do while loop

  12345
  12345
  12345
  12345
  12345

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
i=1;
clrscr();

printf("\nEnter a number: ");
scanf("%d",&n);

do
{j=1;
        do
{
        printf("%d",j);
        j++;
}
while(j<=n);
        
printf("\n");
i++;
}
while(i<=n);

getch();
}



Friday, December 5, 2014

Program to print factorial of any input number with fact as integer using do while loop in C.



factorial with fact as int


Code Box

------------------------------------------------------------

/*Program to print factorial of any input number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact;
/* If we take fact as integer, it can not be able
to store factorial of value greater than 8 */
fact=1;
clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\n(When Fact as integer)\n");
printf("\nFactorial of %d is:",num);
printf("\n-----------------------------\n");
do
{
if(num==0)
{
printf("\n1"); //factorial of 0 is 1
break;
}
fact=fact*num;
num--;
}while(num>=1);

printf("\n%d",fact);

printf("\n------------------------------");

getch();
}


Program to print factorial of any input number using do while loop in C.


factorial of number


Code Box
---------------------------------------------------

/*Program to print factorial of any input number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
float fact;
/* If we take fact as integer, it can not be able
to store factorial of value greater than 9 */
fact=1.0;
clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nFactorial of %d is:",num);
printf("\n-----------------------------\n");

do
{
if(num==0)
{
printf("\n1"); //factorial of 0 is 1
break;
}
fact=fact*num;
num--;
}while(num>=1);

printf("\n%.0f",fact);

printf("\n------------------------------");

getch();
}



Program to find input number is prime or not using do while loop in C.


prime number


Code Box
-----------------------------------------------------

/*Program to find input number is prime or not*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,c;
c=0; //counter
clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");

i=2;
/*start from 2 because every number is divisible by 1 */

/*Prime finder */
do
{
if(num%i==0)
{
c=1;
}
i++;
}while(i<num) ;

/*Prime checker */
if(c==0)
{
printf("\n%d is prime",num);
}
else{
printf("\n%d is not a prime",num);
}

printf("\n------------------------------");

getch();
}



Program to check input number is perfect number or not using do while loop in C.


perfect number


Code Box
-------------------------------------------

/*Program to check input number is perfect number or not*/

/*Perfect number: sum of all it's factors is equal to number itself */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,sum;
clrscr();
sum=0;
i=1;

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nFactores of %d is:",num);
printf("\n-----------------------------\n");

do
{

if(num%i==0)
{
printf(" %d ",i);
sum=sum+i;
}

i++;
}while(i<num);

printf("\nSum of factors is: %d",sum);

if(num==sum)
{
printf("\n%d is a perfect number",num);
}
else
{
printf("\n%d is not a perfect number",num);
}

printf("\n------------------------------");

getch();
}



Thursday, December 4, 2014

Program to print table of any input number along with sum of all values using do while loop in C.



table sum



Code Box
---------------------------------------------
/*Program to print table of any input number along with sum of all values */

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,sum,mul;
sum=0;
i=1;
clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nTable of %d is",num);
printf("\n---------------------------------------\n");

do
{
if(num==0)
{
printf("\nZero entered");
break;
}
mul=num*i;
printf("| %d ",mul);
sum=sum+mul;
i++;
}while(i<=10);

printf("\n--------------------------------------");

printf("\nSum of table is : %d",sum);

getch();
} 

Program to print table of any input number using do while loop in C.




Code Box
---------------------------------------------
/*Program to print table of any input number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,mul;
i=1;
clrscr();

printf("\nEnter any number:");
scanf("%d",&num);

clrscr();  //for clear screen

printf("\n-------------------------------");
printf("\nNumber entered is: %d",num);
printf("\n-------------------------------");
printf("\nTable of %d is",num);
printf("\n---------------------------------------\n");

do
{
if(num==0)
{
printf("\nZero entered");
break;
}
mul=num*i;
printf("\n %d x %d = %d ",num,i,mul);

i++;
}while(i<=10);

printf("\n--------------------------------------");

getch();
}