Search This Blog

Thursday, 12 September 2019

10. C Program to generate n prime numbers and print them in pyramid pattern.

//10. C Program to generate n prime numbers and print them in pyramid pattern.

#include <stdio.h>
#include <conio.h>
void main()
{
    int space, n, prime[100];     //Declare variables.
    int count=0, i=2, j, k=2;
    clrscr();
    printf("Enter nth number:");
    scanf("%d",&n);
    while(i<=n+1)
    {
    for(j=2;j<=k-1;j++)
    {
        if(k%j==0)             //Check whether k is divisible by any other number.
        {
        break;
        }
    }
    if(j==k)
    {
       // printf("%d\n",k);    //For debugging.
        prime[count++]=k;
        i++;
    }
    k++;
    }
    count=0;
    space=n;
    for (i=0;i<=n/2+1;i++)
    {
    for (k=0;k<space;k++)
    {
        printf("   "); //Provide 2 spaces
    }
    for (j=0;j<2*i-1;j++)
    {
        if(count<n)
        printf("%d  ",prime[count++]); //Provide 2 spaces after %d
    }
    space--;
    printf("\n");
    }
    getch();
   
}

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();
}
FREE Hit Counters