C question and answer with explanation









31. SUM OF SQUARES OF THE SERIES 12+22+32+--------+n2
void main()
{
  long int r;
  clrscr();
  printf("\nEnter the range: ");
  scanf("%ld",&r);
  printf("\nSum of the squares of the series is: %ld",((r*(r+1))*(2*r+1))/6);
  getch();
}
32. SUM OF CUBES OF THE SERIES 13+23+33+---------+n3
void main()
{
  int r;
  clrscr();
  printf("\nEnter the number range: ");
  scanf("%d",&r);
  printf("\nSum of the cubes of the series is: %d",(r*(r+1)/2)*(r*(r+1)/2));
  getch();
}
33. LARGEST NUMBER IN AN ARRAY
void main()
{
  int a[50],size,i,big;
  clrscr();
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ”, size);
  for(i=0;i<size;i++)
  scanf("%d",&a[i]);
  big=a[0];
  for(i=1;i<size;i++)
  {
            if(big<a[i])
            big=a[i];
  }
  printf("\nBiggest: %d",big);
  getch();
}
34. SECOND LARGEST NUMBER IN AN UNSORTED ARRAY
main()
{
  int un[10], i, big1, big2;
  printf("Enter array elements: ");
  for ( i = 0; i < 10; ++i )
            scanf("%d", &un[i]);
  big1 = un[0];
  for ( i = 1; i < 10; ++i )
  {
            if ( big1 < un[i] )
                        big1 = un[i];
            if ( big1 != un[0] )
                        big2 = un[0];
            else
                        big2 = un[1];
  }
  for ( i = 1; i < 10; ++i )
  {
            if ( big1 != un[i] && big2 < un[i] )
                        big2 = un[i];
  }
  printf("Second largest: %d\n", big2);
  return 0;
}
35. SECOND SMALLEST NUMBER IN AN UNSORTED ARRAY
main()
{
  int un[10], i, s1, s2;
  clrscr();
  printf("Enter array elements: ");
  for ( i = 0; i < 10; ++i )
            scanf("%d", &un[i]);
  s1 = un[0];
  for ( i = 1; i < 10; ++i )
  {
            if ( s1 > un[i] )
                        s1 = un[i];
            if ( s1 != un[0] )
                        s2 = un[0];
            else
                        s2 = un[1];
  }
  for ( i = 1; i < 10; ++i )
  {
            if ( s1 != un[i] && s2 > un[i] )
                        s2 = un[i];
  }
  printf("\nSecond smallest: %d", s2);
  return 0;
}
36. MULTIPLICATION OF MATRICES
void main()
{
  int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
  clrscr();
  printf("\nEnter the row and column of first matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the row and column of second matrix");
  scanf("%d %d",&o,&p);
  if(n!=o)
  {
            printf("Matrix mutiplication is not possible");
            printf("\nColumn of first matrix must be same as row of second matrix");
  }
  else
  {
            printf("\nEnter the First matrix->");
            for(i=0;i<m;i++)
                        for(j=0;j<n;j++)
                                    scanf("%d",&a[i][j]);
            printf("\nEnter the Second matrix->");
            for(i=0;i<o;i++)
                        for(j=0;j<p;j++)
                                    scanf("%d",&b[i][j]);
            printf("\nThe First matrix is\n");
            for(i=0;i<m;i++)
            {
                        printf("\n");
                        for(j=0;j<n;j++)
                        {
                                    printf("%d\t",a[i][j]);
                        }
            }
            printf("\nThe Second matrix is\n");
            for(i=0;i<o;i++)
            {
                        printf("\n");
                        for(j=0;j<p;j++)
                        {
                                    printf("%d\t",b[i][j]);
            }          }
            for(i=0;i<m;i++)
                        for(j=0;j<p;j++)
                                    c[i][j]=0;
            for(i=0;i<m;i++)//row of first matrix
            {
                        for(j=0;j<p;j++)//column of second matrix
                        {
                                    sum=0;
                                    for(k=0;k<n;k++)
                                                sum=sum+a[i][k]*b[k][j];
                                    c[i][j]=sum;
                        }
            }
  }
  printf("\nThe multiplication of two matrix is\n");
  for(i=0;i<m;i++)
  {
            printf("\n");
            for(j=0;j<p;j++)
            {
                        printf("%d\t",c[i][j]);
            }
  }
  getch();
}
37. SUM OF DIAGONAL ELEMENTS OF A MATRIX
void main()
{
  int a[10][10],i,j,sum=0,m,n;
  clrscr();
  printf("\nEnter the row and column of matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the First matrix->");
  for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                        scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<m;i++)
  {
            printf("\n");
            for(j=0;j<m;j++)
            {
                        printf("%d\t",a[i][j]);
            }
  }
  for(i=0;i<m;i++)
  {
            for(j=0;j<n;j++)
            {
                        if(i==j)
                        sum=sum+a[i][j];
            }
  }
  printf("\n\nSum of the diagonal elements of a matrix is -> ");
  printf("%d",sum);
  getch();
}
38. TRASPOSE OF A MATRIX
void main()
{
  int a[10][10],b[10][10],i,j,k=0,m,n;
  clrscr();
  printf("\nEnter the row and column of matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the First matrix->");
  for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                        scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<m;i++)
  {
            printf("\n");
            for(j=0;j<m;j++)
            {
                        printf("%d\t",a[i][j]);
            }
  }
  for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                        b[i][j]=0;
            for(i=0;i<m;i++)
            {
                         for(j=0;j<n;j++)
                         {
                                    b[i][j]=a[j][i];
                                    printf("\n%d",b[i][j]);
                          }
            }
            printf("\n\nTraspose of a matrix is -> ");
  for(i=0;i<m;i++)
  {
            printf("\n");
            for(j=0;j<m;j++)
            {
                        printf("%d\t",b[i][j]);
            }
  }
  getch();
}
39. CONVERSION OF FAREHNITE TO CENTIGRADE
void main()
{
  float c,f;
  clrscr();
  printf("Enter temp. in farehnite");
  scanf("%f",&f);
  c=(5*(f-32))/9;//Formula for conversion
  printf("The temp. in centigrade is->%f",c);
  getch();
}
40. COUNTING DIFFERENT CHARACTERS IN A STRING
main()
{
  int a[26],A[26],i,c=0;
  char str[100];
  clrscr();
  puts("Enter a string->");
  gets(str);
  for(i=0;i<26;i++)
  {
            a[i]=0;
            A[i]=0;
  }
  for(i=0;str[i]!='\0';i++)
  {
            c=str[i];
            if(c<97)
            {
                        c=c-65;
                        A[c]++;
            }
            else
            {
                        c=c-97;
                        a[c]++;
            }
  }
  for(i=0;i<26;i++)
  {
            if(a[i]!=0)
                        printf("\n%c occurs %d times",i+97,a[i]);
  }
  for(i=0;i<26;i++)
  {
            if(A[i]!=0)
                        printf("\n%c occurs %d times",i+97,A[i]);
  }
  getch();
}
41. SORTING OF STRING
void main()
{
  int i,j,n;
  char str[20][20],temp[20];
  clrscr();
  puts("Enter the no. of string to be sorted");
  scanf("%d",&n);
  for(i=0;i<=n;i++)
            gets(str[i]);
  for(i=0;i<=n;i++)
            for(j=i+1;j<=n;j++)
            {
                        if(strcmp(str[i],str[j])>0)
                        {
                                    strcpy(temp,str[i]);
                                    strcpy(str[i],str[j]);
                                    strcpy(str[j],temp);
                        }
            }
  printf("The sorted string\n");
  for(i=0;i<=n;i++)
            puts(str[i]);
  getch();
}
42. BUBBLE SORT
void main()
{
  int s,temp,i,j,a[20];
  clrscr();
  printf("\nEnter size of the array: ");
  scanf("%d",&s);
  printf("\nEnter %d elements in to the array:",s);
  for(i=0;i<s;i++)
            scanf("%d",&a[i]);
  for(i=0;i<s-1;i++)
  {
            for(j=0;j<s-1-i;j++)
            {
                        if(a[j]>a[j+1])
                        {
                                    temp=a[j];
                                    a[j]=a[j+1];
                                    a[j+1]=temp;
                        }
            }
  }
  printf("\nThe array after sorting is: ");
  for(i=0;i<s;i++)
            printf(" %d",a[i]);
  getch();
}
43. SELECTION SORT
void main()
{
  int s,i,j,temp,a[20];
  clrscr();
  printf("\nEnter size of the array :");
  scanf("%d",&s);
  printf("\nEnter %d elements in to the array:");
  for(i=0;i<s;i++)
            scanf("%d",&a[i]);
            for(i=0;i<s;i++)
            {
                        for(j=i+1;j<s;j++)
                        {
                           if(a[i]>a[j])
                           {
                                    temp=a[i];
                                    a[i]=a[j];
                                    a[j]=temp;
                           }
                        }
            }
  printf("\nThe array after sorting is: ");
  for(i=0;i<s;i++)
            printf(" %d",a[i]);
  getch();
}
44. INSERTION SORT
void main()
{
  int i,j,s,temp,a[20];
  clrscr();
  printf("\nEnter size of the array: ");
  scanf("%d",&s);
  printf("\nEnter %d elements in to the array:",s);
  for(i=0;i<s;i++)
            scanf("%d",&a[i]);
  for(i=1;i<s;i++)
  {
            temp=a[i];
            j=i-1;
            while((temp<a[j])&&(j>=0))
            {
                        a[j+1]=a[j];
                        j=j-1;
            }
            a[j+1]=temp;
  }
  printf("\nAfter sorting the elements are: ");
  for(i=0;i<s;i++)
            printf(" %d",a[i]);
  getch();
}
45. DISPLAY SOURCE CODE AS OUTPUT
#include"stdio.h"
void main()
{
  FILE *p;
  char ch;
  clrscr();
  p=fopen("raja.c","r");
  while((ch=getc(p))!=-1)
            putchar(ch);
  fclose(p);
  getch();
}
46. FACTORIAL OF A NUMBER USING RECURSION
void main()
{
  int num,f;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  f=fact(num);
  printf("\nFactorial of %d is: %d",num,f);
  getch();
}
 int fact(int n)
 {
   if(n==1)
            return 1;
   else
            return(n*fact(n-1));
 }
47. GCD OF A NUMBER USING RECURSION
void main()
{
  int n1,n2,gcd;
  clrscr();
  printf("\nEnter two numbers: ");
  scanf("%d %d",&n1,&n2);
  gcd=findgcd(n1,n2);
  printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
  getch();
}
 int findgcd(int x,int y)
{
  while(x!=y)
  {
            if(x>y)
                        return findgcd(x-y,y);
            else
                        return findgcd(x,y-x);
  }
  return x;
}
48. SUM OF DIGITS OF A NUMBER USING RECURSION
void main()
{
  int num,x;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);
  getch();
}
  int r,s;
  int findsum(int n)
  {
            if(n)
            {
                        r=n%10;
                        s=s+r;
                        findsum(n/10);
            }
            else
                        return s;
  }
49. POWER OF A NUMBER
void main()
{
  int pow,num,i=1;
  long int sum=1;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  printf("\nEnter power: ");
  scanf("%d",&pow);
  while(i<=pow)
  {
            sum=sum*num;
            i++;
  }
  printf("\n%d to the power %d is: %ld",num,pow,sum);
  getch();
}
50. POWER OF A NUMBER USING RECURSION
void main()
{
  int pow,num;
  long int res;
  long int power(int,int);
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  printf("\nEnter power: ");
  scanf("%d",&pow);
  res=power(num,pow);
  printf("\n%d to the power %d is: %ld",num,pow,res);
  getch();
}
  int i=1;
  long int sum=1;
  long int power(int num,int pow)
  {
            if(i<=pow)
            {
                        sum=sum*num;
                        power(num,pow-1);
            }
            else
                        return sum;
}

No comments: