Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write a program to display even and odd numbers from 1 and 100.

Object II. To write a program to display the factorial of a given number.

Object III. To write a program to display the Fibonacci series.

Object IV. To write a program to find whether the given number is prime.

Download this full program | Lab 5 | in pdf format

Background Theory:

The for statement: This is the most commonly used looping statement in C. This statement includes an expression that specifies an initial value for an index, another expression that determines whether or not the loop is continued, and a third one that allows the index to be modified at the end of each pass. The for loop allows us to specify three things about a loop in a single line:

a)     Setting a loop counter to an initial value.

b)     Testing the loop counter to determine whether its value has reached the number of repetitions desired.

c)     Increasing the value of loop counter each time the program segment within the loop has been executed.

The general form of for statement is as under:

 

for (initialize counter; test counter; increment counter)

{

       body of the loop;

}

The looping action continues as long as the value of expression 2 ie test counter is not zero, that is, as long as the logical condition represented by expression 2 is true.

Expression 1 or initialize counter is an assignment expreesion, exp 2 or test counter is a logical counter and the final exp 3 or increment counter is the unary assignment counter. Exp 1 initialize index to control looping action and ex2  checks whether the loop is continued.

 The for statement, like the while and the do-while statements, can be used to carry out looping actions where the number of passes thru the loop is not known in advance. As a rough rule of thumb, while loops are generally used when the number of passes is not known in advance, and for loops are used when the number of passes is known in advance.

 Fibonacci Series: The Fibonacci numbers form an interesting sequence in which each number is equal to the sum of the previous two numbers. In other words,

Fi =Fi-1+Fi-2  where Fi refers to the ith Fibonacci number. The first two numbers are defined as equal to1; ie F1 = F2.

 

Prime Numbers: To write a program to check whether an entered number is prime, first the number should be checked for divisibility, serially from 2 till n-1. 

 

Code:

  //to display odd and even numbers between 1 and 100//

 

    #include <stdio.h>

    #include <conio.h>

    void main()

    {

            int i, j;

            clrscr();

            printf("The odd numbers are\n\n");

            for(i=1; i<=100; i+=2)

            printf("%d\t", i);

 

            printf("\n\nThe even numbers are\n\n");

            for(j=2, j<=100, j+=2)

            printf("%d\t", j);

 

            getch();

    }

 // program to display factorial of a given number//

 

#include <stdio.h>

#include <conio.h>

#include <math.h>

 

void main()

{

            long int d, fact, n;

            clrscr();

            printf("input a number= ");

            scanf("%ld", &n);

            fact=1;

            for(d=1; d<=n; d++)

            fact=fact*d;

            printf("factorial = %ld", fact);

            getch();

}

 // program to display Fibonacci Series of the entered number//

 

#include<stdio.h>

#include<conio.h>

void main()

{

Long int s, i, j, k, n;

clrscr();

printf("Enter the no of terms of series to be displayed: ");

scanf("%ld", &n);

i=0;

j=1;

printf("%ld\t%ld", i, j);

for(k=1;k<=(n-2);k++)

{          

s=i+j;

i=j;

j=s;

printf("\t%ld",s);

}

getch();

}

 

 // program to find whether the given number is prime //

 

            #include<stdio.h>

#include<conio.h>

 

void main()

{

                        int i, n, rem;

                        clrscr();

                        printf("Enter the number:\t");

                        scanf("%d",&n);

                        for (i=2; i<=n-1; i++);

                        {

                                    rem=(n%2);

                                    if(rem==0)

                       

                                    printf("The number is not prime.");

                                   

                        }

                        printf("The number is prime.");

                        getch();

}

To Download this full program | Lab 5 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com