04 February 2022

C Practical Unit 3

  

Assignment 3

 

1.Write a program to find sum of N numbers.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        long int sum=0 , no , i=1;

        clrscr();

        printf("Enter Number :  ");

        scanf("%ld",&no);

 

        while(i<=no)

        {

                sum = sum + i;

                i++;

        }

 

        printf("\n\nYour sum is %ld",sum);

        getch();

}

 

 

1      Write a program to find factorial of given number.

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int fact=1 , no ,i ;

        clrscr();

        printf("Enter Number :  ");

        scanf("%d",&no);

 

        for(i=1;i<=no;i++)

        {

                fact=fact*i;

        }

        printf("\n\nFactorial of %d  is  %d",no,fact);

 

        getch();

}

 

 

2      Write a program to find maximum from given N inputs by user.

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int n1 , n2 , n3 ;

        clrscr();

        printf("Enter value of 1st number :  ");

        scanf("%d",&n1);

 

        printf("\nEnter value of 2nd number :  ");

        scanf("%d",&n2);

 

        printf("\nEnter value of 3rd number :  ");

        scanf("%d",&n3);

 

        if(n3>=n2)

        {

                if(n2>=n3)

                {

                        printf("\nMaximum number is %d",n2);

                }

                else

                {

                        printf("\nMaximum number is %d",n3);

                }

        }

        else

        {

                if(n1>=n2)

                {

                        printf("\nMaximum number is %d",n1);

                }

                else

                {

                        printf("\nMaximum number is %d",n2);

                }

        }

 

 

        getch();

}

 

3      Write a program to find reverse of a given number.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        long int no , revno=0 , i , rem ;

        clrscr();

 

        printf("Enter Number  :  ");

        scanf("%ld",&no);

 

        while(no>0)

        {

                rem = no % 10 ;

                revno = revno * 10 + rem ;

                no = no / 10 ;

        }

 

        printf("\n\nYour reverse number is :  %ld ",revno);

 

        getch();

}

 

4      Write a program to find sum of the digits entered by the user.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int sum=0 , no , rem ;

        clrscr();

 

        printf("Enter Number :  ");

        scanf("%d",&no);

 

        while(no>0)

        {

                rem = no % 10 ;

                sum = sum + rem ;

                no = no / 10 ;

        }

        printf("\n\nSum of digit is %d",sum);

        getch();

}

 

 

5      Write a program to generate Fibonacci series up to N numbers.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    int a=0 , b=1 , c ,no , i ;

    clrscr();

 

    printf("Enter Limit  : ");

    scanf("%d",&no);

 

    for(i=1;i<=no;i++)

    {

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

            c=a+b;

            a=b;

            b=c;

    }

    getch();

}

 

6      Write a program to find GCD and LCM of given 2 numbers.

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    int n1 , n2 , i , gcd , lcm ;

    clrscr();

 

    printf("\nEnter 1st Number :  ");

    scanf("%d",&n1);

 

    printf("\nEnter 2nd Number :  ");

    scanf("%d",&n2);

 

    for(i=1 ; i <= n1 && i <= n2 ; i++)

    {

            if(n1%i==0 && n2%i==0)

            {

                    gcd = i;

            }

    }

    printf("\n\nGcd of %d and %d  =  %d",n1,n2,gcd);

    lcm=(n1*n2)/gcd;

 

    printf("\n\nLcm of %d and %d  =  %d",n1,n2,lcm);

    getch();

 

}

 

7      Write a program to find the sum of first 100 odd nos. and even nos.

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int i , n1 , n2 ;

        clrscr();

 

        printf("\n\t1,2,3,4............................................100");

        printf("\n\n\n\t\t\t\tEven Numbers :\n\n");

 

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

        {

                if(i%2==0)

                {

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

                }

        }

 

        printf("\n\n\n\t\t\t\tOdd Numbers :\n\n");

 

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

        {

                if(i%2!=0)

                {

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

                }

        }

 

 

        getch();

}

 

8      Write a program to check whether given number by the user is Palindrome or no.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    long int no , rem , revno=0 , temp ;

    clrscr();

 

    printf("\n\nEnter number \n\nYou want to check palindrome or not :   ");

    scanf("%ld",&no);

    temp = no ;

 

    while(no>0)

    {

            rem = no % 10 ;

            revno = revno * 10 + rem ;

            no = no / 10 ;

    }

    if(temp == revno)

    {

            printf("\n\n\n%ld is Palindrome number ",temp);

    }

    else

    {

            printf("\n\n\n%ld is not Palindrome number ",temp);

    }

    getch();

}

 

 

9      Write a program to check whether the given number is Prime or not.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    int i , prime=0 , no ;

    clrscr();

    printf("Enter Number :  ");

    scanf("%d",&no);

 

    for(i=1 ; i<=no ; i++)

    {

            if(no%i==0)

            {

                    prime++;

            }

    }

    if(prime==2)

    {

            printf("\n\n %d  is  a  Prime  Number ",no);

    }

    else

    {

            printf("\n\n%d  is not  a  Prime  Number ",no);

    }

    getch();

}

 

10                       Write a program to print all the prime      numbers ranging from 50 to 100.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    int i , j , prime = 0 ;

    clrscr();

    printf("\n\nPrime Numbers From 50 to 100\n\n\n");

    for(i=50 ; i<=100 ; i++)

    {

            prime=0;

 

            for(j=1 ; j<=i ; j++)

            {

                    if(i%j==0)

                    {

                            prime++;

                    }

            }

 

                    if(prime==2)

                    {

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

                    }

    }

    getch();

}

 

 

 

12 Write a C program to find x1+x2+x3+x4+ ….+xn.

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    int i , no , sum=0 , x ;

    clrscr();

 

    printf("Enter a Number  :  ");

    scanf("%d",&no);

    printf("Enter value of x :  ");

    scanf("%d",&x);

    printf("\n\n");

 

    for(i=1 ; i<no ; i++)

    {

            printf("x%d + ",i);

            sum=sum+(x*i);

    }

    printf("x%d",no);

 

    sum=sum+(x*no);

 

    printf("  ==> %d",sum);

 

    getch();

}

 

11                       Write a C program to find 1+1/2+1/3+1/4+ …+1/n.

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

    int no , i , sum = 0 ;

    clrscr();

 

    printf("Enter a Number :  ");

    scanf("%d",&no);

 

    for(i=1 ; i<no ; i++)

    {

            printf("1/%d + ",i);

            sum=sum+(1/i);

    }

    printf("1/%d",no);

    sum=sum+(1/no);

 

    printf("  ==>  %d",sum);

    getch();

 

}

 

14 Write a program to print following pyramid.  

   *   

    * * 

  * * * 

* * * *

 

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int i , j , no ;

        clrscr();

 

        printf("Enter  Limit  of Pyramid  :   ");

        scanf("%d",&no);

        printf("\n\n");

        for(i=1 ; i<=no ; i++)

        {

                for(j=i ; j<no; j++)

                {

                        printf(" ");

                }

                        for(j=1 ; j<=i ; j++)

                        {

                                printf("*");

                        }

                                for(j=1 ; j<i ; j++)

                                {

                                        printf("*");

                                }

                                printf("\n");

        }

        getch();

}

 

15 Write a program that accepts an integer N, if the integer N = 4, then print the pyramid :      

      1    

   121 

 12321

123432

 

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int no , i , j ;

        clrscr();

 

        printf("Enter limit of Pyramid  :  ");

        scanf("%d",&no);

        printf("\n\n")

        for(i=1 ; i<=no ; i++)

        {

 

                for(j=i ; j<=no ; j++)

                {

                        printf(" ");

                }

 

                        for(j=1 ; j<=i ; j++)

                        {

                                printf("%d",j);

                        }

 

                                for(j=1 ; j<=i-1 ; j++)

                                {

                                        printf("%d",i-j);

 

                                }

                                printf("\n");

        }

        getch();

}

 

 

16 Write a program that accepts an integer N, if the integer N = 4,then print the pyramid :

4 4 4 4

3 3 3

 2 2

  1

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int i , j , no ;

        clrscr();

 

        printf("Enter Limit of Pyramid  :   ");

        scanf("%d",&no);

 

        printf("\n\n\n");

 

        for(i=no ; i>=1 ; i--)

        {

                for(j=i ; j<=i ; j++)

                {

                        printf(" ");

                }

                        for(j=1 ; j<=i ; j++)

                        {

                                printf("%d",i);

                        }

                        printf("\n");

        }

        getch();

}

17 Write a program to Print following:      

     A 

   B C  

 D E F

G H I J

18 Write a program to Print following:      

      1 

    0 1  

  1 0 1 

0 1 0 1

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int i , j , no ;

        clrscr();

 

        printf("Enter a Number  :  ");

        scanf("%d",&no);

 

        for(i=1 ; i<=no ; i++)

        {

                for(j=no ; j>=i ; j--)

                {

                        printf(" ");

                }

 

                        for(j=1 ; j<=i ; j++)

                        {

                                printf("%d",(i+j+1)%2);

                        }

                        printf("\n");

        }

        getch();

}

 

19 Write a program to Print following:    

      1 

    0 1

  0 1 0 

1 0 1 0

20 Write a program to Print following: 

       A   

    ABA 

  ABCBA

ABCDCBA

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

        int i , j , no ;

        clrscr();

 

        printf("Enter  Limit  :  ");

        scanf("%d",&no);

        printf("\n\n");

        for(i=1 ; i<=no; i++)

        {

                for(j=i ; j<=no ; j++)

                {

                        printf(" ");

                }

                        for(j=1 ; j<=i ; j++)

                        {

                                printf("%c",'A'+j-1);

                        }

 

                                for(j=i-1 ; j>=1 ; j--)

                                {

                                        printf("%c",'A'+j-1);

                                }

                                printf("\n");

 

        }

        getch();

}


No comments:

Post a Comment