Search

C Programs

by 08:26:00 0 comments
Fibonacci Using recursion
#include<stdio.h>
int fib(int n)
{
    static int a=1,b=1,c;
    if(n--)
    {
        c=a+b;
        a=b;
        b=c;
        printf("%d ",c);
        fib(n);
    }
    return 0;
}
main()
{
    static int n=5;
    static int a=1,b=1;
    printf("%d %d ",a,b);
    fib(n-2);
}

Output:
1 1 2 3 5 


Thermal conversion

#include<stdio.h>
int main()
{
    char a[10],c;
     int b,i;
    scanf("%s",a);
    for(i=0;a[i]!='\0';i++)
        {
        if(a[i]=='k'||a[i]=='K')
        {
            b=atoi(a)-273;
            c='C';
        }
        if(a[i]=='c'||a[i]=='C')
        {
            b=atoi(a)+273;
            c='K';
        }
    }
    printf("%d%c",b,c);
    return 0;
}

Output:
45C
318K

318K
45C

Decimal to binary,decimal,octal,hexadecimal conversion using itoa:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int a=54325;
    char buffer[20];
    itoa(a,buffer,2); 
    printf("Binary value = %s\n", buffer);

    itoa(a,buffer,8); 
    printf("Octal value = %s\n", buffer);
    itoa(a,buffer,10);  
    printf("Decimal value = %s\n", buffer);
    itoa(a,buffer,16); 

    printf("Hexadecimal value = %s\n", buffer);
    return 0;
}
Output:
Binary value = 1101010000110101

Octal value = 152065
Decimal value = 54325
Hexadecimal value = D435


Program to separate one integer and find highest occured number
 #include<stdio.h>
int cmp(void *a,void *b)
{
    return (*(int*)a-*(int*)b);
}
main()
{
    int hash[10]={0};
    int a,b,c,d,e[20],i=0,j;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    while(a)
    {
        e[i]=a%10;
        i++;
        a=a/10;
        e[i]=b%10;
        i++;
        b=b/10;
        e[i]=c%10;
        i++;
        c=c/10;
        e[i]=d%10;
        i++;
        d=d/10;
    }
    qsort(e,i,sizeof(int),cmp);
    for(j=0;j<i;j++)
    {
        hash[e[j]]++;
    }
    int max=0;
    for(j=0;j<10;j++)
    {
        if(hash[j]>hash[max])
        {
            max=j;
        }
    }
    printf("%d",max);
}


Output: 
123 456 789 222
2



HCF for multiple numbers : 
 #include<stdio.h>
int main()
{
    int n,x,y=-1;
    printf("\n Enter the no of elements:");
    scanf("%d",&n);
    printf("\nEnter the numbers ");
    while(n--){
         scanf("%d",&x);
    if (y==-1)
         y=x;
        else if (x<y)
             y=gcd(x,y);
         else
             y=gcd(y,x);
    }
    printf("GCD is %d",y);
    return 0;
}

int gcd(int x,int y)
{
    while(x!=y)

 {

  if(x > y) x -= y;

  else y -= x;

 }
        return x;
}

Output:

Enter the no of elements:2
Enter the numbers 12 6
GCD is 6

LCM for multiple numbers :

#include<stdio.h>
int lcm(int,int);
int main()
{
    int a,b=1,n;
    printf("Enter no of integers.");
    scanf("%d",&n);
    printf("Enter positive integers.");
    while(n--)
    {
     scanf("%d",&a);
            if(a>b)
             b = lcm(a,b);
         else
             b = lcm(b,a);
    }

    printf("LCM is %d",b);
    return 0;
}
int lcm(int a,int b)
{
    int temp = a;
    while(1){
         if(temp % b == 0 && temp % a == 0)
             break;
         temp++;
    }
    return temp;
}
Output: 
Enter no of integers.5
Enter positive integers.12 10 32 45 6
LCM is 1440


Program to split String
#include<stdio.h>
main()
{
    char a[100],b[25][25];
    scanf("%[^\n]s",a);
    int i,j=0,k=0;
    for(i=0;i<a[i];i++)
    {
        if(a[i]!='"')
        {
            b[j][k]=a[i];
            k++;
        }
        if(a[i]=='-')
        {
            b[j][k]='>';
            k++;
        }
        if(a[i]==',')
        {
            j++;
            k=0;   
        }
    }
    for(i=0;i<=j;i++)
    {
        printf("%s\n",b[i]);
    }
}

Output:
"vinoth"-"kumar","gokul"-"kannan"
vinoth->kumar,
gokul->kannan

Amicable Numbers:
#include<stdio.h>
int factor(int n);
main()
{
    int i;
    for(i=1;i<=10000;i++)
    {
        if(i==factor(factor(i)))
        {
            printf("%d ",i);
        }
    }

int factor(int n)

 int i,sum=0; 
 for(i=1; i <n; ++i) 
 {
  if (n%i == 0)
  {
   sum=sum+i;
  }
 }
 return sum;
}

Output:
6 28 220 284 496 1184 1210 2620 2924 5020 5564 6232 6368 8128
Lychrel Number:
#include<stdio.h>
int rev(int n);
main()
{
    int i;
    for(i=100;i<=500;i++){
    int n,c=1,pal;
    n=i+rev(i);
    while(c<=50)
    {
        pal=rev(n);
        if(n!=pal)
        {
            n+=pal;
            c++;
        }
        else
        break;
    }
if(c>50)
{
printf("%d ",i);
}
}

int rev(int n)

 int r=0,rem;
    while(n != 0)
    {
        rem= n%10;
        r=r*10+rem;
        n/=10;
    }
    return r;
}

Output:
187 196 286 295 385 394 484 493





                                                                   

Anonymous

Developer

This is created by GAVASKAR .

0 comments:

Post a Comment