Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

5 Dec 2012

C program to print sum of series 1+2+3+..+n


#include<stdio.h>
void main()
{
  int sum=0,i,n;
  printf("\n>>> C Program to print Sum of Series 1+2+3+..+n <<<\n");
  printf("\n Enter the number of terms:");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
      sum+=i;
  }
  printf("\n Sum of series upto %d is :%d \n\n",n,sum);

}
..
Output:



11 Nov 2012

HOW TO PERFORM % OPERATION ON DOUBLE VALUES/ FMOD( ) FUNCTION IN C

Before you go through this post go for the link to know about % operator.
-> As % operator cannot used with DOUBLE values, as it generates compile time error, there is a special function " fmod( ) " in c. 

-> fmod( ) return type is double. So, we use %lf specifier when it is used .

#include<math.h>
double fmod(double numerator,double denominator)

Example programs to understand fmod() function :

void main()
{
  int a ;
 printf("%lf",fmod(10.5%3));
}

output: 1.500000
--------------------------------------


void main()
{
  float a=10.0%3;
  printf("%lf",a);
}

output: shows error like Illegal use of floating point.

10 Nov 2012

% OPERATOR/ MOD OPERATOR IN C

% OPERATOR IN C :
-----------------------------------
-> % OPERATOR is to get the remainder when a number is divided with any other number.
   
   Example: int a=10%2; then the a value is zero. because the when 10 is divided by 2   
      the remainder is zero.
     
 int a=10%2; Here 10 is numerator and 2 is denominator.

-> When the  numerator is less than the denominator, then the result of the % operator is
      numerator value. 
     Example: int a=2%10; Here the numerator 2 is less than the denominator 10. Hence  
     a value will be 2. 

-> We cannot apply mod % operator on the floating values. It will generate compile time
      error. 
-> There is a special function " fmod() " for applying % operation on float.
-> Whenever we are performing mod % operation on any two operands then the resultant
     sign is equivalent to numerator sign.

     int a=-10%3; Then the result is a=-1 as the sign of numerator
                10 is negative.

Examples to understand the operation of % operator:

1) int a=1%10;
   printf("%d",a);
  output:1
-------------------------
2) int a=2%10;
   printf("%d",a);
  output:2
--------------------------
3) int a=3%10;
   printf("%d",a);
  output:3
--------------------------
4) int a=10%3;
   printf("%d",a);
  output:1
--------------------------
5) int a=10%4;
   printf("%d",a);
  output:2
---------------------------
6)int a=10%2;
   printf("%d",a);
  output:0
---------------------------
7) int a=-10%3;
   printf("%d",a);
  output:-1
-------------------------
8) int a=10.0%3;
   printf("%d",a);
  output: Error. As % operator cannot be applied on float.
--------------------------
9) int a= -10%-3;
   printf("%d",a);
  output: -1 
  (As the result sign is same as numerator sign)
------------------------------
10) int a=10%3.0;
   printf("%d",a);
  output: Error.As 3.0 is float and cannot be used in 
          % operation.

C PROGRAM TO IMPLEMENT SIMPLE IF-ELSE IN C

Syntax of Simple if-else
-------------------------
if(condition)
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}
else
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}

Program:


#include<stdio.h>
void main()
{
    int num;
    printf(" >>> c program to implement simple if-else <<< \n\n");
    printf("\n Enter the number: ");
    scanf("%d",&num);
    if(num%2==0) //% operator returns remainder
    {
        printf("\n Entered number is Even Number \n");
    }
    else
    {
      printf("\n Entered number is Odd Number \n\n");
    }
 
}

Output:

Explanation:
In the above program, if statements are executed if the condition is true. otherwise else statements are 
executed. And else statements are executed only if the if condition fails.

SIMPLE IF STATEMENT IN C

Syntax of Simple-if
-------------------------
if(condition)
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}

Simple C Program to understand Simple if :


#include<stdio.h>
void main()
{
    int age;
    printf(" >>> c program to implement simple if<<< \n\n");
    printf("\n Enter the age of the person: ");
    scanf("%d",&age);
    if(age>=18)
    {
        printf("\n Eligible for voting \n");
    }
    printf("\n program ends \n\n");
}

Output:

Explanation:
In the above program, the control enters into the if block only if the condition is true. And the statement printf("\n program ends \n\n"); is executed every time.

C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY

C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY


  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define MAX 10
  4. int queue[MAX], front = -1, rear = -1;
  5. void Insert_Element();
  6. void Delete_Element();
  7. void Display_Queue();
  8. void Empty_Queue();
  9.  
  10. int main()
  11. {
  12. int option;
  13. printf(">>> c program to implement queue operations <<<");
  14. do
  15. {
  16. printf("\n\n 1.Insert an element");
  17. printf("\n 2.Delete an element");
  18. printf("\n 3.Display queue");
  19. printf("\n 4.Empty queue");
  20. printf("\n 5.Exit");
  21. printf("\n Enter your choice: ");
  22. scanf("%d", &option);
  23. switch (option)
  24. {
  25. case 1: Insert_Element();
  26. break;
  27. case 2: Delete_Element();
  28. break;
  29. case 3: Display_Queue();
  30. break;
  31. case 4: Empty_Queue();
  32. break;
  33. case 5: return 0; /*program ends*/
  34. }
  35.  
  36. } while (option != 5);
  37. }
  38.  
  39. void Insert_Element()
  40. {
  41. int num;
  42. if (rear < MAX - 1)
  43. {
  44. if (front == -1)
  45. /*when queue is initially empty */
  46. front = 0;
  47. printf("\n Enter the number to be inserted: ");
  48. scanf("%d", &num);
  49. rear = rear + 1;
  50. queue[rear] = num;
  51. }
  52. else
  53. {
  54. printf("\n Queue OverFlow Occured");
  55. }
  56. }
  57.  
  58. void Delete_Element()
  59. {
  60. int element;
  61.  
  62. if (front == -1 || front > rear)
  63. {
  64. printf("\n Queue Underflow occured.\n");
  65. return;
  66. }
  67. else
  68. {
  69. element = queue[front];
  70. printf("\n Element deleted from queue is : %d", element);
  71. front = front + 1;
  72. }
  73. }
  74.  
  75. void Display_Queue()
  76. {
  77. int i;
  78. if (front == -1 || front > rear)
  79. printf("\n No elements to display");
  80. else
  81. {
  82. printf("\n The queue elements are:\n ");
  83. for (i = front; i <= rear; i++)
  84. {
  85. printf("\t %d", queue[i]);
  86. }
  87. }
  88. }
  89.  
  90. void Empty_Queue()
  91. {
  92. /*Reset queue or Creates Empty queue*/
  93. front = -1;
  94. rear = -1;
  95. printf("\n New Queue created successfully.");
  96. }

Sample output :

>>> c program to implement queue operations <<<

 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 1

 Enter the number to be inserted: 10


 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 1

 Enter the number to be inserted: 20


 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 1

 Enter the number to be inserted: 30


 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 2

 Element deleted from queue is : 10

 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 3

 The queue elements are:
         20      30

 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice:

Download Source Code