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

Saturday, December 20, 2014

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





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

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

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

void main()
{
int num,num1,len,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
for(len=0;num1>=1;len++)
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem; //Reverse formula
}

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

getch();

}



--EXPLANATION----------------------------------

To find given number is palindrome or not we need following things to do
  • Create a number reverse formula
  • Find reverse of the given number 
  • Check given number and reverse number are same or not.

this is the simplest way to find that the given number is a palindrome or not.

But this is the human way not for computer. So, you need to convert this strategies into codes.

For doing this we simply follow:
  • Problem understanding done!! as we know the concept of finding palindrome number.
  • Now we need to find variables possible.
  • Logic Building means what are the things we need Like a loop to find reverse, we need to keep given number safe so that we can use it again at end and if else for checking at end. 
 


Now we have every thing concept, variables and logic.
Start writing codes...


  • Take input a number from user
  • reverse it using reverse formula
  • use if else to check input number is equal to reverse number or not
  • print the results.

If you have any more query or any problem feel free to comment below at codeboxc. we will try to reach you soon with answer.... Learn.Explore.Share!!

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



Palindrome number checker in C


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

/*Program to find given number is palindrome number or not by 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


while(num1>=1)
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem; //Reverse formula
}


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

getch();

}



--EXPLANATION----------------------------------

To find given number is palindrome or not we need following things to do
  • Create a number reverse formula
  • Find reverse of the given number 
  • Check given number and reverse number are same or not.

this is the simplest way to find that the given number is a palindrome or not.

But this is the human way not for computer. So, you need to convert this strategies into codes.

For doing this we simply follow:
  • Problem understanding done!! as we know the concept of finding palindrome number.
  • Now we need to find variables possible.
  • Logic Building means what are the things we need Like a loop to find reverse, we need to keep given number safe so that we can use it again at end and if else for checking at end. 
 


Now we have every thing concept, variables and logic.
Start writing codes...


  • Take input a number from user
  • reverse it using reverse formula
  • use if else to check input number is equal to reverse number or not
  • print the results.

Here we are using while loop to find reverse of the given number.





If you have any more query or any problem feel free to comment below at codeboxc. we will try to reach you soon with answer.... Learn.Explore.Share!!


Friday, December 19, 2014

Program to find any number inside another number in C.



Number Finder in C


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

/*Program to find given numbers presence in another number in C

or 

Program to find any number inside another number in C.
 */

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

void main()
{
int num,num1,nf,rem,newnum,len,i,c;
clrscr();

len=0;
newnum=0;
c=0; //Counter Variable

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

/*Single Digit number */
printf("\nEnter number to find:");
scanf("%d",&nf);


num1=num; //to save actual number


/*Number Reverse */
while(num1>=1)
{
rem=num1%10;
num1=num1/10;
newnum=newnum*10+rem; 
len++;
}


for(i=1;i<=len;i++)
{
rem=newnum%10;
newnum=newnum/10;

if(rem==nf)
{
printf("\nNumber %d is present in %d at position %d.",nf,num,i);
}
else
{
c++; //Counter Variable
}
}


if(c==len)  //Checking for absence 
{
printf("\nNumber Not Found!!!");
}

getch();

}

Tuesday, December 16, 2014

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

}


Wednesday, December 10, 2014

Write a program to enter marks of a student in five subjects and find grade in C.


Student details


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

/*program to enter marks of a student in five subjects and find grade as

=> average>90   A+
=> average<90 and average<75 A
=> average<75 and average >60 B
=> average<60 and average >50 C
=> average<50 D

*/

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5; // marks in five subjects
float avg; 
clrscr();


/* Accepting marks of five subjects */
printf("\nEnter marks(max. 100):");
printf("\nEnglish:");
scanf("%d",&m1);

printf("\nMaths:");
scanf("%d",&m2);

printf("\nPhysics:");
scanf("%d",&m3);

printf("\nChemistry:");
scanf("%d",&m4);

printf("\nBiology:");
scanf("%d",&m5);

/*Calculation of average */

avg=(m1+m2+m3+m4+m5)/5;

/*Percentage */
printf("\nPercentage: %.3f%",avg);

/*Selection of grade */

if(avg>=90)
{
printf("\nGrade : A+");
}
else if(avg<90 && avg>=75)
{
printf("\nGrade : A");
}
else if(avg<75 && avg>=60)
{
printf("\nGrade : B");
}else if(avg<60 && avg>=50)
{
printf("\nGrade : C");
}else if(avg<50)
{
printf("\nGrade : D");
}

getch();
}


Sunday, December 7, 2014

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



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 with special case 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-------------------------------");


if(num==0)
{
printf("\nZero entered!!"); //Zero case
}
else
{
/*Prime finder */
for(i=2;i<=(num/2);i++)
{ /*start from 2 because every number is divisible by 1 */
/* i<=(num/2) because any number can be completely divisible by only half or less than itself.*/
if(num%i==0)
{
c=1;
}
}

/*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 find input number is prime or not using for loop in C.


Prime number using for loop


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


/*Prime finder */
for(i=2;i<num;i++)
{ /*start from 2 because every number is divisible by 1 */
if(num%i==0)
{
c=1;
}
}

/*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 find input number is prime or not using 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 */
while(i<num)
{
if(num%i==0)
{
c=1;
}
i++;
}

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


for loop


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;

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

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

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

}

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

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



Program to check input number is perfect number or not using 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");

while(i<num)
{

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

i++;
}

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




Sunday, November 30, 2014

Program to Solve Quadratic Equation in C.





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


/* Program to Solve Quadratic Equation */

/* In quadratic equation we need values of a,b,c
and D=(b square -4.a.c)
and every thing depends on the value of D
*If D grater than 0 multiple and real root
*If D==0 only single root
*If D is less than 0 than imaginary roots

from codeboxc.blogspot.com

*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,D;

float x1,x2,D1;

clrscr();

printf("\nEnter Values");
printf("\n a = ");
scanf("%d",&a);

printf("\n b = ");
scanf("%d",&b);

printf("\n c = ");
scanf("%d",&c);

printf("\nEnterd Values are : a=%d b=%d c=%d",a,b,c);

printf("\nQuadratic Equation Calculation:");

D=((b*b)-(4*a*c));

printf("\nValue of Normal D= %d ",D);
D1=abs(D);
D1=sqrt(D1);

printf("\nValue of Squared D= %.3f ",D1);

if(D>0)
{ //Multiple Roots
printf("\nReal & Multiple Roots");


x1=((b+D1)/(2*a));

x2=((b-D1)/(2*a));

printf("\n\nRoots are X1=%.3f X2=%.3f ",x1,x2);

}
else if(D==0)
{ //Single Root
printf("\nReal & Single Roots");

x1=((b)/(2*a));

printf("\n\nRoots are X1=%.3f",x1);

}
else
{ //Imaginary Part

printf("\nImaginary Roots");

printf("\nX1=(%d+%.3fi)/2*%d",b,D1,a);

printf("\nX2=(%d-%.3fi)/2*%d",b,D1,a);

}

getch();
}



Saturday, November 8, 2014

Program to print all prime numbers between a given range of numbers in C.

Prime Number


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

/* Program to print all prime numbers between a given range of numbers */

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

printf("\nEnter Start of range:");
scanf("%d",&start);

printf("\nEnter End of range :");
scanf("%d",&end);

printf("\n\nPrime Numbers in range :");
for(i=start;i<=end;i++)
{c=0;
for(j=2;j<start;j++)
{
if(i%j==0)
{
c=1;
}
}
if(c==0)
{
printf(" %d",i);
}
}

getch();
}


Program to take 3 positive integers as input and verify whether they from a pythagorean triplet or not in C.



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

/*Program to take 3 positive integers as input and verify whether they
from a pythagorean triplet or not */

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

void main()
{

int a,b,c,gre,num,num1,num2,sum,sqr;
clrscr();

printf("\nEnter three +ve numbers:");

printf("\nEnter first number:");
scanf("%d",&a);
printf("\nEnter second number:");
scanf("%d",&b);
printf("\nEnter third number:");
scanf("%d",&c);

num=a*a;
num1=b*b;
num2=c*c;


if(a>b && a>c)
{
sqr=sqrt(num1+num2);
gre=a;

}
else
if(b>c)
{
sqr=sqrt(num2+num);
gre=b;
}
else
{
sqr=sqrt(num+num1);
gre=c;
}

if(sqr==gre)
{
printf("\nPythagorean Triangle possible ");
}
else
{
printf("\nTriangle not possible");
}

getch();
}


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


Code Box 2
---------------------------------------------

/*Program to take 3 positive integers as input and verify whether they
from a pythagorean triplet or not */

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

void main()
{

int a,b,c,gre,num,num1,num2,sum,sqr;
clrscr();

printf("\nEnter three +ve numbers:");

printf("\nEnter first number:");
scanf("%d",&a);
printf("\nEnter second number:");
scanf("%d",&b);
printf("\nEnter third number:");
scanf("%d",&c);


if(a==0 || b==0 ||c==0)
{
printf("\nside zero not possible ");
goto next;
}


num=a*a;
num1=b*b;
num2=c*c;

printf("squares of numbers are:");
printf("\nsquare of a = %d",num);
printf("\nsquare of b = %d",num1);
printf("\nsquare of c = %d",num2);


if(a>b && a>c)
{
sqr=sqrt(num1+num2);
gre=a;

printf("\nGeater is a");
}
else
if(b>c)
{
sqr=sqrt(num2+num);
gre=b;
printf("\nGeater is b");

}
else
{
sqr=sqrt(num+num1);
gre=c;

printf("\nGeater is c");
}

if(sqr==gre)
{
printf("\nPythagorean Triangle possible ");
}
else
{
printf("\nTriangle not possible");
}

next:
getch();
}

Thursday, November 6, 2014

Program to find largest number in list in C.

Largest Number In List.


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

/*Program to find largest number in list
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num[10];
int i,k,large;
clrscr();

printf("\nEnter 10 number:");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
printf("\nDone");
clrscr();
printf("\nYou entered:\n");

for(i=0;i<10;i++)
{
printf("  %d",num[i]);
}
printf("\n\nLargest numbers in list:\n");
large=num[0];
for(i=0;i<10;i++)
{
if(large<=num[i])
{
large=num[i];
}
}
printf(" %d",large);

getch();
}


Program to delete an element from list in C.

List out put.


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

/* Program to delete an element from list
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,l,k,ins,temp,n,del,pos;
int num[50];
clrscr();

printf("\nEnter List Size(max 50):");
scanf("%d",&n);
printf("\nEnter elements :");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}

clrscr();
printf("\nElements in list :");
for(i=0;i<n;i++)
{
printf(" %d",num[i]);
}
printf("\nSelect :\n1.for insert into list\n2.for delete from list :");
scanf("%d",&l);
switch(l)
{
case 1:

printf("\nEnter element :");
scanf("%d",&ins);
printf("\nPosition :");
scanf("%d",&pos);
pos=pos-1;
n=n+1;
for(i=pos;i<n;i++)
{ temp=num[i];
  num[i]=ins;
  ins=temp;
}

break;

case 2:

printf("\nSelect \n1.To enter number for delete\n2.for position of number :");
scanf("%d",&j);
switch(j)
{
case 1:
printf("\nEnter element :");
scanf("%d",&del);
for(i=0;i<n;i++)
{
if(num[i]==del)
{for(k=i+1;k<n;k++)
 {
  num[k-1]=num[k];
 }
}
}
break;

case 2:
printf("\nPosition :");
scanf("%d",&pos);
pos=pos-1;
for(i=pos;i<n;i++)
{
num[i]=num[i+1];
}
break;

default :
printf("\nerror:check entry!!!!");
}
n=n-1;

break;
default :
printf("\nerror:check entry!!!!");

}

printf("\nElements in new list :");
for(i=0;i<n;i++)
{
printf(" %d",num[i]);
}

getch();
}