Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label C. Show all posts
Showing posts with label C. 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 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. 

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

}


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 convert temperature Celsius to Fahrenheit in C.

Celsius to Fahreheit


Code Box
----------------------------------------
/*Program to convert temperature Celsius to Fahrenheit in C */

#include<conio.h>
#include<stdio.h>
void main()
{
float c,f;
clrscr();

printf("\n\tTEMPERATURE CONVERTER \n\t[Celsius to Fahrenheit]");
printf("\n\tEnter temperature in C:");
scanf("%f",&c);

f=((c*9)/5)+32;

printf("\n\tC= %f equals to F= %f",c,f);

getch();
}

Program to check given character is in upper case or lower case and convert into upper or lower case in C.

Lower Character to Upper Character codeboxc


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

/* program to Check in put character is Lower or upper case and convert them into other case */

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

printf("\nEnter a character :");
scanf("%c",&c);

i=c;

if(i>=97 && i<=122)
{
printf("\nEntered Character is Lower Case : %c",c);
i=i-32;
c=(char)i;  /*Explicit type conversion */
printf("\nUpper case  Character is: %c",c);

}
else if(i>=65 && i<=90)
{
printf("\nEntered Character is Upper Case : %c",c);
i=i+32;
c=(char)i;   /*Explicit type conversion */
printf("\nLower Case  Character is: %c",c);

}
else
{
printf("\nPlease enter a correct case Character");

}

getch();

}


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

Number Pattern 10 codeboxc


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

/*program to print number pattern  by 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);

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

        while(k>=1)
        {

        printf("%d",k);
        k--;
}
printf("\n");
i++;
}
getch();

}

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

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

/*program to print number pattern  by for loop

            1
          21
        321
      4321
    54321

*/

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

clrscr();

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

for(i=1;i<=n;i++)
{
        for(j=n;j>i;j--)
        {
        printf(" ");
        }

        for(k=i;k>=1;k--)
        {

        printf("%d",k);
        }
printf("\n");

}
getch();
}



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

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

/*program to print number pattern  by 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);

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

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

getch();
}

Sunday, December 7, 2014

Program to calculate sum of n numbers using while loop in C.



Sum of series using while loop


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

/*program to calculate sum of the series 
1+2+3+4+.......+n
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("\nSeries= ");

i=1; //initialization

while(i<=num)
{
printf(" + %d ",i);
sum=sum+i;
i++;             //increment 
}
printf("\n\nSum of series= %d",sum);

getch();
}



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


sum of cube series


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

/*program to calculate sum of the series 
(1*1*1)+(2*2*2)+(3*3*3)+(4*4*4)+.......+(n*n*n).
cube number series. 
without using power function.
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("\nCube Number Series ");

printf("\nSeries= ");
for(i=1;i<=num;i++)
{
sum=sum+(i*i*i);
printf("+ (%d*%d*%d)",i,i,i);
}

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

getch();
}

Program to calculate sum of the square series using power function and for loop in C.


square sum using power function


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

/*program to calculate sum of the series 
(1*1)+(2*2)+(3*3)+(4*4)+.......+(n*n).
Square number series. 
by using power function.
where n is the limit entered by user.
*/

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

clrscr();

sum=0;

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

printf("\nSquare Number Series ");

printf("\nSeries= ");
for(i=1;i<=num;i++)
{
sum=sum+pow(i,2);
printf("+ (%d*%d)",i,i);
}

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

getch();
}



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


square series sum


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

/*program to calculate sum of the series 
(1*1)+(2*2)+(3*3)+(4*4)+.......+(n*n).
Square 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("\nSquare Number Series ");

printf("\nSeries= ");
for(i=1;i<=num;i++)
{
sum=sum+(i*i);
printf("+ (%d*%d)",i,i);
}

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

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


program to calculate sum of the series



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

/*program to calculate sum of the series 
1+2+3+4+.......+n
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("\nSeries= ");
for(i=1;i<=num;i++)
{
printf(" + %d ",i);
sum=sum+i;
}
printf("\n\nSum of series= %d",sum);

getch();
}

Program to calculate sum of this special series using for loop in C.


sum of special series

sum of special series formula



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

/*Program to calculate sum of series

 1/(1*2)+1/(2*3)+1/(3*4)+.......+1/((n-1)*n)

 where n is the value entered by user.

 =>Explicit type conversion
 =>Nested loop
*/

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


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


printf("\nSeries=");

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

for(i=1;i<n;i++)
{

mul=i*(i+1);
printf(" +1/(%d*%d) ",i,i+1);


sum=sum+(float)1/mul;  //Explicit type conversion
}

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

printf("\nSum of series= %.3f",sum);

getch();
}

Saturday, December 6, 2014

Program to calculate sum of factorial series using for loop and power function in C.






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

/*Program to calculate sum of series

 1/1!+1/2!+1/3!+.......+1/n!.
 by power function.

 where n is the value entered by user
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,j,mul;
float sum;
clrscr();
sum=0;

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


printf("\nSeries=");

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

for(i=1;i<=n;i++)
{
mul=1;
printf(" +1/%d! ",i);

for(j=1;j<=i;j++)
{
mul=mul*j;
}

sum=sum+pow(mul,-1); //power function

}

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

printf("\nSum of series= %.3f",sum);

getch();
}

Program to calculate sum of series using for loop without power function in C.



Sum of series


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

/*Program to calculate sum of series

 1/(1square)+1/(2 square)+1/(3 square)+.......+1/(n square).

 where n is the value entered by user.

 =>Explicit type conversion

*/

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

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


printf("\nSeries=");

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

for(i=1;i<=n;i++)
{

printf(" +1/(%d*%d) ",i,i);
sum=sum+(float)1/(i*i); //Explicit type conversion

}

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

printf("\nSum of series= %.3f",sum);

getch();
}