Friday, 30 May 2014

Write a program to print even numbers from 1 to 50.

#include<stdio.h>

main()
{
    int i=1;
   
    printf("\nEven numbers from 1 to 50=");
   
    while(i<=50)
    {
        if(i%2==0)
            printf("%d ",i);
        i++;
    }
}

Write a program to display your name upto 10 times using while loop.

#include<stdio.h>

main()
{
    int i=1;
    char name[100];
    printf("\nEnter your name=");
    gets(name);
    while(i<=10)
    {
        printf("\n%s",name);
        i++;
    }
}

Program to calculate area of a triangle. Base and height given by the user.

#include<stdio.h>

main()
{
    float l,b,area;
    printf("\nEnter base and height of the triangle=");
    scanf("%f%f",&b,&h);
    area=0.5*b*h;
    printf("\nThe area=%d",area);
}

Thursday, 29 May 2014

Write a program to simple interest, compound interest and amount.

#include<stdio.h>
#include<math.h>
main()
{
    float p,r,n,si,ci,cia,temp;
   
    printf("\nEnter Principal, rate of interest and number of years=");
    scanf("%f%f%d",&p,&r,&n);

    si=p*n*r/100;
    cia=p*pow((1+(r/100)),n);
    ci=cia-p;

    printf("\nSimple Interest=%f, Amount=%f",si,si+p);
    printf("\nCompound Interest=%f, Amount=%f",ci,cia);
}

Monday, 26 May 2014

Write a program to calculate the sum of all the numbers from 1 to 50 using for loop.

#include<stdio.h>

main()
{
    int i,sum=0;
    for(i=1;i<=50;i++)
    {
        sum=sum+i;
    }
    printf("\nSum of all the number from 1 to 50=%d",sum);
}

Saturday, 3 May 2014

Write a C program to find the second largest element of an array of n elements.

#include<stdio.h>

main()
{
    int a[100],n,fl,sl,i;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("\nThe list =");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }

    fl=a[0];
    for(i=1;i<n;i++)
    {
        if(fl<a[i])
            fl=a[i];
    }
   
    sl=a[0];
    for(i=1;i<n;i++)
    {
        if(sl<a[i] && a[i]<fl)
            sl=a[i];
    }
   
    printf("\nThe second largest element=%d",sl);
}

Write a C program to find frequency of a given character in a given string. e.g, str="assam" ch=s; frequency=2

#include<stdio.h>
#include<string.h>
main()
{
    char str[100],ch='a';
    int i,f=0;
    printf("\nEnter a string=");
    gets(str);
    //scanf("%s",str);
    printf("\nEnter a character=");
    scanf("%c",&ch);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==ch)
            f++;
    }

    printf("\nFrequency of %c in %s is %d",ch,str,f);
}

Write a C program to find the number of vowels and consonants in a given string.

#include<stdio.h>
#include<string.h>
main()
{
    char str[100];
    int i,v=0,c=0;

    printf("\nEnter a string=");
    scanf("%s",str);
    for(i=0;i<strlen(str);i++)
    {
        if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
        {
            if(str[i]=='a' ||str[i]=='A' ||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
                v++;
            else
                c++;
        }
    }

    printf("\nThe number of vowels=%d, Number of consonants=%d",v,c);
}

Write a ‘C’ program to remove duplicates from an ordered array. e.g. if input is 1,1,1,3,4,4,5,8,8 then output should be 1,3,4,5,8.

#include<stdio.h>

main()
{
    int a[100],result[100],n,i,j,k,f;
    printf("\nEnter number of terms=");
    scanf("%d",&n);
    printf("\nEnter the elements in ascending order=");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("\nThe array =");
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    result[0]=a[0];
   
    for(i=1,j=1,k=1;i<n;i++)
    {
        f=0;
        for(j=0;j<i;j++)
        {
            if(a[i]==a[j])
            {
                f=1;
                break;
            }
        }
        if(f==0)
        {
            result[k++]=a[i];
        }
    }
   
    printf("\nThe resultant array=");
    for(i=0;i<k;i++)
    {
        printf("%d ",result[i]);
    }
}