Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label Modulus Operator. Show all posts
Showing posts with label Modulus Operator. Show all posts

Friday, December 19, 2014

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 for loop in C.


Place value of number using for loop


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

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

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

void main()
{
int num,num1,rem,pval,rev,len,l;
clrscr();

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 & reverse */ 
for(len=0;num1>=1;len++)
{
rem=num1%10;
num1=num1/10;
rev=rev*10+rem;
}


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

}

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


Place value of number by while loop



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

/*Program to find place value of all digits of any number given by user using 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 & reverse */ 
while(num1>=1)
{
rem=num1%10;
num1=num1/10;
rev=rev*10+rem;
len++;
}


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

}

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 while loop in C.


Sum of digits


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

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

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

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

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

num1=num; //to save actual number

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

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

getch();

}


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


Sum of all digits


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

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

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

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

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

num1=num; //to save actual number
for(i=1;num1>=1;i++)
{
rem=num1%10;
num1=num1/10;
sum=sum+rem;
}

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

getch();

}


Wednesday, December 10, 2014

Program to convert hours into days and print numbers of days and remaining hours in C.

Hours to day converter in C


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

/*Program to enter hours and print numbers of days and remaining hours */

#include<stdio.h>
#include<conio.h>
void main()
{

int hours,rhours;
/*hours for hours enter by User and rhours for remaining hours */

int days;
/*For numbers of Day */

clrscr();

printf("\n\tHours to Day Converter");
printf("\n\tEnter Hours :");
scanf("%d",&hours);


rhours=hours%24;

/*Modulus operator provide us the remainder after Division which is remaining hours */


days=hours/24;
/*Division operator to calculate Days as we know 24 hours means 1 Day */


printf("\n\tNumber of Days : %d",days);
printf("\n\tRemaining Hours: %d",rhours);


getch();
}

Tuesday, December 9, 2014

Solving Fizz -Buzz problem program in C with Conditional Operator.


Fizz-Buzz program in C


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

/*Program to print fizz on entered value is multiple of 3 and buzz on entered value is multiple of 5 using if else */

/*Things to remember :
1.print fizz on multiple of 3
2.print buzz on multiple of 5

so 
3.if entered number is multiple of both than it should print "fizz-buzz" */

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();

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


printf("\nEnterd number is :%d",num);

//By Conditional operator

(num%3==0 && num%5==0)?printf("\nfizz-buzz"): (num%3==0?printf("\nfizz") : (num%5==0 ? printf("\nbuzz") : printf("\n")));


getch();
}




Sunday, December 7, 2014

Program to calculate sum of odd-even number series using while loop in C.


even-odd series sum


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

/*program to calculate sum of the series
1-2+3-4+5-6+7-8+9.......+n
odd-even number series(alternative)
where n is the limit entered by user.
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,evensum,oddsum,sum,num;

clrscr();

evensum=0;
oddsum=0;

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

printf("\nOdd Number Series ");

printf("\nSeries= ");

i=1;                     //initialization

while(i<=num)
{

if(i%2==0)          // % modulus operator
{
printf(" - ");
evensum=evensum+i;
}
else
{
printf(" + ");
oddsum=oddsum+i;
}
printf("%d",i);

i++;                 //increment post fix operator

}

sum=oddsum-evensum;

printf("\n\nSum of series= %d",sum);

getch();
}


Program to calculate sum of odd number series using while loop in C.


Sum of odd series using while loop


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

/*program to calculate sum of the series 
1+3+5+7+9.......+n
odd number series
where n is the limit entered by user.
*/

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

clrscr();

sum=0;

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

printf("\nOdd Number Series ");

printf("\nSeries= ");

i=1;                      //initialization 
while(i<=num)      //test condition 
{

if(i%2!=0)            //% modulus operator  
{
printf(" + %d ",i);
sum=sum+i;
}
i++;                   //increment post fix operator
}
printf("\n\nSum of series= %d",sum);

getch();
}



Program to calculate sum of even series using while loop in C.


sum of even series using while loop


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

/*program to calculate sum of the series
2+4+6+8.......+n
even number series
where n is the limit entered by user.
*/

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

clrscr();

sum=0;

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

printf("\nEven Number Series ");

printf("\nSeries= ");

i=1;
while(i<=num)
{

if(i%2==0)      //modulus operator
{
printf(" + %d ",i);
sum=sum+i;
}
i++;                //increment operator
}
printf("\n\nSum of series= %d",sum);

getch();
}



Program to calculate sum of the even-odd series using for loop in C.


even-odd series sum


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

/*program to calculate sum of the series 
1-2+3-4+5-6+7-8+9.......+n
odd-even number series(alternative) 
where n is the limit entered by user.
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,evensum,oddsum,sum,num;

clrscr();

evensum=0;
oddsum=0;

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

printf("\nOdd Number Series ");

printf("\nSeries= ");
for(i=1;i<=num;i++)
{

if(i%2==0)
{
printf(" - ");
evensum=evensum+i;
}
else
{
printf(" + ");
oddsum=oddsum+i;
}
printf("%d",i);
}

sum=oddsum-evensum;

printf("\n\nSum of series= %d",sum);

getch();
}



Program to calculate sum of the odd number series using for loop in C.


sum of odd number series


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

/*program to calculate sum of the series 
1+3+5+7+9.......+n
odd number series
where n is the limit entered by user.
*/

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

clrscr();

sum=0;

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

printf("\nOdd Number Series ");

printf("\nSeries= ");
for(i=1;i<=num;i++)
{

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

}
printf("\n\nSum of series= %d",sum);

getch();
}

Program to calculate sum of the even series using for loop in C.


sum of even series


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

/*program to calculate sum of the series 
2+4+6+8.......+n
even number series
where n is the limit entered by user.
*/

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

clrscr();

sum=0;

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

printf("\nEven Number Series ");

printf("\nSeries= ");
for(i=1;i<=num;i++)
{

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

}
printf("\n\nSum of series= %d",sum);

getch();
}

Friday, December 5, 2014

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();
}



Sunday, November 30, 2014

Program to enter hours and print numbers of days and remaining hours and convert remaining hours into minutes in C.




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

/*Program to enter hours and print numbers of days and remaining hours and convert remaining hours into minutes */

#include<stdio.h>
#include<conio.h>
void main()
{

float hours,rhours,min;
/*hours for hours enter by User and rhours for remaining hours */

int days,h;
/*For numbers of Day */

clrscr();

printf("\n\tHours to Day Converter");
printf("\n\tEnter Hours :");
scanf("%f",&hours);

h=hours;

printf("\n\tHours:%d",h);
min=hours-h;
printf("\n\tRemaining Hours:%.2f",min);

min=(min*60);
printf("\n\tMinutes:%.2f",min);


rhours=h%24;

/*Modulus operator provide us the remainder after Division which is remaining hours */

days=(int)hours/24;
/*Division operator to calculate Days as we know 24 hours means 1 Day */

printf("\n\tNumber of Days : %d",days);
printf("\n\tRemaining Hours: %f",rhours);

getch();
}

Saturday, November 29, 2014

Program to enter hours and print numbers of days and remaining hours in C




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

/*Program to enter hours and print numbers of days and remaining hours */

#include<stdio.h>
#include<conio.h>
void main()
{

int hours,rhours;
/*hours for hours enter by User and rhours for remaining hours */

int days;
/*For numbers of Day */

clrscr();

printf("\n\tHours to Day Converter");
printf("\n\tEnter Hours :");
scanf("%d",&hours);

rhours=hours%24;
/*Modulus operator provide us the remainder after Division which is remaining hours */

days=hours/24;
/*Division operator to calculate Days as we know 24 hours means 1 Day */

printf("\n\tNumber of Days : %d",days);
printf("\n\tRemaining Hours: %d",rhours);


getch();
}