Posts

‘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]); ...

‘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]); ...

‘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    ...

‘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    ...

‘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)         ...

‘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)   ...

‘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; }