Thursday, October 15, 2015

Program to generate Prime numbers from 1 to n

#include<stdio.h>
void main()
{
     int num,i=1,j,count;
     clrscr();


          printf("Enter Num value To Print Prime Numbers between 1 and Num: ");
          scanf("%d",&num);


    printf("Prime Numbers upto %d :\n \n",num);

    while(i<=num)
    {
        count=0; // initializing count to 0 for each iteration
                for(j=1;j<=i;j++)
                {
                    if(i%j==0) //checking whether num is dvisible by j
                       count++;
                }
                    if(count==2) //if num is divisible by 2 numbers,then it is prime
                     printf("%d ",i);
                    i++;
   }
printf("\n\n");
getch();
}

Program to find first 20 Fabonacci series

#include<stdio.h>
void main()
{
    int k,r=20;
    long int i=0l,j=1,f;
    clrscr();
    //Taking maximum numbers form user

    printf("FIBONACCI SERIES:\n ");
    printf("%ld %ld",i,j); //printing firts two values.

    for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
    }

Program to convert digits into English characters



#include <stdio.h>

void main()
{
    int num, digit,counter;
    int reversed = 0, backupZero = 0;
    clrscr();
    printf("Please enter an integer:\n");
    scanf("%d", &num);

    if (num == 0) // In case the input is just "0"
    {
printf("zero");
    }

    while (num > 0) // Loop to reverse the integer
    {
digit = num % 10;
reversed = (reversed * 10) + digit;

if ((reversed == 0) && (digit == 0)) // If the integer finishes in zero
{
   ++backupZero; // Use this to add extra zeroes later
}

num /= 10;
    }

    while (reversed > 0)
    {
digit = reversed % 10;
reversed /= 10;

switch (digit)
{
   case 1:
printf("one ");
break;

   case 2:
printf("two ");
break;

   case 3:
printf("three ");
break;

   case 4:
printf("four ");
break;

   case 5:
printf("five ");
break;

   case 6:
printf("six ");
break;

   case 7:
printf("seven ");
break;

   case 8:
printf("eight ");
break;

   case 9:
printf("nine ");
break;

   default:
printf("zero ");
break;
}

    }

    for (counter = 0; counter < backupZero; ++counter) // Prints the extra zeroes at the end
    {
printf("zero ");
--backupZero;
    }

    printf("\n");

    getch();
}

Program for String Operations

void main()
{
char str1[20],c;
int i,j,len,vow=0,a=0, the=0;
clrscr();
printf("*********STRING OPERATION PROGRAM********");
printf("\n\tEnter the String: ");
gets(str1);     //gets() inputs a string & saves into str1 variable

len=strlen(str1);  //strln() is used to calculate length of string
printf("\n\tLength of String is: %d",len);
//Below code will find vowels in a string str1

for(i=0;i<len;i++) //this will check string str1 from index 0 to its length
{
if(str1[i]=='a'|| str1[i]=='i'|| str1[i]=='e'|| str1[i]=='o'|| str1[i]=='u')
vow++;
}
if(vow==0)
printf("\n\tString contains no vowels");
else
printf("\n\tNo. of Vowels in string str1: %d",vow);
       //End of finding vowels
       //Below code will find number of occurance of 'a'
       for(i=0;i<len;i++)
       {
if(str1[i]=='a')
a++;

       }
       if(a==0)
       printf("\n\tString co ntains no 'a' character");
       else
       printf("\n\tNo. of 'a' in String: %d",a);
       //End of finding character 'a'
       //Below code will find 'the' in string
       for(i=0;i<len;i++)
       {
 if(str1[i]=='t')
 { i++;
   if(str1[i]=='h')
   {
    i++;
    if(str1[i]=='e')
    the++;
   }
  }
       }
       printf("No. of occurance of 'the': %d",the);
       //End of finding 'the'
       getch();

//End of program

}