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

}

Tuesday, September 22, 2015

Write a C Program to accept three sides of triangle from console and to test and print the type of triangle-Equilateral, Isosceles, Right angle, none of these.

Write a C Program to accept three sides of triangle from console and to test and print the type of triangle-Equilateral, Isosceles, Right angle, none of these.

#include<stdio.h>
#include<math.h>

int main() {
  int a, b, c;
  float s, area;

  printf("Enter the values of the sides of the triangle: \n");
  scanf("%d %d %d", &a, &b, &c);                                    //Input three sides of triangle
  if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))   //test sides using IF ELSE. Here if else uses logical AND operator(&&) which checks for the conditions and return true if all the conditions are true, else it will return falls.
{
    s = (a + b + c) / 2.0;
    area = sqrt((s * (s - a) * (s - b) * (s - c)));    //"sqrt" function determines square root o the given term
    if (a == b && b == c) {
      printf("Equilateral Triangle. \n");
      printf("Area of Equilateral Triangle is: %f", area);
    }
    else if (a == b || b == c || a == c) {
      printf("Isosceles Triangle. \n");
      printf("Area of an Isosceles Triangle: %f", area);
    }
    else {
      printf("Scalene Triangle. \n");
      printf("Area of Scalene Triangle: %f", area);
    }
  }
  else {
    printf("Triangle formation not possible");
  }

  return 0;
}


//************Description****************
// Above program accepts three sides of triangle and determines its type by using IF-//ELSE statement.
//For more description leave your comment below

Friday, August 14, 2015

Write a C program to calculate the sum of all numbers from 0 to 100 that are divisible by 4

#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0 ;
clrscr();
for ( i =0 ; i <= 100 ; i++)
{
if(i%4 == 0)
{
printf("\n %d",i);
sum+=i;
}
}
printf("\n Sum = %d",sum);
getch();
}

Sunday, August 2, 2015

Write a C Program to Accept five numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,t;  //Take Array of 5 integers
clrscr();
printf("Enter 5 nos.\n\n");
for (i=0;i<5;i++)     //Loop to fill array with five numbers
scanf("%d",&a[i]);
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)  //loop to compare which number is smaller
{
if(a[i]>a[j])
{
t=a[i];           // Swap small to large numbers
a[i]=a[j];
a[j]=t;
}
}
}
printf("Ascending Order is:");    //Print Result
for(j=0;j<5;j++)
printf("\n%d",a[j]);
getch();
}


******Description*******
1. If you wish to process multiple data inputs, use Array (Array is simply a sequence of memory location includes same type of data). Array can be made for int, char, float, etc. But mix form of array is not allowed. Like some array having elements both int and char is not allowed.

2. To put elements in array one should use 'Loop'. And very popular loop is 'FOR' loop. (Syntax of 'FOR' loop is 'for(initialization;condition;increment/decrement) )

3. Iteratively we compare each element with its successor. If we find it small then we will swap them.

4. We will loop this until end of array encounters

5. Now we have result.

6. To print the result we again will need loop.(Remember, to read or write to/from array you will need a loop)

7. And thats it. Post a comment for your queries.