2nd set of C programs


WRITE A PROGRAM TO PRINT A SUM OF FIRST N EVEN NUMBERS. (EX. = 2 + 4 + 6 + 8 + ……… + N)

#include<stdio.h>
#include<conio.h>

void main()
{
            int i=1, sum=0, even = 2;
            clrscr();
            printf(“\nENTER NUMBER      :           “);
            scanf(“%d” ,&num);
           
            while(i <= num)
            {
                        /* calculate sum of first N even numbers */
                        sum = sum + even;
                        /* next even number */
                        even = even + 2;
                        i = i + 1;
            }
            printf(“\nSUM OF FIRST %d EVEN NUMBERS IS         :           %d”, num, sum);

            getch();
}

WRITE A PROGRAM TO PRINT A TABLE OF CONVERSION FROM FAHRENHEIT TO CENTIGRADE.

#include<stdio.h>
#include<conio.h>

void main()
{
            float c, f;
           
            clrscr();
           
            printf(“\n\t_____________________”);
            printf(“\n\tFAHRENHEIT\tCENTIGRADE”);
            printf(“\n\t_____________________”);

            for(f=0.0; f<=250; f=f+25)
            {
                        /* convert farenheit into centigrade */
                        c = (f.32.0) / 1.8;
                        printf(“\n\t%10.2f\t%10.2f” ,f,c);
            }

            getch();
}

WRITE A PROGRAM TO FIND THE NUMBERS OF AND SUM OF ALL THE INTEGERS GREATER THAN 100 AND LESS THAN 200 THAT ARE DIVISIBLE BY 7.

#include<stdio.h>
#include<conio.h>

void main()
{
            int I, cnt=0, sum=0;
           
            clrscr();
           
            printf(“\n\nNUMBER DIVISIBLE BY 7 BETWEEN 100 AND 200 ARE …………\n\n”);
            for(i=100; i<=200; i++)
            {
                        /*check whether the number is divisible by 7 or not */
                        if(i%7=0)
                        {
                                    cnt = cnt + 1;
                                    /* calculate sum of these numbers */
                                    sum = sum + i;
                                    printf(“%d\t” ,i);
                        }
            }
            printf(“\n\nSUM OF THESE NUMBERS IS       :           %d” ,sum);
            printf(“\n\nNUMBERS OF NUMBER BETWEEN 100 AND 200 WHICH ARE DIVISIBLE BY 7 ARE : %d” ,cnt);
            getch();
}

IF A FIVE DIGIT NUMBER IS INPUT THROUGH THE KEYBOARD, WRITE A PROGRAM TO CALCULATE THE SUM OF DIGIT. (HINT : USE THE MODULUS OPERATOR ‘%’)

#include<stdio.h>
#include<conio.h>

void main()
{
            int num, sum=0, rem;
           
            clrscr();
           
            a;
            printf(“\n\nENTER ANY FIVE DIGIT NUMBER :           “);
            scanf(“%d”, &num);

            if(num <= 0)
            {
                        printf(“\nENTER POSITIVE NUMBER ONLY ……… “);
                        getch();
                        clrscr();
                        goto a
            }

            while (num > 0 )
            {
                        /* find out remainder */
                        rem = num % 10;
                        /* calculate sum of remainders */
                        sum = sum + rem;
                        /* remove last digit */
                        num = num / 10;
            }

            printf(“\n___________________”);
            printf(“\nSUM OF DIGIT OF THIS NUMBER IS = %4d” ,sum);
            printf(“\n___________________”);
           
            getch();
}


IF A FIVE DIGIT NUMBER IS INPUT THROUGH THE KEYBOARD, WRITE A PROGRAM TO REVERSE THE NUMBER.

#include<stdio.h>
#include<conio.h>

void main()
{
            long int num, rem, rev=0;
           
            clrscr();
           
            a          :           /* label */
            printf(“\n\nENTER ANY FIVE DIGIT NUMBER :           “);
            scanf(“%ld”, &num);

            if(num <= 0)
            {
                        printf(“\nENTER POSITIVE NUMBER ONLY ……… “);
                        getch();
                        clrscr();
                        goto a;             /* controls transfers to label a: */
            }

            while (num > 0 )
            {
                        /* find out remainder */
                        rem = num % 10;
                        /* reverse the number */
                        rev = (rev * 10) + rev ;
                        num = num / 10;
            }

            printf(“\n___________________”);
            printf(“\REVERSE OF THIS NUMBER IS = %6ld” ,rev);
            printf(“\n___________________”);
           
            getch();
}

IF A FIVE DIGIT NUMBER IS INPUT THROUGH THE KEYBOARD, WRITE A PROGRAM TO OBTAIN THE REVERSE NUMBER AND DETERMINE WHETHER THE ORIGINAL AND REVERSED NUMBER ARE EQUAL OR NOT.

#include<stdio.h>
#include<conio.h>

void main()
{
            long int num, rem, rev=0, NO;
           
            clrscr();
           
            a:        
            printf(“\n\nENTER ANY FIVE DIGIT NUMBER :           “);
            scanf(“%ld”, &num);
            no = num;
            if(num <= 0)
            {
                        printf(“\nENTER POSITIVE NUMBER ONLY ……… “);
                        getch();
                        clrscr();
                        goto a;            
            }

            while (num > 0 )
            {
                        /* find out remainder */
                        rem = num % 10;
                        /* reverse the number */
                        rev = (rev * 10) + rev ;
                        num = num / 10;
            }

            /* check whether the original & reversed number are same or not */
            if (no == rev)
            {
                        printf(“\n___________________”);
                        printf(“\ORIGINAL AND REVERSEDS  NUMBER ARE EQUAL ……………..”);
                        printf(“\n___________________”);
            }
            else
            {
                        printf(“\n___________________”);
                        printf(“\ORIGINAL AND REVERSEDS  NUMBER ARE NOT EQUAL ……………..”);
                        printf(“\n___________________”);
            }
           
            getch();
}

WRITE A PROGRAM TO PRINT ALL THE ASCII VALUES AND THEIR EQUIVALENT CHARACTERS USING A WHILE LOOP. THE ASCII VALUES VARY FROM 0 TO 255.

#include<stdio.h>
#include<conio.h>

void main()
{
            int i=0, j=1;
           
            clrscr();
           
            printf(“\n\n__________________________”);
            printf(“\nASCII VALUE\tCHARACTER”);
            printf(“\n________________________\n”);

            while (i<=255)
            {
                        /* printf ascii value and characters */
                        printf(“\n%d\t\t%c” ,i ,i);
                       
                        i = i + 1;          
                        j = j + 1;
           
                        /* check whether the screen is full or not */
                        if(j == 15)
                        {
                                    printf(“\n\nPress any key ……………………………….”);
                                    getch();
                                    clrscr();
                       
                                    printf(“\n\n_________________”);
                                    printf(“\nASCII VALUE\tCHARACTER”);
                                    printf(“\n\n_________________”);
           
                                    j = 1;
                        }
            }
            getch();
}


WRITE A PROGRAM TO GENERATE MULTIPLICATION TABLE. [1 TO 10]

#include<stdio.h>
#include<conio.h>

void main()
{
            int i, j;
           
            clrscr();
           
            for(i=1; i<=10; i++)
            {
                        printf(“\n\n__________________________”);
                        printf(“\nMULTIPICATION TABLE OF %d”, i”);
                        printf(“\n________________________\n”);

                        for(j=1; j<=10; j++)
                        {
                                    /* print multiplication table */
                                    printf(“\n         %d       *3d      = %3d”, i, j, i * j);
                        }
getch();
clrscr();
}
}

WRITE A PROGRAM TO FIND THE FACTORIAL VALUE OF ANY NUMBER ENTERED THROUGH THE KEYBOARD.

#include<stdio.h>
#include<conio.h>

void main()
{
            long unsigned int no, fact, i;
            char ch= ‘y’;

            while(ch== ‘y’ || ch== ‘y’)
            {
                        clrscr();
           
                        lbl;
                        printf(“\nENTER NUMBER      : “);
                        scanf(“%lu” ,&no);

                        /* check whether the entered number is positive or not */
                        if(no <= 0)
                        {
                                    printf(“\n\nENTER POSITIVE NUMBER ONLY ……….”);
                                    getch();
                                    clrscr();
                                    goto lbl;
                        }
                       
                        fact = 0;

                        for(i=1; i; i<=no i++)
                        {
                                    /* calculate factorial value */
                                    fact = fact * i;
                        }

                        printf(“\n\n__________________”);
                        printf(“\nFACTORIAL VALUE of %lu = %6lu” ,no ,fact);
                        printf(“\n__________________”);
                        printf(“\n\nDO YOU WANT TO CONTINUE? [y/n] : “);
                        fflush(stdin);
                        ch=getchar();
            }
}

TWO NUMBERS ARE ENTERED THROUGH THE KEYBOARD. WRITE A PROGRAM TO FIND THE VALUE OF ONE NUMBER RAISED TO THE POWER OF ANOTHER.

#include<stdio.h>
#include<conio.h>

void main()
{
            int a, b, i;
            float s;
            char ch= ‘y’;

            while(ch== ‘y’ || ch== ‘Y’)
            {
                        clrscr();

                        printf(“\n\nENTER FIRST NUMBER     :           “);
                        scanf(“%d” ,&a);
                        printf(“\nENTER SECOND NUMBER   :           “);
                        scanf(“%d” ,&b);

                        s = 1.0;
                        if(b < 0)
                        {
                                    /* calculate A raised to B for –tive number */
                                    b = b * -1;
                                   
                                    for(i=1; i<=b; i++)
                                    {
                                                s = s * a;
                                    }
                                    s = 1 / (float)s;
                        }
                        else
                        {
                                    /* calculate A raised to B for +tive number */
                                    for(i=1; i<=b; i++)
                                    {
                                                s = s * a;
                                    }          
                        }
           
                        printf(“\n\n_______________________”);
                        printf(“\n%d RAISED TO %d = %6.2f” ,a ,b ,s);
                        printf(“\n_______________________”);
                       
                        printf(“\n\nDO YOU WANT TO CONTINUE ? [y / n] : “);
                        fflush(stdin);
                        ch = getchar();
            }
}

WRITE PROGRAM TO COMPUTER AND PRINT THE LARGEST POWER OF ONE NUMBER WHICH IS LESS THEN OR EQUAL TO ANOTHER NUMBER. FOR EXAMPLE THE TWO NUMBERS ARE 3 AND 10, THE ANSWER IS 2 SINCE 3 RAISED TO 2 IS LESS THAN 10 BUT 3 RAISED TO 3 WHICH IS GREATER THAN 10.

#include<stdio.h>
#include<conio.h>

void main()
{
            int a, b, i, j=1, pol=1;
            clrscr();
            x:
            printf(“\nENTER FIRST NUMBER : “);
            scanf(“%d” ,&a);
            printf(“\nENTER SECOND NUMBER : “);
            scanf(“%d” ,&b);
            /* check whether the first number us greater then second or not */
            If(a > b)
            {
                        printf(“\n FIRST NUMBER SHOULD LESS THEN OR EQUAL TO SECOND . . . . . .  “)
                        getch( ) ;
                        clrscr ( ) ;
                        goto x;
            }
            for (I = 1 ; i<=j ; I ++)
            {
                        /* calculate power */
                        po = po * a;
                        if (po > b)
                                    break;
                        j++;
            }
            printf(“\n ANSWER IS %d BECAUSE” , j-1);
            printf(“\n%d RAISED TO %d IS LESS THAN %d “ , a,j-1,b)
            printf(“\n BUT %d RAISED TO %d IS GREATER THAN %d . . . . . . “ , a, j, b) :
getch ( );
}

ANY INTEGER IS INPUT THROUGH THE KEYBOARD, WRITE A PROGRAM TO FIND OUT ALL ITS DIVISORS.
EX.= the devisors of 20 are 1,2,4,5,10,20
#include<stdio.h>
#include<conio.h>

void main()
{
            int no, i;
            char ch=’y’ ;
            clrscr( );
while (ch== ‘y’ || c== ‘Y’)
{
                        clrscr( ) ;
                        printf (“\n\nENTER ANY NUMBER TO FIND IT’S DIVISORS : “) ;
                        scanf (“%d”, &no);
                        printf(“ \n\n THE DIVISORS OF %d ARE …………\n\n” ,no) ;
                        for(i=1; i<=no; i++)
                        {
/* find out devisors of a number */
if (no % i == 0)
                                    {          
                                                printf(“ %d      “ ,i) ;
                                    }
                        }
printf(“ \n\n\nDO YOU WANT TO CONTINUE? [y/n] . . . . . .  “ ) ;
                        fflush(stdin) ;
                        scanf(“%c” , &ch);
            }
}

WRITE A PROGRAM TO PRINT ALL PRIME NUMBERS FROM 1 TO 300. [HINT : USE NESTED LOOP, BREAK & CONTINUE].
#include<stdio.h>
#include<conio.h>

void main()
{
            int  i, j, flag;
            clrscr( );
            printf (“\n\n\t\t\t__________________ “) ;
            printf (“\n\t\t\t PRIME NUMBER BETWEEN 1 AND 300”);
            printf(“ \n\t\t\t_________________ \n\n\n”) ;
            for(i=1; i<=300; i++) /* outer loop */
{
flag=0 ;
            for(j=2; j<=I -1 ; j++) /* inner loop */
{
/* check whether the number is prime or not */
if (i % j == 0)
                                    {          
                                                flag=1 ;
                                    }
                        }
if(flag ! =1)
{
            printf(“%3d\t” ,i) ;
}
}
printf(“ \n\n\Press any key] . . . . . . . . . . .  “ ) ;
            getch( );
}

WRITE A PROGRAM TO DETERMINE AND PRINT THE SUM OF THE FOLLOWING HARMONIC SERIES FOR A GIVEN VALUE OF N.
1+1/2+1/3+………… + N
#include<stdio.h>
#include<conio.h>
void main()
{
            int no, i;
            double sum=0.0 , temp;
char ch=’y’ ;
            clrscr( );
while (ch== ‘y’ || c== ‘Y’)
{
                        clrscr( ) ;
                        printf (“\n\nENTER ANY NUMBER  : “) ;
                        scanf (“%d”, &no);
                        printf(“ \n\n\n\t\tADDITION OF FIRST %d TERM OF FOLLOWING SERIES.”  ,no) ;
                        printf(“ \n\n\n\t\t\t1 + 1/2 + 1/3 + 1/4 +. . . . . . . . . . ”) ;
                        for(i=1; I <=no; i++)
                        {
                                    /* calculate value of 1/2 , 1/3 , and so on */
                                    temp = 1/ (double) I /* type casting */
                                    /* calculate sum of these numbers */
                                    sum = sum + temp;
                        }
                        printf(“\n\n\t\t\t/t SUM = %If” , Sum);
                        printf(“\n\n DO YOU WANT TO CONTINUE? [y/n] : “)
                        fflush(stdin);
                        ch=getchar( );
            }
}

WRITE A PROGRAM TO ADD FIRST SEVEN TERMS OF THE FOLLOWING SERIES USING FOR LOOP.
1/1! + 2/2! + 3/3! + …………….
#include<stdio.h>
#include<conio.h>
void main()
{
            long int i,j, fact=1
            double sum=0.0 , a=0.0;
            clrscr( );
for(i=1; i<=7; i++) /* outer loop */
{
                        fact=1;
                        for( j=1; j<=I ;j++) /* inner loop */
                        {                      
                                    /* calculate factorial */
                                    fact = fact * j ;
                                   
}
/* calculate value of 1/1! , 2/2! And so on */
a = i (double) fact ;
/* calculate sum of these numbers */
                                    sum = sum + a;
                        }
                        printf(“\n\n\n\n\t       ADDITION OF FIRST SEVEN TERM OF THE FOLLOWING SERIES.”)
                        printf(“\n\n\t\t1/1! + 2/2 ! 3/3! + 4/4! . . . . . . . . . . . . . . .");
                        printf(“\n\n\t\t\t         SUM IS = % IF” , Sum);
                        printf(“\n\n\n Press any key . . . . . . . . . . . . .”);

getchar( );
            }

WRITE A PROGRAM TO PRINT FIBONACCI SERIES. THE FIRST 7 FIBONACCI NUMBERS ARE 1, 1, 2, 3, 5, 8, 13 . . .
#include<stdio.h>
#include<conio.h>
void main()
{
            long int num, i , pre, cur , new;
            char ch= ‘y’;
            while(ch== ‘y’ || ch== ‘Y’ )
{
clrscr( );
printf(“\n\n ENTER HOW MANY NUMBERS YOU WANT IN FIBONACCI SERIES : “) ;
scanf(“%ld” ,&num);

pre = 0;
cur = 1;
new = pre + cur ;
printf(“\n\nSERIES IS ………………………….. \n”);
printf(“\n%ld, %ld, %ld, pre, cur, new);

for(i=4; i<=num; i++)
{
            /* fond out fibonacci series */
            pre = cur;
            cur = new;
            new = pre + cur;
            printf(“ %ld, “ ,new);
}
printf(“………………………………….”);

printf(“\n\nDO YOU WANT TO CONTINUE [y/n] : “);
fflush(stdin);
scanf(“%c” ,&ch);
}
}

WRITE A PROGRAM TO FIND AND PRINT FIRST N POSITIVE INTEGERS WHOSE SQUARES ARE PALINDROMES. EX.- 11^2=121, 26^2=676………………..
#include<stdio.h>
#include<conio.h>
void main()
{
            long int num, rem, rev;
            long int j=1, i=1, sq, temp;

            clrscr();

            a :  /* label */
            printf(“\n\nENTER ANY NUMBER : “);
            scanf(“%ld”, &num);
           
            /* check whether the number is positive or not */
            if(num <= 0)
            {
                        printf(“\nENTER POSITIVE NUMBER ONLY……………..”);
                        getch();
                        clrscr();
                        goto a; /* control transfers to label a: */
            }

            printf(“\nFIRST %ld NUMBER WHOSE SQUARE ARE PALINDROMES………”, num);
            printf(“\n\n––––––––––––––––––––“);
            printf(“\Nnumber\t\tSQUARE”);
            printf(“\n––––––––––––––––––––“);

            while(j <= num)
            {
                        sq = 0;
                        rev = 0;
                        rem = 0;
                        /* find out square of the number */
                        sq = i * i;
                        temp = sq;
           
                        while( sq > 0 )
                        {
                                    rem = sq % 10; /* find out remainder */

                                    /* find out reverse number */
                                    rev = (rev * 10) + rem;
                                    sq = sq / 10;
                        }

                        /* check for equality */
                        if(temp == rev)           
                        {
                                    printf(“\n%ld\t\t%ld” ,i ,temp);
                                    j = j + 1;
                        }
            }
            printf(“\n\nPress any key……….”);
            getch();
}

WRITE A PROGRAM TO PRINT THE FOLLOWING DIGIT PYRAMIDS.
1                                     1
        2      2                           1    2    1
      3    3     3                   1   2    3    2    1

#include<stdio.h>
#include<conio.h>
void main()
{
            int row, I, j, sp=40, k;
           
            clrscr();

             /* ask for number of rows */
            printf(“\n\nENTER HOW MANY ROWS YOU WANT IN PYRAMID : “);
            scanf(“%ld”, &row);
            printf(“\n\n”);
            for(i=1; i<=row; i++ /* outer loop */
            {
                        for(k=1; k<=sp; k++) /* inner loop */
                        {
                                    /* leave the spaces */
                                    printf(“ “);
                        }
                        for(j=1; j<1; j++)
                        {
                                    printf(“%d      “ ,i”);
                        }
                        printf(“\n”);
           
                        /* reduce number of spaces */
                        sp = sp – 2;
            }
            getch();
           
            clrscr();

            printf(“\n\n\n”);
           
            sp = 40;

            for(i=1; i<=row; i++)
            {
                        for(k=1; k<=sp; k++)   
                        {
                                    /* leave the spaces */
                                    printf(“ “);
                        }
                        for(j=1; j<=i; j ++)
                        {
                                    /* print in forward direction */
                                    printf(“%d “ ,j);
                        }
                        K = j-2;
                        for( ; k>=1; k -)
                        {
                                    /* print in backward direction */
                                    printf(“%d “ ,k);
                        }
                        printf(“\n”);
                        /* reduce number of spaces */
                        sp = sp – 2;
            }
            getch();
}

WRITE A PROGRAM TO PRINT FLOYED’S TRIANGLE.

1
2              3
4              5              6

#include<stdio.h>
#include<conio.h>
void main()
{
            int row, i, j, k=1;
           
            clrscr();

            printf(“\n\aENTER HOW MANY ROWS YOU WANT IN TRIANGLE : “);
            scanf(“%d”, &row);
            printf(“\n\n”);

            for(i=1; i<=row; i++ /* outer loop */
            {
                        for(j=1; j<=i; j++) /* inner loop */
                        {
                                    /* print floyed’s triangle */
                                    printf(“\t%d” ,k “);
                                    k = k + 1;
                        }                      
            }
            getch();
}

WRITE A PROGRAM TO PRODUCE THE FOLLOWING OUTPUT.

1
0              1             
1              0              1
0              1              0              1                             

#include<stdio.h>
#include<conio.h>
void main()
{
            int row, I, j, j, k;
           
            clrsr();
           
            printf(“\n\nENTER HOW MANY ROWS YOU WANT IN PYRAMID : “);
            scanf(“%d” ,&row);
            printf(“\n\n”);

            for(i=1; i<=row; i++) /* outer loop */
            {
                        /* check the position of a number in triangle */
                        if(i%2 != 0)
                                    k =1;
                        else
                                    k = 0;
                        for(j=1; j<=I; j++) /* inner loop */
                        {
                                    printf(“\t%d” ,k);
                                    if(k==1)
                                                k=0;
                                    else
                                                k=1;
                        }
                        printf(“\n”);
            }
            getch();
}

No comments

Post your comments

Powered by Blogger.