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




UNIT 4

 

/* 1 Display this kind of output on screen.

C
CP
CPR
. .
CPROGRAMING
.
.
CPR
CP
C

*/

#include <stdio.h>

 

void main()

{

    int c, d;

    char string[] = "CProgramming";

 

 

    for( c = 0 ; c <= 11 ; c++ )

        {

            d = c + 1;

            printf("%-12.*s\n", d, string);

        }

 

 

    for( c = 11 ; c >= 0 ; c-- )

 

        {

            d = c + 1;printf("%-12.*s\n", d, string);

        }

}

 

 

/*  2 Write a program which will take 10 numbers from user

and stored it in the array. It will print all the numbers, their sum and

average of it. */

 

#include<stdio.h>

 

void main() {

 

 

int arr[10];

int i;

float sum=0;

float avg;

 

 

printf("\n Enter 10 numbers : \n\n");

 

 for(i = 0; i<10;i++) {

 

      printf("Enter no. %d   :   \n",i+1);

      scanf("%d",&arr[i]);

 

 

 }

 

 for(i = 0; i<10;i++) {

 

         sum = sum + arr[i];

}

 printf("Sum : %.0f\n",sum);

 

 avg = sum/10;

 

 printf("Average : %.2f",avg);

 

 

}

 

 

/* 3 Write a program to find binary of given number */

 

#include<stdio.h>

#include<stdlib.h>

int main()

{

    int a[10],n,i;

 

    printf("Enter the number to convert: ");

    scanf("%d",&n);

 

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

        {

            a[i]=n%2;

            n=n/2;

        }

 

    printf("\nBinary of Given Number is=");

 

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

            {

        printf("%d",a[i]);

            }

return 0;

}

 

 

/* 4 Write a program to sort and array. */

 

#include <stdio.h>

    void main()

    {

 

        int i, j, a, n, number[30];

        printf("Enter the value of N \n");

        scanf("%d", &n);

 

        printf("Enter the numbers \n");

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

            scanf("%d", &number[i]);

 

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

        {

 

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

            {

 

                if (number[i] > number[j])

                {

 

                    a =  number[i];

                    number[i] = number[j];

                    number[j] = a;

 

                }

 

            }

 

        }

 

        printf("The numbers arranged in ascending order are given below \n");

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

            printf("%d\n", number[i]);

 

    }

 

 

/* 5 Write a program to search an element from the array. */

 

#include <stdio.h>

 

 

int main()

{

    int a[10000],i,n,key;

 

    printf("Enter size of the  array : ");

    scanf("%d", &n);

    printf("Enter elements in array : ");

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

    {

        scanf("%d",&a[i]);

    }

     printf("Enter the key : ");

    scanf("%d", &key);

 

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

    {

        if(a[i]==key)

        {

                                    printf("element found at %d ",i+1);

            return 0;

        }

 

    }

 

            printf("element  not  found");

}

 

 

/* 6 Write a program to find addition of two matrices of 3*3 */

 

#include <stdio.h>

 

int main()

 

{

  int r, c, a[100][100], b[100][100], sum[100][100], i, j;

 

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

  scanf("%d", &r);

 

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

  scanf("%d", &c);

 

  printf("\nEnter elements of 1st matrix:\n");

 

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

    for (j = 0; j < c; ++j) {

      printf("Enter element a%d%d: ", i + 1, j + 1);

      scanf("%d", &a[i][j]);

    }

 

  printf("Enter elements of 2nd matrix:\n");

 

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

  for (j = 0; j < c; ++j)

        {

      printf("Enter element b%d%d: ", i + 1, j + 1);

      scanf("%d", &b[i][j]);

        }

 

 

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

  for (j = 0; j < c; ++j)

    {

      sum[i][j] = a[i][j] + b[i][j];

    }

 

 

  printf("\nSum of two matrices: \n");

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

  for (j = 0; j < c; ++j)

    {

      printf("%d   ", sum[i][j]);

      if (j == c - 1) {

        printf("\n\n");

      }

    }

 

  return 0;

}

 

 

/* 7 Write a program to find multiplication of two matrices of 3*3 */

 

#include<stdio.h>

#include<stdlib.h>

 

int main()

{

   int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;

 

        printf("enter the number of row=");

        scanf("%d",&r);

 

        printf("enter the number of column=");

        scanf("%d",&c);

 

        printf("enter the first matrix element=\n");

 

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

          {

            for(j=0;j<c;j++)

          {

               scanf("%d",&a[i][j]);

          }

          }

 

        printf("enter the second matrix element=\n");

 

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

          {

           for(j=0;j<c;j++)

          {

              scanf("%d",&b[i][j]);

          }

          }

 

        printf("multiply of the matrix=\n");

 

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

          {

           for(j=0;j<c;j++)

          {

               mul[i][j]=0;

        for(k=0;k<c;k++)

          {

              mul[i][j]+=a[i][k]*b[k][j];

          }

          }

          }

 

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

          {

           for(j=0;j<c;j++)

          {

              printf("%d\t",mul[i][j]);

          }

              printf("\n");

          }

        return 0;

}

 

 

/* 8 Take two strings from the user and check whether the string is

palindrome or not */

 

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

{

    char str[20];

    int i, len, temp=0;

    int flag = 0;

 

    printf("Enter a string:");

    scanf("%s", str);

 

    len = strlen(str);

 

    for(i=0;i < len ;i++){

        if(str[i] != str[len-i-1]){

            temp = 1;

        break;

   }

}

 

    if (temp==0) {

        printf("String is a palindrome");

    }

    else {

        printf("String is not a palindrome");

    }

    return 0;

}

 

 

/* 9 Write a program to find sum, average of two numbers passed to

user defined functions called sum(int,int) and average(int,int). */

 

#include <stdio.h>

 

int main()

{

    int number1, number2;

    int sum;

    float avg;

 

    printf("Enter the first integer number: ");

    scanf("%d", &number1);

 

    printf("Enter the second integer number: ");

    scanf("%d", &number2);

 

    sum = sumTwoNum(number1, number2);

    avg = averageTwoNum(number1, number2);

 

    printf("Number1: %d, Number2: %d\n", number1, number2);

    printf("Sum: %d, Average: %f\n", sum, avg);

 

    return 0;

}

 

int sumTwoNum(int x, int y)

{

    int sum;

    sum = x + y;

    return sum;

}

 

int averageTwoNum(int x, int y)

{

    float average;

    return ((float)(x) + (float)(y)) / 2;

}

 

 

/* 10 Write a program to print factorial of a given number by recursive

user defined function fact(int). */

 

#include<stdio.h>

 

int main()

{

    int n;

 

    printf("Enter a positive integer: ");

    scanf("%d",&n);

 

    printf("Factorial of %d = %ld", n, fact(n));

    return 0;

}

 

int fact(int n)

{

    if (n>=1)

        return n*fact(n-1);

    else

        return 1;

}

 

 

/* 11 Write a program to print Fibonacci series using recursive UDF. */

 

#include<stdio.h>

 

int main()

{

            int n, i;

 

            printf("Enter the number of element you want in series :\n");

            scanf("%d",&n);

 

            printf("fibonacci series is : \n");

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

        {

                        printf("%d ",fibonacci(i));

                }

}

 

int fibonacci(int i){

            if(i==0) return 0;

            else if(i==1) return 1;

            else return (fibonacci(i-1)+fibonacci(i-2));

}

 

 

/* 12 Write a program to find length of the given string (without

including string.h). */

 

#include <stdio.h>

int main()

{

    char s[] = "BCA 4 U";

    int i;

 

    for (i = 0; s[i] != '\0'; ++i);

 

    printf("Length of the string: %d", i);

    return 0;

}

 

 

/* 13 Write a program to find length of the given string (without

including string.h). */

 

#include <stdio.h>

int main()

{

    char s[] = "BCA 4 U";

    int i;

 

    for (i = 0; s[i] != '\0'; ++i);

 

    printf("Length of the string: %d", i);

    return 0;

}

 

 

/* 14 Write a program to convert lowercase string to uppercase string

(without including string.h). */

 

#include <stdio.h>

 

int main()

{

    char s[1000];

    int i;

 

    printf("Enter  the string in lower case: ");

    gets(s);

 

    printf("string in lowercase ='%s'\n",s);

 

 

    for(i=0;s[i];i++)

    {

        if(s[i]>=97 && s[i]<=122)

         s[i]-=32;

            }

 

 

    printf("string in uppercase ='%s'\n",s);

}

 

/* 15 Write a program which will accept two strings from the user and

print the message that the strings are same or not */

 

#include <stdio.h>

#include<string.h>

int main()

{

   char str1[20];

   char str2[20];

   int value;

 

   printf("Enter the first string : ");

   scanf("%s",str1);

 

   printf("Enter the second string : ");

   scanf("%s",str2);

 

   value=strcmp(str1,str2);

 

   if(value==0)

   printf("strings are same");

   else

   printf("strings are not same");

   return 0;

}

 

/* 16 Write a program which take a lowercase string from the user and

print its length and uppercase string. */

 

 

#include <stdio.h>

#define MAX_SIZE 100

 

 

int main()

{

    char str[MAX_SIZE];

    int i;

 

    printf("Enter your text : ");

    gets(str);

 

     for (i = 0; str[i] != '\0'; ++i);

 {

     printf("Length of string is %d\n", i);

 }

 

 

    for(i=0; str[i]!='\0'; i++)

    {

        if(str[i]>='a' && str[i]<='z')

        {

            str[i] = str[i] - 32;

        }

    }

    printf("Uppercase string : %s",str);

 

}

 

 

/* 17 Write a program that uses function digit(N,k) that return the

value of the kth digit from the right of the number N. For eg. The

function call digit (254693,2) should return 9 */

 

#include<stdio.h>

int digit(int,int);

 

void main()

{

        int num,pos,dg;

 

        printf("\nEnter a number=");

        scanf("%d",&num);

 

        printf("\nEnter the position=");

        scanf("%d",&pos);

 

        dg=digit(num,pos);

 

        if(dg==-1)

        printf("\nInvalid position");

        else

        printf("\nDigit found in position %d is %d",pos,dg);

}

 

int digit(int n,int p)

{

        int i=1;

        while(i<p && n>=0)

        {

        n=n/10;

        i++;

        }

        if(n==0)

        return -1;

        else

        return n%10;

}

 

 

/* 18 Program to find if the given no. is prime or not. The function

should accept the number as argument and return if the no. is prime

or not */

 

#include <stdio.h>

 

void main()

{

            int num,res=0;

 

            printf("\nENTER A NUMBER: ");

            scanf("%d",&num);

 

            res=prime(num);

 

            if(res==0)

 

                        printf("\n%d IS A PRIME NUMBER",num);

            else

 

                        printf("\n%d IS NOT A PRIME NUMBER",num);

}

 

int prime(int n)

{

            int i;

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

            {

                        if(n%i!=0)

                                    continue;

                        else

                                    return 1;

            }

            return 0;

}