-->

Friday, December 12, 2014

Program to find sum of squares of first N natural numbers in C

Let us design the algorithm and write the program to find the sum of the following series:
12+22+32+42+....................................................N2
We know the program segment to add all the numbers from 1 to N. The program segment is:
sum=0;
for(i=1; i<=n; i++)
{

sum = sum+i;

}
But, to add 12 , 22 , 32 , 42 , ...............N2    it is necessary to replace the statement:
sum = sum+i;

by the statement:

sum = sum+i*i;

The algorithm and flowchart to find the sum of squares of all natural numbers are as follows:

Algorithm : SERIESSUM

Step 1: [Input the number of terms]
Read: n
Step 2: [Initialization]
sum =0
Step 3: [Find the sum of all terms]
for i=1 to n in steps of 1 do
sum = sum + i*i

[End of for i]

Step 4: [Output the sum]
Write : sum
Step 5: [Finished]
Exit.

The complete program to find the sum of the given series is shown below:
Example : Program to add the series  12+22+32+42+....................................................N2

PROGRAM

#include<stdio.h>
main( )
{
int n, i, sum;
printf("Enter the number of terms \n");
scanf("%d",&n);
sum =0;
for(i=1; i<=n; i++)
{
sum = sum+(i*i);
}
printf("Sum of squares of numbers = %d",sum);
}

TRACING

Execution starts from here:
Non-executable statement
Input
Enter the number of terms
5
Computations
sum=0
              i= 1 2 3 4 5
sum =0+12+22+32+42+52
Output
Sum of squares of numbers =55

Thursday, December 11, 2014

Program to simulate calculator using switch in C

The input is the expression of the form (a op b) where a and b are the operands and op is an operator. For example 10+20. Based on the operator, the operation is performed and the result is displayed. The complete algorithm along with the flowchart is shown below:

Algorithm: CALCULATOR

Step 1: [Read expression of from (a+b)]
Read: a, op, b
Step 2: [perform the required operation]
switch(op)

case "+" : res = a+b
case "-" : res = a-b
case "*" : res= a*b
case "/" : if (b==0)
Write : "Divide by 0"
Exit
else

res = a/b
[End of if]
default : Write: ' Invalid operator'
Exit
[End of switch]
Step 3: [Output the result]
Write: res
Step 4: [Finished]
Exit.


The equivalent C program to perform various operations such as addition, subtraction, multiplication and division is shown below:

Program

#include<stdio.h>
#include<process.h>
main( )
{
int a, b, res;
char op; /* can be +, - , *,, or / */
printf("Enter the expression");
scanf("%d%c%d",&a, &op, &b);
switch(op)
{
case '+' :
res = a+b;
break;
case '-':
res= a-b;
break;
case '*':
res=a*b;
break;
case '/' :
if(b!=0)
res=a/b;
else
{
printf("Div by 0\n");
exit(0);
}
default:
printf("Invalid operator \n");
}
/* end of switch statement */
printf("%d %c %d = %d\n",a,op,b,res);  /* Display the result */
}

Tuesday, December 9, 2014

Program to find numbers within range n1 and n2 in C

Just now, we have seen, how to check whether a given number m is prime or not. Let us modify this program to generate prime numbers within the range n1 and n2.
Procedure: Assume m varies from n1 to n2. If m is prime let us display it. Otherwise, take the next number in the range and check for prime number. If it is prime, display it; otherwise, take the next number and repeat the procedure. Thus, all prime numbers are displayed in the range n1 to n2.

So, when m is prime, in place of displaying the message " The number is prime", let us print the prime number. If m is not prime, instead of displaying the message "The number is not prime", take the next number in the range. If these modifications are done to the program discussed in the previous section, then prime numbers within the range are generated. The modified program is shown below:

#include<stdio.h>
void main( )
{
int n, n1, n2, i, count, prime;
printf("Enter n1 and n2:\n");
scanf("%d%d",&n1,&n2);
count=0;
printf("Primes between %d to %d are: \n", n1, n2);
for (n=n1; n<=n2; n++)
{
prime =1; /* Assume m is prime */

for (i=2; i<=n/2; i++)
{
if(n%i == 0)
{
/* n is not prime */
prime =0;
break;
}
}
/* if m is not prime, take next m, */
if(prime  == 0) continue;
/* print the prime number */
printf("%d",n);
count++; /* Update the counter */
}
printf("No. of primes = %d\n",count);
}

Tracing 

Input 
Enter n1 and n2:
2 9
Computations
count = 0
m=2 3 4 5 6 7 8 9

4 6 8 9
are not prime numbers and they are skipped

Output
2 3 5 7
No. of primes = 4

Program to check whether a number is prime or not in C

Before we check whether a number is prime or not, we should know the answer for the question "What is a prime number ?"
Definition : A number which is only divisible by 1 and itself is a prime number. For example, numbers such as 1, 2, 3, 5, 7, 11 etc. are not divisible by any numbers other than 1 and themselves. So, they are all prime numbers. But, the numbers such as 4, 6, 8, 16 etc., are divisible by 2 and hence they are non-prime numbers. The number 9 is divisible by 3 and hence it is also no-prime number. Now, let us see " How to check whether a given number is prime or not?"

Procedure : Consider the number 16. It is not divisible by 9, 10, 11, 12, 13, 14 and 15 which are all greater than 16/2. So, the first point to remember is:
  • A number m cannot be divisible by a number which is greater than m / 2.
In our case m is 16. It is divisible by the numbers 2, 4 and 8 which are less than or equal to 16/2.
The second point to remember is:
  • A number m may be only divisible by either 2 or 3 or 4 or 5 ..... or m/2.
So, to check whether m is prime or not, it is sufficient to divide m by the numbers 2, 3, 4....m/2 one after the other. During this process, if the remainder is zero, the number m is non-prime.
But, even after dividing m by all the numbers from 2 to m/2, if the remainder is still not zero, the number m is prime.
The partial code is as follows 
for (i=2; i<=m/2; i++)
{

if(m%i == 0)
{
printf("The number is non-prime");
exit(0);
}

}
printf("The number is prime\n");

The algorithm and the equivalent flowchart are shown below:
Algorithm : PRIME
[This algorithm checks whether the given number is prime or not.]

Step 1: [Enter the number]
Read: n
Step 2: [Check for prime number]
for i=2 to n/2 in step 1
if(n%i == 0)
Write : 'Number is non-prime'
Exit.
[End of if]
[End of for]
Step 3: [Output prime]
Write: 'number is prime'
Step 4: [finished]
Exit.

The complete program to check whether the given number is prime or not is shown below:
Example : Program to check whether a number is prime or not.

Program
#include<stdio.h>
#include<stdlib.h>
main( )
{
int n, i;
printf("Enter a number:\n");
scanf("%d",&n);
for(i=2; i<=n/2; i++)
{
if(n%i == 0)
{
printf("%d is non-prime",n);
exit(0);
}
}
printf("%d is prime",n);
}

TRACE 1

Input 
Enter a number
13
Computations
i=2 3 4 5 6
13/2  13/3  13/4  13/5 13/6
2,3,4,5,6 can't divide 13
Output
13 is prime

TRACE 2

Input
Enter a number: 
9
Computations
i=2 3 4
9/2  9/3

Output
9 is non-prime

Sunday, December 7, 2014

Adding Animation to Web Page Part-2: jQuery Effects

JQuery library have all the effects for adding animation as discussed in earlier article. How to show or hide an element, sliding effects on an element or perform any custom animation on web-page have been discussed in my previous discussion.

Wherever these function didn’t work on the page then there may be some script error or you are defining the function with wrong syntax. Whatever the error be, jQuery library is there for help any type of definition, syntaxes or any example.

User can apply fading on an element, and make that element out of visibility, by using below functions specially created for this functionality. Reading an element’s description and do some practical with them are two different tasks. So just read and perform practical to clarify all these effects.

  • fadeIn(): used to fade in a hidden element on the web-page.
  • fadeOut(): used to fade out a hidden element on the web-page.
  • fadeTo(): allows fading to a given opacity.
  • fadeToggle(): used to toggle between fadeIn() and fadeOut() methods alternatively.

These methods have some parameters like speed, callback and opacity as per their requirements. Below are the examples for above methods:

$(".divEmployee").fadeIn(“slow”); or $(".divEmployee").fadeIn(4500);
$(".divEmployee").fadeOut(“slow”); or $(".divEmployee").fadeOut(4500);
$(".divEmployee"). fadeToggle (“slow”); or $(".divEmployee"). fadeToggle (4500);
$(".divEmployee"). fadeTo("slow",0.5);

Stop()

This method is used to stop all the effects before it is finished and revert back to the previous stage. It can work with all effects function e.g. hide/show, slideDown/slideUp, fadeIn/fadeOut or any custom animation.

$(".divEmployee").stop();

This function will stop the effect applied on specified element and revert back. If the element is hidden and perform a show() function then it will stop and make that hidden back.

Stop() function may take two parameters stopAll (to clear animation queue, default it will only stop the active animation) and goToEnd (to specify whether complete the current animation or not).

We will discuss about callback feature of these functions means what to do after animation has finished.

Friday, December 5, 2014

Adding Animation to Web Page: jQuery Effects

JQuery library have all the effects for adding animation user can imagine/think about like to animate, fade, toggle, show, hide and etc. All these animation have their own functions according to their job.

jQuery methods allow users to easily use these effects with minimum configuration. In other context we can show or hide any element on the page by using these effects to make better UI. These animation can be done in any event of an existing element. I have listed some of these methods including a brief description:

  • hide(): will hide the related element.
  • show(): will show related element.
  • toggle(): show of hide related element. (If element is shown then it will hide)
  • slideDown()/slideUp(): show or hide related element with sliding motion.
  • slideToggle(): show of hide related element with sliding motion.
  • animate(): this method is used for custom animation.

These methods have also some parameter to be used with e.g. speed (represents predefined speed among slow, normal and fast) and callback (optional - represent a function to be executed after animation completion). Whether the parameter is required or optional is function dependent, may be change in other definition.

$(“#button”).hide(1000); will hide the button as per the given speed
$(“#button”).show(1000); will show the button as per the given speed

Consider following code:

$(".divEmployee").toggle('slow', function(){
             $(".txtMessage").text(successfully done');
          });

It will show/hide the whole div tag and after completion given message will show on the element provided. Following are some examples about how to use these animation methods. Write these according to element on you page and look out the effects.

$(".divEmployee").slideDown('slow');
$(".divEmployee").slideUp('slow');
$(".divEmployee").slideToggle('slow');

The custom animation animate() method will require all the specified parameters to complete the animation. Lookout the following example of custom animation:

  $( "#divEmployee" ).animate({
    opacity: .25,
    height: "toggle"
  }, 4500, function() {
  });

Run this jQuery code and check the animation, don’t forget to confirm the element id on your page. This code will shrinks the height of div to hide it. We will learn more about these effects and how this can stop by using jQuery function.

Program to check for leap year in C

Before writing the program let us see "What is a leap year?"
Definition: A year which satisfies one of the following two cases:

  • It is divisible by 4 and should not be divisibly by 100 
  • Divisible by 400
If one of the condition is true, it is a leap year. But, if both condition are false, the year is not a leap year. The following table shows whether a year is a leap year or not along with the reasons:

Leap year Table

The partial code for this can be written as follows:

if(year % 4 == 0 && year % 100 !=0) || (year % 400 == 0)
printf ("%d is a leap year \n",year);
else
printf("%d is not a leap year \n");

The complete program is shown below:

Program
#include<stdio.h>
void main( )
{
int year;
printf("Enter the year");
scanf("%d",&year);
if((year %4==0 && year %100!=0 ) !! (year %400 == 0))
printf("%d is a leap year ",year);
else
printf("%d is not a leap year",year);
}
© Copyright 2013 Computer Programming | All Right Reserved