• Click below to download Unit 3 programs in pdf file: -




UNIT 3



/* 1 Write a program to find sum of N numbers*/

 

#include <stdio.h>

int main() {

    int n, i, sum = 0;

 

    printf("Enter a positive integer: ");

    scanf("%d", &n);

 

    for (i = 1; i <= n; ++i) {

        sum += i;

    }

 

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

}

 

 

/* 2 Write a program to find factorial of given number*/

 

#include<stdio.h>

int main()

{

 int i,fact=1,number;

 

 printf("Enter a number: ");

  scanf("%d",&number);

 

    for(i=1;i<=number;i++){

      fact=fact*i;

  }

  printf("Factorial of %d is: %d",number,fact);

}

 

 

/* 3 Write a program to find maximum from given N inputs by user */

 

#include <stdio.h>

 

int main()

{

  int i,num,n,large=0;

 

  printf("How many numbers: ");

  scanf("%d",&n);

 

  for(i=0; i<n; i++)

  {

    printf("\nEnter number %d: ",i+1);

    scanf("%d",&num);

    if(num>large)

    large=num;

  }

 

  printf("\n\nThe Largest Number is %d",large);

}

 

 

/* 4 Write a program to find reverse of a given number */

 

#include<stdio.h>

 

int main()

{

int n, reverse=0, rem;

 

  printf("Enter a number: ");

  scanf("%d", &n);

 

  while(n!=0)

  {

     rem=n%10;

     reverse=reverse*10+rem;

     n/=10;

  }

  printf("Reversed Number: %d",reverse);

}

 

 

/* 5 Write a program to find sum of the digits entered by the user. */

 

#include<stdio.h>

 int main()

{

    int n,sum=0,m;

 

    printf("Enter a number:");

    scanf("%d",&n);

 

    while(n>0)

{

        m=n%10;

        sum=sum+m;

        n=n/10;

}

 

 

 

/* 6 Write a program to generate Fibonacci series up to N numbers */

 

#include<stdio.h>

int main()

    {

     int n1=0,n2=1,n3,i,number;

 

     printf("Enter the number of elements:");

     scanf("%d",&number);

 

     printf("\n%d %d",n1,n2);

 

     for(i=2;i<number;++i)

        {

          n3=n1+n2;

          printf(" %d",n3);

          n1=n2;

          n2=n3;

        }

 }

}

 

 

/* 7 Write a program to find GCD and LCM of given 2 numbers */

 

#include <stdio.h>

 

int main()

{

    int num1, num2, gcd, lcm, count = 1, small;

 

    printf("Enter 2 integer numbers\n");

    scanf("%d%d", &num1, &num2);

 

    small = (num1 < num2) ? num1 : num2;

 

    while(count <= small)

    {

        if(num1 % count == 0 && num2 % count == 0)

        {

            gcd = count;

        }

        count++;

    }

 

    lcm = ( num1 * num2 ) / gcd;

 

    printf("GCD = %d\nLCM = %d\n", gcd, lcm);

 

    return 0;

}

 

 

/* 8 Write a program to find the sum of first 100 odd nos. and even nos */

 

#include <stdio.h>

 

void main()

{

    int i, num, odd_sum = 0, even_sum = 0;

 

    printf("Enter the range\n");

    scanf("%d", &num);

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

    {

        if (i % 2 == 0)

            even_sum = even_sum + i;

        else

            odd_sum = odd_sum + i;

    }

    printf("Sum of all odd numbers  = %d\n", odd_sum);

    printf("Sum of all even numbers = %d\n", even_sum);

}

 

 

/* 9 Write a program to check whether given number by the user is

Palindrome or not. */

 

#include <stdio.h>

int main() {

  int n, reversed = 0, remainder, original;

 

    printf("Enter an integer: ");

    scanf("%d", &n);

    original = n;

 

    while (n != 0) {

        remainder = n % 10;

        reversed = reversed * 10 + remainder;

        n /= 10;

    }

 

    if (original == reversed)

        printf("%d is a palindrome.", original);

    else

        printf("%d is not a palindrome.", original);

 

    return 0;

}

 

 

/* 10 Write a program to check whether the given number is Prime or not */

 

#include <stdio.h>

 

int main() {

 

  int n, i, flag = 0;

  printf("Enter a positive integer: ");

  scanf("%d", &n);

 

  if (n == 0 || n == 1)

    flag = 1;

 

  for (i = 2; i <= n / 2; ++i) {

 

    if (n % i == 0) {

      flag = 1;

      break;

    }

  }

 

  if (flag == 0)

    printf("%d is a prime number.", n);

  else

    printf("%d is not a prime number.", n);

 

  return 0;

}

 

 

/* 11 Write a program to print all the prime numbers ranging from 50 to

100. */

 

#include <stdio.h>

 

int main()

{

  int i, a = 50, count;

 

 

  while(a <= 100)

  {

    count = 0;

    i = 2;

    while(i <= a/2)

    {

      if(a%i == 0)

      {

        count++;

            break;

      }

      i++;

    }

    if(count == 0 && a != 1 )

    {

            printf(" %d ", a);

    }

    a++;

  }

  return 0;

}

 

 

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

 

#include<stdio.h>

#include<math.h>

 

void main()

{

    int i,n,x,ans;

 

    printf("\n enter last number : ");

    scanf("%d",&n);

 

    printf("\n enter value of x : ");

    scanf("%d",&x);

 

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

    {

                    ans=pow(x,i);

                    printf("\n%d",ans);

    }

}

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

 

 

#include<stdio.h>

int main()

{

    int num,i,sum=0;

 

        printf("Input any number: ");

        scanf("%d",&num);

 

    printf("1 + ");

 

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

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

 

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

        sum = sum + i;

 

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

    printf("\nSum = 1/%d",sum+1/num);

 

return 0;

}

 

 

/* 14 Write a program to print following pyramid.

   *

  * *

 * * *

* * * *

 

*/

 

#include <stdio.h>

 

int main()

{

   int i, space, rows, k = 0;

 

   printf("Enter the number of rows: ");

   scanf("%d", &rows);

 

   for (i = 1; i <= rows; ++i, k = 0) {

      for (space = 1; space <= rows - i; ++space) {

         printf("  ");

      }

 

      while (k != 2 * i - 1) {

         printf("* ");

         ++k;

      }

      printf("\n");

   }

   return 0;

}

 

 

/* 15 Write a program that accepts an integer N, if the integer N = 4,

then print the pyramid :

   1

  121

 12321

1234321

 

*/

 

#include<stdio.h>

 

int main()

{

    int i,j,row;

 

    printf("Enter number of rows: ");

    scanf("%d",&row);

 

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

    {

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

        {

            printf(" ");

        }

 

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

        {

            printf("%d", j);

        }

 

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

        {

            printf("%d", j);

        }

        printf("\n");

    }

 

    return 0;

}

 

 

/* 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>

 

void main()

{

   int i,j,spc,rows,k;

 

   printf("Input number of rows : ");

   scanf("%d",&rows);

 

   spc=rows+4-1;

 

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

   {

         for(k=spc;k>=1;k--)

            {

              printf(" ");

            }

 

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

               printf("%d ",i);

 

            printf("\n");

    spc++;

   }

}

 

 

/* 17 Write a program to Print following:

   A

  B C

 D E F

G H I J

 

*/

 

#include <stdio.h>

 

int main()

{

   int i, space, rows, k = 0;

   char alphabet='A';

 

   printf("Enter the number of rows: ");

   scanf("%d", &rows);

 

   for (i = 1; i <= rows; ++i, k = 0) {

      for (space = 1; space <= rows - i; ++space) {

         printf("  ");

      }

 

      while (k != 2 * i - 1) {

         printf("%c ",alphabet++);

         ++k;

      }

      printf("\n");

   }

}

 

 

/* 18 Write a program to Print following:

1

0 1

1 0 1

0 1 0 1

 

*/

 

#include <stdio.h>

 

int main()

{

    int i,j,rows;

 

    printf("Enter the number of rows: ");

    scanf("%d",&rows);

 

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

        {

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

            {

              if((i+j)%2==1)

              {

                 printf("0");

              }

          else

          {

              printf("1");

          }

            }

    printf("\n");

        }

}

 

 

/* 19 Write a program to Print following:

1

0 1

0 1 0

1 0 1 0

 

*/

 

#include <stdio.h>

 

int main()

{

    int i, j, k, N;

 

    printf("Enter N: ");

    scanf("%d", &N);

 

    k = 1;

 

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

    {

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

        {

            if(k % 2 == 1)

            {

                printf("1");

            }

            else

            {

                printf("0");

            }

 

            k++;

        }

 

        printf("\n");

    }

 

    return 0;

}

 

 

/* 20 Write a program to Print following:

   A

  ABA

 ABCBA

ABCDCBA

 

*/

 

#include<stdio.h>

 

int main()

{

    int ch=65;

    int i,j,k,m;

 

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

    {

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

            printf(" ");

 

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

            printf("%c",ch++);

            ch--;

 

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

            printf("%c",--ch);

 

        printf("\n");

        ch=65;

    }

return 0;

}