Tuesday, 31 December 2013

PROGRAM TO CHECK WHETHER GIVEN NUMBER IS PRIME OR NOT....!!!!

#include<stdio.h>
int main()
{
 int number;
 int count=0;
 printf("Enter number to check if it is prime or not:");
scanf("%d",&number);
for(int a=1;a<=number;a++)
{
  if(number%a==0)
  {
  count++;
  }
}
    if(count==2)
    {
    printf("\nNUMBER IS PRIME");
    }
    else
    {
    printf("\nNUMBER IS NOT PRIME");
    }
return 0;
}

EXPECTED OUTPUT;
















PROGRAM TO PRINT HOLLOW SQUARE...!!!!

#include<stdio.h>
#include<conio.h>
int main()
{
int size;
printf("please enter the size of square:");
scanf("%d",&size);
for (int row = 1; row <= size; row++)
{
for (int col = 1; col <= size; col++)
{
if (row > 1 && row < size && col > 1 && col < size)
{
   printf(" ");
}
else
{
printf("*");
}
}

printf("\n");
}
getch();
return 0;
}

EXPECTED OUTPUT:




PROGRAM TO PRINT A TABLE USING FOR LOOP...!!!

#include<stdio.h>
int main()
{
    int table,x;
    printf("Enter the number to print table:");
    scanf("%d",&table);
    for(x=1;x<=10;x++)
    {
        printf("%d X %d = %d\n",table,x,x*table);
    }
    getch();
    return 0;
}

Expected Output: 





Sunday, 29 December 2013

FOR CHECKING PROGRAMMING SKILLS AND TO PRACTICE

           Give the output of following programs.

Question # 1.


 void main()
   {
           int a,b;   // let a=5; b=6;
           printf(“Enter values of a and b”);
           scanf(“%d %d”,&a,&b);
           printf(“a = %d b = %d”,a,b);
   } 

Question # 2. 

 void main()
   {
int i=10;
for(;i>=0; i--)
{
     i -=2;
     printf(“%d”, i);
 }
   }


 Question # 3.

  void main()
           {
              int i = 1;
              for (;;)
              {
                printf("%d", i++);
                if (i >10)
                break;
              }
           }


 Question # 4.

  void main() 
           {
              int i = 4;
  switch (i)
  {
     default:
     printf(“\n This is the Default case”);
     case 1:
     printf(“\n This is the Case 1”);
     break;
     case 2:
     printf(“\n This is the Case 2”);
     break;
     case 3:
     printf(“\n This is the Case 3”);
   }
           }


Question # 5.
  
  void main()
{
    int i;
    for(i=0; i<10; i++)
    {
      printf("%d\t ", i);
      if (i%2 == 0) continue;
      printf("\n");
     }    
}


Expected output :
Question # 1

     Enter the value of a and b 5
     6
     a = 5 b = 6

Question # 2

     852-1

Question # 3

12345678910

Question # 4

     This is the Default case
     This is the Case 1
Question # 5

     0    1
     2    3
     4    5
     6    7
     8    9


PROGRAM TO ARRANGE ELEMENTS OF ARRAY IN ASCENDING AND DESCENDING ORDER

This is a program which arrange the array elements in different orders depending upon users input using "Functions"...!!!
It could be easily modified to "n" number of elements of array...!!!!
Feel free to contact me for any problem and help...!!!

#include<stdio.h>
void ascending(int a[],int size);
void descending(int a[],int size);
int main()
{
   int size=10;
int a[]={2,3,4,5,3,2,1,4,5,9};
int x,y,choice;
printf("1 for ascending\n2 for descending\nEnter the choice:");
scanf("%d",&choice);
    if (choice==1)
    ascending(a,size);
    else if(choice==2)
    descending(a,size);
    else
    printf("Not a valid input");
getch();
return 0;
}

void ascending(int a[],int size)
{
    int x=0,b;
    int temp;
    for(b=0;b<size-1;b++)
    {
 for(x=0;x<size-1;x++)
 {
     if (a[x+1]<a[x])
         {
            temp=a[x];
            a[x]=a[x+1];
            a[x+1]=temp;
         }
 }
    }
 for(x=0;x<size;x++)
 {
    printf("%d\t",a[x]);
 }

}

void descending(int a[],int size)
{
       int x,y;
       int temp;
    for(y=0;y<size-1;y++)
    {
        for(x=0;x<size-1;x++)
        {
           if (a[x]<a[x+1])
           {
            temp=a[x+1];
            a[x+1]=a[x];
            a[x]=temp;
           }
        }
    }
   for(x=0;x<size;x++)
  {
    printf("%d\t",a[x]);
  }
}

EXPECTED OUTPUT: