Posts

Showing posts from 2020

Assignment III - Basic 'C'

Assignment III Write a ‘C’ program to add two matrices of order m X n. Write a ‘C’ program to check if a n X n matrix is symmetric. Write a ‘C’ program to read a matrix and calculate the sum of its diagonal elements. Write a ‘C’ program to subtract two matrices of order m Xn. Write a ‘C’ program to read a matrix and display its transpose. Write a ‘C’ program to check if a matrix is upper triangular. Write a ‘C’ program with menu to perform the following operations on a character. 1. Check uppercase or lowercase 2. Display its ASCII value 3. Display its next and previous character 4. Exit. Write a menu driven program to perform the following operations on an integer. Write separate functions.  1. Check if is even or odd   2. Check if it is prime   3. Exit Accept two numbers and perform the following operations till the user selects Exit.  1. Maximum    2. Display all numbers between the two   3. Sum and average    4. EXIT Write a function in ‘C’, which accepts

‘C’ program, which accepts annual basic salary of an employee and calculates and displays the Income tax

C File => /* Write a ‘C’ program, which accepts annual basic salary of an employee and calculates and displays the Income tax as per the following rules.   Basic: < 1,50,000 Tax = 0   1,50,000 to 3,00,000 Tax = 20%   > 3,00,000 Tax = 30% */ #include<stdio.h> int main()  {      int sal;      float tax;        printf("\n Enter basic salary : ");      scanf("%d",&sal);        if(sal<150000)          printf("\n Income tax for given salary : 0 Rs.");     else if(sal>150000 && sal<=300000)    {       tax=sal*0.2;       printf("\n Income tax = %f",tax);    }     else if(sal>300000)    {        tax=sal*0.3;        printf("\n Income tax = %f",tax);    }       return 0;  }

Accept two numbers and perform the following operations till the user selects Exit.

C File => /* Accept two numbers and perform the following operations till the user selects Exit.  i. Maximum  ii. Display all numbers between the two  iii. Sum and average  iv. EXIT */ #include<stdio.h> #include<stdlib.h> int main()  {        int n1,n2,i,ch,sum;       float avg;         printf("\n Enter number1 : ");       scanf("%d",&n1);       printf("\n Enter number2 : ");       scanf("%d",&n2);         printf("\n Menu : \n1. Maximum");       printf("\n 2. Display all numbers between %d and %d",n1,n2);       printf("\n 3. Sum and Average of two");       printf("\n 4. Exit\n Enter your choice (1-4) : ");       scanf("%d",&ch);         switch(ch)      {         case 1: if(n1>n2)                           printf("\n Maximum = %d",n1);                       else                           printf("\n Maximum = %d",n2);        break;        case 2: for

Menu driven program to perform operations on an integer.

C File => /* Write a menu driven program to perform the following operations on an  integer. Write separate functions.  1. Check if is even or odd  2. Check if it is prime  3. Exit */ #include<stdio.h> #include<stdlib.h> int main()  {       int n,ch,i;         void chk_even(int n)      {         if(n%2==0)            printf("\n Number is EVEN..");        else           printf("\n Number is ODD...");      }              void chk_prime(int n)      {         int flag=1;         for(i=2;i<n;i++)        {            if(n%i==0)           {               flag=0;               break;            }         }          if(flag==0)              printf("\n Number is NOT prime..");       else             printf("\n Number is PRIME..");       }        printf("\n Enter an integer : ");      scanf("%d",&n);            printf("\n Menu driven operations:\n 1. Check if is even or odd\n  2. Check if it is prime\n 3. Exit&

‘C’ program with menu to perform operations on a character.

C File => /*Write a ‘C’ program with menu to perform the following operations on a  character.  1. Check uppercase or lowercase  2. Display its ASCII value  3. Display its next and previous character  4. Exit */ #include<stdio.h> #include<stdlib.h> #include<ctype.h> int main()  {      int ch;      char c;        printf("\n Enter any character : ");      scanf("%c",&c);      printf("\n Menu : \n1. Check uppercase or lowercase");      printf("\n 2. Display its ASCII value");      printf("\n 3. Display its next and previous character\n 4. Exit");      printf("\n Enter your choice (1-4)");      scanf("%d",&ch);        switch(ch)     {        case 1: if(isupper(c))                           printf("Uppercase character");                      else if(islower(c))                           printf("Lowercase character");                     else                           printf

‘C’ program to check if a matrix is upper triangular.

C File => /* Write a ‘C’ program to check if a matrix is upper triangular. */ #include<stdio.h> int main()  {      int i,j,r,c,mat[30][30],flag=1;        printf("\n Enter number of rows in matrix : ");      scanf("%d",&r);      printf("\n Enter number of cols in matrix : ");      scanf("%d",&c);     //Accepting matrix     for(i=0;i<r;i++)    {        for(j=0;j<c;j++)       {           printf("\n Enter matrix element : ");           scanf("%d",&mat[i][j]);       }     }       //checking upper triangular       for(i=0;i<r;i++)     {         for(j=0;j<c;j++)        {             if(r>c && mat[i][j]!=0)             {                  flag=0;                  break;              }          }       }         if(flag==0)              printf("\n Matrix is NOT upper triangular..");      else             printf("\n Matrix is upper triangular.. ");        return 0; }

‘C’ program to display matrix transpose.

C File => /* Write a ‘C’ program to read a matrix and display its transpose. */ #include<stdio.h> int main()  {      int mat[30][30],trans[30][30],i,j,m,n;        printf("\n Enter how many rows?");      scanf("%d",&m);      printf("\n Enter how many cols?");      scanf("%d",&n);        printf("\n Input matrix");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {         printf("\n Enter element %d%d",i+1,j+1);         scanf("%d",&mat[i][j]);       }     }       printf("\n\n Given matrix is\n");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {          printf("%d\t",mat[i][j]);          trans[j][i]=mat[i][j];       }       printf("\n");     }        printf("\n\n Transpose of given matrix\n");     for(i=0;i<m;i++)    {      for(j=0;j<n;j++)      {         printf("%d\t",trans[i][j]);       }      printf("\n");

‘C’ program to subtract two matrices of order m X n.

C File => /* Write a ‘C’ program to subtract two matrices of order mXn */ #include<stdio.h> int main()  {      int m,n,i,j,mat1[20][20];      int mat2[20][20],sub[20][20];        printf("\n Enter no. of rows : ");      scanf("%d",&m);      printf("\n Enter no. of cols : ");      scanf("%d",&n);      printf("\n Input Matrix1 : \n");      for(i=0;i<m;i++)     {       for(j=0;j<n;j++)      {         printf("\n Enter element %d%d : ",i+1,j+1);         scanf("%d",&mat1[i][j]);       }     }       printf("\n Input Matrix2 : \n");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {          printf("\n Enter element %d%d : ",i+1,j+1);          scanf("%d",&mat2[i][j]);       }     }     printf("\n\n Resultant Matrix is :\n");     for(i=0;i<m;i++)    {        for(j=0;j<n;j++)       {          sub[i][j]=mat1[i][j]-mat2[i][j];          printf(

‘C’ program to calculate the sum of diagonal elements of matrix.

C File => /* Write a ‘C’ program to read a matrix and calculate the sum of its diagonal  elements. */ #include<stdio.h> int main()  {      int mat[30][30],sum=0,i,j,m,n;        printf("\n Enter how many rows?");      scanf("%d",&m);      printf("\n Enter how many cols?");      scanf("%d",&n);        printf("\n Input matrix");      for(i=0;i<m;i++)     {         for(j=0;j<n;j++)        {            printf("\n Enter element %d%d",i+1,j+1);           scanf("%d",&mat[i][j]);         }     }       printf("\n\n Given matrix is\n");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {         printf("%d\t",mat[i][j]);                  if(i==j)            sum=sum+mat[i][j];       }       printf("\n");     }       printf("\n Sum of diagonal : %d",sum);       return 0; }

‘C’ program to check if a n X n matrix is symmetric.

C File => /* Write a ‘C’ program to check if a nXn matrix is symmetric. */ #include<stdio.h> int main()  {      int mat[30][30],i,j,m,n,flag=1;            printf("\n Enter how many rows?");      scanf("%d",&m);      printf("\n Enter how many cols?");      scanf("%d",&n);      printf("\n Input matrix");      for(i=0;i<m;i++)     {         for(j=0;j<n;j++)        {            printf("\n Enter element %d%d",i+1,j+1);            scanf("%d",&mat[i][j]);         }      }      printf("\n\n Given matrix is\n");      for(i=0;i<m;i++)     {         for(j=0;j<n;j++)             printf("%d\t",mat[i][j]);        printf("\n");     }     for(i=0;i<m;i++)    {        for(j=0;j<n;j++)       {           if(mat[i][j]!=mat[j][i])           {               flag=0;               break;           }        }        printf("\n");     }     if(flag==1)        print

‘C’ program to accepts a character and integer n as parameter and displays the next 'n' characters.

C File => /* Write a function in ‘C’, which accepts a character and integer n as parameter and displays the next 'n' characters. */ #include<stdio.h> int main()  {       int n,i;       char c;     // defining function        void disp_chars(char ch,int n)       {          for(i=1;i<=n;i++)          {              printf("%c\t",ch+1);              ch++;           }       }         printf("\n Enter character : ");       scanf("%c",&c);       printf("\n Enter value of n : ");       scanf("%d",&n);         printf("\n Next %d characters are :\n",n);  disp_chars(c,n);         return 0; }

Assignment II - Basic 'C'

Assignment II Write a ‘C’ program to calculate the sum of factors of anumber. Write a recursive function in ‘C’ to calculate factorialof a number. Use this function in main.   Write a function in ‘C’ to reverse an integer. Use thisin main   Write a function in ‘C’ to calculate sum of digits of aninteger. Use this function in main. Write a ‘C’ program to display n terms of the Fibonacciseries.   Write a ‘C’ function to check if a number is prime. Use this function to  display all prime numbers between 100 and 500.   Write a ‘C’ program to display n lines of the following pattern. 1 2 3 4 5 6   Write a ‘C’ program to calculate the GCD of two numbers.   Write a ‘C’ program to accept real number x and integer n and calculate the sum of first n terms of the series   x+ x/3!+ x/5!+ x/7!+...   Write a ‘C’ function to calculate x y . Use this function to calculate the sum of first n terms of the series    x + x 3 /3 + x 5 /5 + ....   Write a ‘C’ program to acc

‘C’ program to check if a number is perfect

C File => /* Write a ‘C’ program to check if a number is perfect (number = sum of its factors) */ #include <stdio.h> int main()  {      int n, i,sum=0;      printf("\n Enter a positive integer: ");      scanf("%d",&n);      printf("\n Factors of %d are: ", n);      for (i = 1; i<n; ++i)      {         if (n % i == 0)         {               printf("%d\t", i);               sum=sum+i;          }      } //end for      if(sum==n)           printf("\n %d is perfect number",n);      else           printf("\n %d is Not a perfect number",n);      return 0; }

‘C’ program to check given year is leap year or not.

C File => /* Write a ‘C’ program to check whether the given year is leap year or not. */ #include<stdio.h> int main()  {       int year;       printf("Enter year: ");       scanf("%d",&year);         if(year % 4 == 0)      {           //Nested if else         if( year % 100 == 0)         {             if ( year % 400 == 0)                 printf("\n%d is a Leap Year", year);             else                 printf("\n%d is not a Leap Year", year);           }           else                printf("\n%d is a Leap Year", year );         }         else                printf("\n%d is not a Leap Year", year);           return 0; }

‘C’ program to check if character is alphabet, digit or special symbol.

C File => /* Write a ‘C’ program to accept a character and check if it is alphabet,  digit or special symbol. If it is an alphabet, check if it is uppercase or  lowercase. */ #include<stdio.h> #include<ctype.h> int main()  {       char c;         printf("\n Enter any character : ");       scanf("%c",&c);         if(isalpha(c))      {           if(islower(c))              printf("\n Lowercase alphabet");          else              printf("\n Uppercase alphabet");       }       else if(isdigit(c))             printf("\n You entered Digit");       else             printf("It is a special symbol");         return 0; }

‘C’ function to calculate x^y. Use this function to calculate the sum of first n terms of the series x + x^3/3 + x^5/5 + ...

C File => /* Write a ‘C’ function to calculate xy. Use this function to calculate the sum  of first n terms of the series x + x^3/3 + x^5/5 + ... */ #include<stdio.h> int main()  {       int n,x,i,sum=0;      // defining function to compute x^y      int cal_pow(int x, int y)     {         if (y == 0)             return 1;         else if (y%2 == 0)             return cal_pow(x, y/2)*cal_pow(x, y/2);         else            return x*cal_pow(x, y/2)*cal_pow(x, y/2);       }        printf("\n Enter value of x : ");      scanf("%d",&x);        printf("\n Enter value of n : ");      scanf("%d",&n);        for(i=1;i<=n;i=i+2)              sum=sum+(cal_pow(x,i)/i);      printf("\n Sum of series = %d",sum);          return 0; }

‘C’ program to accept real number x and integer n and calculate the sum of first n terms of the series x+ x/3!+ x/5!+ x/7!+…

C File => /* Write a ‘C’ program to accept real number x and integer n and calculate  the sum of first n terms of the series x+ x/3!+ x/5!+ x/7!+… */ #include<stdio.h> int main()  {       int x,n,i;       float sum=0;         printf("\n Enter value of x : ");       scanf("%d",&x);       printf("\n Enter value of n : ");       scanf("%d",&n);        printf("\n\n Sum of series x+x/3!+x/5!...\n");     // defining function to compute factorial       int fact(int n)      {           if(n==1)               return 1;          else               return (n*fact(n-1));       }        // computing sum of series       for(i=1;i<=n;i=i+3)           sum=sum+x/fact(i);         printf("\n %f",sum);         return 0; }

‘C’ program to calculate the GCD of two numbers.

C File => /* Write a ‘C’ program to calculate the GCD of two numbers. */ #include<stdio.h> int main()  {       int m, n;         printf("Enter two positive integers: ");       scanf("%d %d",&m,&n);         while(m!=n)       {            if(m > n)                 m = m - n;           else                 n = n - m;        }         printf("GCD = %d",m);         return 0; }

‘C’ program to display n lines of the pattern.

C File => /* Write a ‘C’ program to display n lines of the following pattern.   1   2 3   4 5 6 */ #include<stdio.h> int main()  {       int i,j,n=1;         for(i=1;i<4;i++)      {          for(j=1;j<=i;j++)          {              printf("%d\t",n);              n++;           }          printf("\n");      }      return 0; }

‘C’ function to check if a number is prime.

C File => /* Write a ‘C’ function to check if a number is prime. Use this function to  display all prime numbers between 100 and 500. */ #include<stdio.h> int main()  {        int i;     // function to check if number is prime        int isprime(int n)       {            int flag=1,i;            for(i=2;i<n-1;i++)            {                if(n%i==0)               {                   flag=0;                   break;                }            }           return flag;        }           printf("\n Prime numbers between 100 and 500 :\n");         for(i=100;i<500;i++)       {           if(isprime(i)==1)                printf("%d\t",i);       }         return 0; }

‘C’ program to display n terms of the Fibonacci series.

C File => /* Write a ‘C’ program to display n terms of the Fibonacci series. */ #include<stdio.h> int main()  {       int n,i,a=0,b=1,c;         printf("\n Enter value of n : ");       scanf("%d",&n);         if(n==1)          printf("\n %d",a);       else if(n==2)          printf("\n%d\t%d",a,b);       else if(n>2)       {           printf("\n%d\t%d\t",a,b);           for(i=3;i<=n;i++)           {               c=a+b;               printf("%d\t",c);               a=b;               b=c;           } //end for        }          return 0; }

‘C’ to calculate sum of digits of an integer.

C File => /* Write a function in ‘C’ to calculate sum of digits of an integer. Use this function in main */ #include<stdio.h> int main()  {       int num;      // defining function        void sum_digits(int n)       {            int r,sum=0;            int num=n;              while(n>0)            {                r=n%10;                sum=sum+r;                n=n/10;             }            printf("\n Sum of digits in %d is %d",num,sum);        }          printf("\n Enter number : ");        scanf("%d",&num);         //calling function         sum_digits(num);                return 0; }

‘C’ to reverse an integer.

C File => /* Write a function in ‘C’ to reverse an integer. Use this in main */ #include<stdio.h> int main()  { int num;     // function to compute reverse      void reverse_num(int n)      {           int r,sum=0;           printf("\n Reverse of %d is ",n);            while(n>0)           {                r=n%10;                printf("%d",r);                n=n/10;            }        }        printf("\n Enter number : ");        scanf("%d",&num);       //calling function        reverse_num(num);       return 0; }

‘C’ to calculate factorial of a number using recursion.

C File => /* Write a recursive function in ‘C’ to calculate factorial of a number. Use  this function in main. */ #include<stdio.h> int main()  {       int num;      // defining recursive function       int fact(int n)     {          if(n==1 || n==0)                  return 1;          else                  return(n*fact(n-1));      }       printf("\n Enter number : ");       scanf("%d",&num);       printf("\n Factorial of %d is %d",num,fact(num));      return 0; }

‘C’ program to calculate the sum of factors of a number.

C File => /* Write a ‘C’ program to calculate the sum of  factors of a number. */ #include <stdio.h> int main()  {         int n, i,fact[100],sum=0;           printf("\n Enter a positive integer: ");         scanf("%d",&n);         printf("\n Factors of %d are: ", n);         for (i = 1; i < n; ++i)         {              if (n % i == 0)              {                    printf("%d\t", i);                    sum=sum+i;              }          }         printf("\n Sum of factors : %d",sum);  return 0; }

‘C’ program to check if number is divisible by 3 or 5.

C File => /* Write a ‘C’ program to accept an integer and check if it is divisible by  3 and 5. */ #include<stdio.h> int main()  {        int num;          printf("\n Enter number : ");        scanf("%d",&num);          if(num%3==0 && num%5==0)              printf("\n %d is divisible by both 3 and 5",num);       else if(num%3==0)              printf("\n %d is divisible by only 3",num);       else if(num%5==0)              printf("\n %d is divisible by only 5",num);      else              printf("\n %d is not divisible by 3 and 5",num);         return 0; }

‘C’ program to print the surface area and volume (surface area = 2πr2 + 2πrh, volume = πr2h) of Cylinder

C File = /* Write a ‘C’ program to accept dimensions of a cylinder and print the surface area and volume  (surface area = 2πr2 + 2πrh,  volume = πr2h) */ #include<stdio.h> #define pi 3.142 int main()  {       int r,h;       float sa,v;         printf("\n Enter radius of cylinder : ");       scanf("%d",&r);       printf("\n Enter height of cylinder : ");       scanf("%d",&h);         v=(pi*r*r*h);       sa=(2*pi*r*r)+(2*pi*r*h);         printf("\n Surface Area = %f",sa);       printf("\n Volume = %f",v);       return 0; }

‘C’ program to print surface area (surface area=2(lb+lh+bh) of cuboid.

C File => /* Write a ‘C’ program to accept three dimensions length (l), breadth(b)  and height(h) of a cuboid and print surface area (surface area=2(lb+lh+bh)*/ #include<stdio.h> int main()  {        int l,b,h;          printf("\n Enter length : ");        scanf("%d",&l);        printf("\n Enter breadth : ");        scanf("%d",&b);        printf("\n Enter height : ");        scanf("%d",&h);        printf("\n Surface Area of cuboid = %d",2*(l*b+l*h+b*h));        return 0; }

‘C’ program to check if character is uppercase or lowercase.

C File => /* Write a ‘C’ program to accept a character and check if it is uppercase  or lowercase */ #include<stdio.h> #include<ctype.h> int main()  {         char ch;         printf("\n Enter character : ");         scanf("%c",&ch);         if(islower(ch))               printf("\n Lower case character..");         else if(isupper(ch))               printf("\n Upper case character..");        else               printf("\n Invalid input...");       return 0; }

‘C’ program to search for a specific number in array.

C File => /* Write a ‘C’ program to accept n integers in an array and search for a  specific number. */ #include<stdio.h> int main()  {        int n,i,key,arr[30],flag=0;            printf("\n How many integers? ");        scanf("%d",&n);          for(i=0;i<n;i++)       {          printf("\n Enter element%d",i+1);          scanf("%d",&arr[i]);        }          printf("\n Enter element to search : ");          scanf("%d",&key);           // Searching for key         for(i=0;i<n;i++)        {             if(arr[i]==key)            {                flag=1;                break;             }         }       if(flag==1)           printf("\n %d is found in array",key);      else           printf("\n %d is Not found in array",key);      return 0; }

‘C’ program to find the maximum number from an array of n integers.

C File => /* Write ‘C’ program to find the maximum number from an array of n  integers. */ #include<stdio.h> int main()  {        int num[30],i,max=0,n;        printf("\n How many integers? ");        scanf("%d",&n);        printf("\n Enter integers :\n");          for(i=0;i<n;i++)             scanf("%d",&num[i]);          for(i=0;i<n;i++)       {            if(max<num[i])                   max=num[i];        }        printf("\n Maximum of given numbers is : %d",max);  return 0; }

‘C’ program to accept an array of n float values and display them in the reverse order.

C File => /* Write a ‘C’ program to accept an array of n float values and display  them in the reverse order. */ #include<stdio.h> int main()  {       int n,i;       float arr[30];       printf("\n How many elements? ");       scanf("%d",&n);      // Accepting input      for(i=0;i<n;i++)     {         printf("\n Enter element%d : ",i+1);         scanf("%f",&arr[i]);      }      printf("\n Array elements in Reverse order : \n");       for(i=n-1;i>=0;i--)             printf("%.2f\t",arr[i]);       return 0; }

‘C’ program to print digit into word

C File => /* Write ‘C’ program to accept a single digit and display it in words. For  example, Input = 9 Output = Nine */ #include<stdio.h> int main()  {       int n;       printf("\n Enter single digit number : ");       scanf("%d",&n);       switch(n)      {            case 0:printf("\n Zero");            break;            case 1:printf("\n One");            break;            case 3:printf("\n Three");            break;            case 4:printf("\n Four");            break;            case 5:printf("\n Five");            break;            case 6:printf("\n Six");            break;            case 7:printf("\n Seven");            break;            case 8:printf("\n Eight");            break;            case 9:printf("\n Nine");            break;           default:printf("\n Invalid input");         }     return 0; }

‘C’ program to calculate area and perimeter of a rectangle.

C File => /* Write a ‘C’ program to calculate area and perimeter  of a rectangle. */ #include<stdio.h> int main()  {     int l,b,a,p;     printf("\n Enter length and bredth of rectangle : ");     scanf("%d%d",&l,&b);     a=l*b;     p=2*(l+b);     printf("\n Area of rectangle = %d",a);     printf("\n Perimeter of rerectangle = %d",p);  return 0; }

‘C’ program to calculate area and circumference of a circle

C File => /* Write a ‘C’ program to calculate area and circumference of a circle. */ #include<stdio.h> #define PI 3.142 int main()  {      int r;      printf("\n Enter radius : ");      scanf("%d",&r);      printf("\n Area of circle = %f",PI*r*r);      printf("\n Circumference of circle : %f",2*PI*r);    return 0; }

Assignment I - Basic 'C'

Image
Assignment 1 Write a 'C' program to find maximum of two numbers . program to interchange numbers. To convert temperaturefrom farenheit to celcius. Best deals for Mobile To check if number is positive, negative orzero. Display all numbers between two given numbers. Write a 'C' program to calculate area and circumference of a circle. Write a ‘C’ program to calculate area and perimeter of a rectangle. Write ‘C’ program to accept a single digit and display it in words. Forexample, Input = 9 Output = Nine Write a ‘C’ program to accept an array of n floatvalues and display them in the reverse order. Write ‘C’ program to find the maximum number froman array of n integers. Write a ‘C’ program to accept n integers in an array and search for a specific number. Write a ‘C’ program to accept a character and checkif it is uppercase or lowercase. Write a ‘C’ program to accept three dimensionslength (l), breadth(b) and height(h) of a cuboid and prin

‘C’ program to display all numbers between two given numbers.

C File => /* Write a ‘C’ program to display all numbers between two given  numbers. */ #include<stdio.h> int main()  {       int n,m,i;       printf("\n Enter first number : ");       scanf("%d",&n);       printf("\n Enter last number : ");       scanf("%d",&m);         for(i=n+1;i<m;i++)             printf("%d\t",i);       return 0; }