Search This Blog

Tuesday 16 July 2019

9. C program to count the number of vowels, consonants and special characters in a string

/*9. C program to count the number of vowels, consonants and special characters in a string.*/
#include <stdio.h>

void main()
{
    char   str[100];
    int i, digit, vowel, consonant, splchar, spaces;
    clrscr();

    digit = vowel = consonant = splchar = spaces = 0;

    printf("Enter a string: ");
    gets(str);

    for(i=0;str[i]!=NULL;i++)
    {

    if(str[i]>='0' && str[i]<='9')
        digit++;
    else if(str[i]==' ')
        spaces++;
    else if(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]== 'O' || str[i]=='U' ||
    str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')

        vowel++;
    else if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
        consonant++;
    else
        splchar++;
    }

    printf("\nDigits: %d \nVowels: %d \nSpaces: %d \nConsonants: %d \nSpecial Characters: %d",digit,vowel,spaces, consonant,splchar);

    getch();
}

8. Program to find e to the power x using n terms of the exponential series.

// 8. Program to find e to the power x using n terms of the exponential series.
#include<stdio.h>
#include<conio.h>
#include<math.h>
      //Correction is required
void main()
{
    int i,n,j;
    float x,t, exp , sum=0;
    clrscr();

    printf("Enter the value for x:");
    scanf("%f", &x);

    printf("\nEnter the value for n:");
    scanf("%d", &n);
    if(n<=1)
    {
    printf("1\n");
    getch();
    exit(0);
    }
    printf("\nThe exponential series is:\n");
    
    for(i=1;i<n;i++)
    {
       if(i==1)
      printf("%d + ",i);
       t=i;
       for(j=i;j>1;j--)
       {
          t=t*(j-1);
       }

       exp=pow(x,i)/t;
       sum=sum+exp;
       printf("%.3f + ",exp);
    }
    printf("\nThe Exponential Value = %f",sum+1);
    getch();
}

7. Program to generate n terms of the series 1,-2,6,-24,120.....

//7. Program to generate n terms of the series 1,-2,6,-24,120.....
#include<stdio.h>
void main()
{
    int n=10,i,j=-2,k=1;
    clrscr();
    printf("Enter the limit:");
    scanf("%d",&n);
    printf("The Series is:\n");
    for(i=0;i<n;i++)
    {
           printf("%d\n",k);
           k=k*(j);
           j--;
    }
    getch();
}

6. Program to convert decimal number into binary.

//6. Program to convert decimal number into binary.
#include<stdio.h>
void main()
{
    int dec,bin=0,rem,i=1;
    clrscr();
    printf("Enter a decimal number:");
    scanf("%d",&dec);
    do
    {
        rem = dec%2;
        bin = bin + rem * i;
        dec = dec /2;
        i=i*10;
    }while(dec>0);
    printf("Binary Equivalent = %d",bin);
    getch();
}

5. C Program to find the factors of nth Fibonacci number.

//5. C Program to find the factors of nth Fibonacci number.
#include<stdio.h>
void main()
{
    int num,f1=0,f2=1,f3=0,i;
    clrscr();
    printf("Enter the value for N:");
    scanf("%d",&num);

    for(i=1;i<num;i++)
    {
        f1=f2;
        f2=f3;
        printf("%d\n",f3); //This statement is for debugging.
        f3=f1+f2;

    }
    printf("\nNth Fibonacci Number = %d  \n",f3);
    printf("Its factors are:\n");
    for(i=1;i<=f3;i++)
    {
        if(f3%i==0)
            printf("%d\n",i);
    }

    getch();
}

4. Program to find nth prime and check whether it is palindrome or not.

//4. Program to find nth prime and check whether it is palindrome or not.
#include<stdio.h>
void main()
{
    int n,i=2,j,k=3,prime,rev=0,rem,temp;
    clrscr();
    printf("Enter nth number:");
    scanf("%d",&n);
    printf("2\n");
    while(i<=n)
    {
        for(j=2;j<=k-1;j++)
        {
            if(k%j==0)
            {
                break;
            }
        }
        if(j==k)
        {
            printf("%d\n",k);
            i++;
            prime=k;
        }
        k++;
    }
    temp=prime;
    while(temp!=0)
    {
        rem = temp%10;
        rev = rev * 10 + rem;
        temp = temp / 10;
    }
    printf("%dth Prime Number is %d \n",n,prime);
    if(prime==rev)
        printf("It is Palindrome. \n");
    else
        printf("It is not Palindrome. \n");

    getch();
}

3. Program to check whether a given number is Armstrong, odd or even, perfect square or cube.

//3. Program to check whether a given number is Armstrong, odd or even, perfect square or cube.
#include<stdio.h>
#include<math.h>
void main()
{
    int number,i,temp,sflag=0,cflag=0;
    double remainder, result = 0,n=0;
    clrscr();
    printf("Enter a number: ");
    scanf("%d", &number);
    temp = number;
   
    while (temp != 0)
    {
    temp =temp / 10;
    ++n;
    //printf("n=%d\n",n); //This is for debugging.
    }
    temp = number;
    while (temp != 0)
    {
    remainder = temp%10;
    result = result + pow(remainder, n);
    temp = temp / 10;
    //fflush(stdin);
    }
    if(result == number)
    printf("It is an Armstrong number.\n");
    else
    printf("It is not an Armstrong number.\n");
    // True if the number is perfectly divisible by 2
    if(number % 2 == 0)
    printf("It is an even number.\n");
    else
    printf("It is an odd number.\n");


    for(i=0;i<number/2;i++)
    {
        if(i*i==number)
        {
            printf("It is a perfect square.\n");
            sflag=1;
        }

        if(i*i*i==number)
        {
            printf("It is a perfect cube.\n");
            cflag=1;
        }

    }
    if(sflag==0)
        printf("It is not a perfect square.\n");
    if(cflag==0)
        printf("It is not a perfect cube.\n");
      getch();
}

1. C Program to find the biggest and smallest among 4 numbers using nested if.

//1. C Program to find the biggest and smallest among 4 numbers using nested if.
#include<stdio.h>
void main()
{
    int a=400,b=300,c=20,d=200,big,small; //Declare variables and initialize them.
    clrscr();    //Clear the screen.
    if((a>b && a>c) && a>d)      //Check whether a is greater than b,c and d.
    {
        big=a;
        if(b<c && b<d)       //Check whether b is lesser than c and d.
            small=b;
        else if(c<d)               //Check whether c is lesser than d.
            small=c;
        else
            small=d;

    }
    else if(b>c && b>d)             //Check whether b is greater than c and d.
    {
        big=b;
        if(a<c && a<d)             //Check whether a is lesser than c and d.
            small = a;
        else if(c<d)               //Check whether c is lesser than d.
            small = c;
        else
            small = d;

    }
    else if(c>d)                       //Check whether c is greater than d.
    {
        big=c;
        if(a<b && a<d)          //Check whether a is lesser than b and d.
            small=a;
        else if(b<d)               //Check whether b is lesser than d.
            small=b;
        else
            small=d;

    }
    else
    {
        big=d;
        if(a<b && a<c)
            small=a;
        else if(b<c)
            small=b;
        else
            small=c;
    }
    printf("Biggest Number=%d \t Smallest Number=%d",big,small);
    getch();
}
FREE Hit Counters