Friday, 31 May 2013

    C program to check a number is perfect number or not

A number which is equal to the sum of its divisor is called perfect number.For eg,divisors of 6 are 1,2 and 3.The sum of these divisors is 6.So 6 is called as perfect number.
#include<stdio.h>
#include<conio.h>


//perfect number checking
void main()
{
    int n,d,sum;
    clrscr();
    printf("\n Enter the number : ");
    scanf("%d",&n);
    sum = 0;

    d = 1;
    while ( d<= n/2)
    {
        if ( n % d == 0 )
        {
            sum = sum + d;
        }
        d++;
    }
    if ( sum == n )
        printf("\n %d is perfect number",n);
    else
        printf("\n %d is not perfect number",n);

  and the output is like:
                                    




 

 

Friday, 31 May 2013 by Partha Roy · 0

Wednesday, 26 December 2012

Any base to any base conversion


The Program for any base to any base conversion:

This program converts a given number in any base(2,8,10,16) to any base that user wants.here we use two functions one is  "other_to_decimal" which is for converting a number from any base to decimal,; and other is "decimal_to_other" which is for converting a number from decimal to any base.. 

//Any Base to Any Base...
#include<stdio.h>
#include<conio.h>
long signed int other_to_decimal(long signed int,long signed int);
long signed int decimal_to_other(long signed int,long signed int);
void main()
{
long signed int inputbase,outputbase,inputnumber,outputnumber,s;
clrscr();
printf("Enter the number and its base:");
scanf("%ld%ld",&inputnumber,&inputbase);
printf("Enter the base you want to convert the number to:");
scanf("%ld",&outputbase);
if(outputbase==10)
{
outputnumber=other_to_decimal(inputnumber,inputbase);
}

else
{

if(inputbase==10)
{
outputnumber=decimal_to_other(inputnumber,outputbase);
}
else
{
s=other_to_decimal(inputnumber,inputbase);
outputnumber=decimal_to_other(s,outputbase);
}
}
printf("The converted number is %ld",outputnumber);
getch();
}
long signed int other_to_decimal(long signed int inputnumber,long signed int inputbase)
{
long signed int k,op,s,p;
op=0;
k=inputnumber;
p=1;
while(k!=0)
{
s=k%10;
k=k/10;
op=op+s*p;
p=p*inputbase;
}
return op;
}
long signed int decimal_to_other(long signed int outputnumber,long signed int outputbase)
{
long signed int op=0,p,k,s;
k=outputnumber;
p=1;
while(k!=0)
{
s=k%outputbase;
k=k/outputbase;
op=op+s*p;
p=p*10;
}
return op;
}

and the output is like:


Wednesday, 26 December 2012 by Partha Roy · 4

Any base to Decimal conversion:


Program for Any base to Decimal conversion:

This program shows how to convert a number in any base(it can be 2, 7..)
to decimal number system.The user enters the number and its base and the program converts it to decimal system.
//any base to decimal converting ....

#include<stdio.h>
#include<conio.h>
void main()
{
long signed int inputnumber,inputbase,k,op,s,p;
clrscr();
printf("Enter the number and its base");
scanf("%ld%ld",&inputnumber,&inputbase);
op=0;
k=inputnumber;
p=1;
while(k!=0)
{
s=k%10;
k=k/10;
op=op+s*p;
p=p*inputbase;
}
printf("The number is %ld",op);
getch();
}

and the output is like:

by Partha Roy · 0

Decimal to any number system conversion:


The program for Decimal to any base conversion:

This program converts a decimal number to any base that user wants.. The User at first , enters the the number and the output base and the program converts the decimal number to the wanted base.

//Decimal to any base....

#include<stdio.h>
#include<conio.h>
void main()
{
int outputnumber,outputbase,k,s;
long signed int op=0,p;
clrscr();
printf("Enter the Decimal number and the outputbase ");
scanf("%d%d",&outputnumber,&outputbase);
k=outputnumber;
p=1;
while(k!=0)
{
s=k%outputbase;
k=k/outputbase;
op=op+s*p;
p=p*10;
}
printf("The converted number is %ld",op);
getch();
}

and the output is like::



by Partha Roy · 1

Monday, 24 December 2012

Printing the Pascal Triangle:


Program for the Pascal Triangle:

//Pascal triangle...

#include<stdio.h>
#include<conio.h>
void main()
{
int p[10][10];
int i,j,row,m,n;
clrscr();
printf("Enter the number of rows you want to get printed:");
scanf("%d",&row);
p[0][0]=1;
p[1][0]=1;
p[1][1]=1;
for(i=2;i<row;i++)
{
for(j=0;j<=i;j++)
{
if(j==0)
{
p[i][0]=1;
}
else
{
 if(i==j)
 {
p[i][i]=1;
 }
 else
 {
p[i][j]=p[i-1][j-1]+p[i-1][j];
 }
}
}
}
for(m=0;m<row;m++)
{
for(n=0;n<=m;n++)
{
printf("%d ",p[m][n]);
}
printf("\n");
}
getch();
}

and the output is like:


Monday, 24 December 2012 by Partha Roy · 0

Printing the Fibonacci series:

Program for Printing the Fibonacci series:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,range,i=2;
clrscr();
printf("Enter How many numbers you want to get printed:");
scanf("%d",&range);
a=0;
b=1;
printf("%d,%d",a,b);
while(i!=range)
{
c=a+b;
a=b;
b=c;
i=i+1;
printf(",%d",c);
}
getch();
}

and the output is like:

by Partha Roy · 0

The permutation of a three(3) digit number

The Program for the permutation of a three(3) digit number:


//the permutation of a three(3) digit number...

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,m,len=0,num[3],n;
clrscr;
printf("enter any three digit number:");
scanf("%d",&k);
m=k;
while(m!=0)
{
m=m/10;
len++;
}
if(len==3)
{
num[0]=k/100;
num[1]=(k%100)/10;
num[2]=(k%100)%10;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(l=0;l<3;l++)
{

if(i!=j && j!=l && l!=i)
printf("%d%d%d \n",num[i],num[j],num[l]);
}
}
}
}
else
printf("you have entered a %d digit number.please make sure length is 3",len);
getch();
}

and the output is like:

by Partha Roy · 0

Recent Comments

All Rights Reserved Assignments on C language