Thursday, 10 March 2016

Combination of Number & String, Perfect Number, Simple programs

Write a program to find All the combination of 3 char string

/* this program finds all the combinations that can be obtained from a 3 character string*/
#include<stdio.h>

int main(void){
int i,j;
char str[3];
printf("enter 3 char string\n");
scanf("%s",str);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(i!=j)
printf("%c%c%c\n",str[i],str[j],str[3-i-j]);
}
}

}

Output:
enter 3 char string : abc
abc
acb
bac
bca
cab

cba

Write a program to find All the combination of a number

# include <stdio.h>
void perm(int *,int,int);
int main(void)
{
int i,num,a[10];
printf("\nenter the number\n");
scanf("%d",&num);
i=0;
while(num!=0)
{
i++;
a[i]=num%10;
num=num/10;
}
perm(a,1,i);
}

void perm(int a[10],int k,int i)
{
int temp,j;
if(k==i){
for(j=1;j<=i;j++)
printf("%d",a[j]);
printf("\n");
}
else{
for(j=k;j<=i;j++){
temp=a[k];
a[k]=a[j];
a[j]=temp;
perm(a,k+1,i);
}
}
return;
}


Output:
enter the number
123
321
312
132
123
321

312

Write a program to find whether a Number is a perfect number

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,s;
printf("Enter a number: -");
scanf("%d",&n);
s=0;
i=1;
while(i<n)
{
  if(n%i==0)
   s=s+i;
   i++;

  }

 if(s==n)
  printf("%d is a perfect number.",n);
 else
  printf("%d is not a perfect number.",n);
getch();

}



Output:
Enter a number: -28

28 is a perfect number.


Write a program to find Palindrome numbers between a range


#include<stdio.h>
#include<conio.h>
int main (void)
{
  int i,n,r,m;
  printf("The palindrome numbers between 1 to 1000 are : - ");
  for(i=1;i<1000;i++)
  {
     n=i;
     m=0;
     while(n!=0)
      {
//r=n-(int)(n/10)*10;
r=(int)(n%10);
m=m*10+r;
n=(int)(n/10);
      }
     if(i==m)
      printf("%d\n",i);
    }
   getch();
   return 0;

}

Fibonacci series using loop

/*               This is a program for printing the Fibonacci series of n terms

Enter the number of terms  : - 10
1
1
2
3
5
8
13
21
34
55
*/

#include<stdio.h>
int main()
{
 int i,num,num1=1,num2=1,result;
 printf("Enter the number of terms  : - ");
 scanf("%d",&num);
 printf("%d\n%d\n",num1,num2);
 for(i=1;i<=num-2;i++)
  {
   result=num1+num2;
   printf("%d\n",result);
   num2=num1;
   num1=result;
   }
return 0;

}


Fibonacci series using function upto a limit

/*
How many fibonacci numbers : 10
ith value = 1,  Fibonacci Value =1
ith value = 2,  Fibonacci Value =1
ith value = 3,  Fibonacci Value =2
ith value = 4,  Fibonacci Value =3
ith value = 5,  Fibonacci Value =5
ith value = 6,  Fibonacci Value =8
ith value = 7,  Fibonacci Value =13
ith value = 8,  Fibonacci Value =21
ith value = 9,  Fibonacci Value =34
ith value = 10,  Fibonacci Value =55
*/

#include<stdio.h>
int fibonacci(int count);
int main()
{
 int count = 1,n= 1;
 printf("How many fibonacci numbers : \n");
 scanf("%d",&n);
 printf("\n");
 for(count=1;count<=n;++count)
 {
  printf("ith value = %d,  Fibonacci Value =%d \n", count,fibonacci(count));
 }
 return 0;
}
int fibonacci(int count)
{
 static long int f1=1,f2=1;
 long int f;
 f=(count<3)?1:f1+f2;
 f2=f1;
 f1=f;
 return(f);

}


Fibonacci series up to a limit using recursion

/*
Enter the number if terms to be generated :-  10
The fibonacii series is :-
1  1   2  3  5   8   13    21    34     55
*/

#include<stdio.h>
#include<conio.h>
int fibo(int);
int main()
{
int n,i;
printf("Enter the number if terms to be generated :-  ");
scanf("%d",&n);
printf("\n\nThe fibonacii series is :-\n\n");
for(i=0;i<n;i++)
{
printf("%d\t",fibo(i));
}
getch();
}
int fibo(int n)
{
int val=0;
if(n==0)
   return(1);
else if(n==1)
   return(1);
else
   val=fibo(n-1)+fibo(n-2);
   return(val);

}

Find Factorial using Loop


/*
Enter the number :  5
The factorial value of the number is :  120
*/
#include<stdio.h>
int fact(int);
int main()
{
  int num,result;
  printf("Enter the number :  ");
  scanf("%d",&num);
  result=fact(num);
  printf("The factorial value of the number is :  %d",result);
  return 0;
}
int fact(int num)
{
  int i,fact=1;
  for(i=num;i>0;i--)
  fact=fact*i;
  return(fact);
}

Find Factorial using recursion


/*
Enter the number whose factorial is needed :-  10
The factorial value of 10 is 3628800

Enter the number whose factorial is needed :-  5
The factorial value of 5 is 120
*/

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

int factorial(int);

int main()
{
int n;
printf("Enter the number whose factorial is needed :-  ");
scanf("%d",&n);
printf("\n\nThe factorial value of %d is %d",n,factorial(n));
return 0;
}

int factorial(int val)
{
if(val==0 || val==1)
   return(1);
else
   val=val*factorial(val-1);
}

Find NCR factor using C


/*
Enter value for n & r : 10 2
NCR factor : 45
*/
#include<stdio.h>
#include<conio.h>

int fact(int);

int main()
{
 int n,r,c;
 printf("Enter value for n & r : ");
 scanf("%d%d",&n,&r);
 c=fact(n)/(fact(r) * (fact(n-r)));
 printf("\n\n NCR factor : %d",c);
 getch();
}
int fact(int x)
{
int i,f=1;
for(i=x;i>=1;i--)
f=f*i;
return(f);
}

Armstrong number upto a range

/*
The Armstrong numbers are : -
1
153
370
371
407
*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i,sum=0,a,b,j,k;
 printf("The Armstrong numbers are : - ");
 for(k=1;k<=1000;k++)
  {
  j=k;
 while(j>0)
 {
   b=j%10;
   sum=sum+(b*b*b);
   j=j/10;
 }
 if(k==sum)
 printf("\n%d",k);
 sum=0;
}
 getch();
}



No comments:

Post a Comment