Sunday, 27 December 2015

I BCom CS - C Programs Questions - 31,3,9,11,12,15,16,17,19,21,28,29, 30, 41,34, 45, 46, 47

/*Q31 C Program for Pascal triangle */


#include <stdio.h>

long factorial(int);

void main()
{
       int i, n, c;

      printf("Enter the number of rows you wish to see in pascal triangle\n");
      scanf("%d",&n);

      for (i = 0; i < n; i++)
      {
          for (c = 0; c <= (n - i - 2); c++)
               printf(" ");

          for (c = 0 ; c <= i; c++)
               printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

          printf("\n");
      }

     getch();
}

long factorial(int n)
{
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
         result = result*c;

   return result;
}


Output:
Enter the number of rows you wish to see in pascal triangle
5
                                    1
1               1
   1         2          1
 1        3         3           1
1       4         6          4         1




/*Q3 program to find the total and average of marks in an array*/
#include<stdio.h>
#include<conio.h>
void main()
 {
    int marks[7],i,total=0;
    float avg;
    clrcsr();
    printf("Enter marks of 7 subjects\n");
    for(i=0;i<7;i++)
       scanf("%d",&marks[i]);
    for(i=0;i<7;i++)
       total=total+marks[i];
    avg=(float)total/7;
    printf("\n  Total marks : %d", total);
    printf("\n Average of marks : %f",avg);
    getch();
 }

Output:
Enter marks of 7 subjects
65
75
85
95
60
70
80
Total Marks : 530

Average of marks : 75.714287

/*Q9 program to find the sum of the exponential series 1 + x + x2/2! + x3/3! + ....*/                                                                                         
#include<stdio.h>
#include<conio.h>
void main()
 {
    int n,i;
    float x,sum=1.0,t=1.0;
    clrcsr();
    printf("Enter x value of the series\n");
    scanf("%f",&x);
    printf("Enter n value of the series for number of terms\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
       t=(t*x)/i;
       sum=sum+t;
    }
    printf("\nSum of the exponential series is : %f\n", sum);
    getch();
 }

Output:
Enter x value of the series
1.2
Enter n value of the series for number of terms
5
Sum of the exponential series is : 3.315136

/* Q11  Calculate grades of N students from 3 tests using arrays*/
#include<stdio.h>
#include<conio.h>
void main()
  {
      int n,subj[70],i,j,tot;
      float avg;
      clrscr():
      printf("\nEnter n value for number of students\n");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
         tot=0;
         printf("\n Enter 3 Subjects marks of student[%d]:\n",i);
         for(j=0;j<3;j++)
                scanf("%d",&subj[j]);
         for(j=0;j<3;j++)
                tot=tot+subj[j];
         avg=tot/3.0;
         if(avg>=75)
               printf("\nGrade : A+");
         else if((avg<75)&&(avg>=60))
                printf("\nGrade : A");

         else if((avg<60)&&(avg>=50))
                printf("\nGrade : B");
         else if((avg<50)&&(avg>=40))
                  printf("\nGrade : C");
         else
                  printf("\nGrade : D");
      }
      getch();
   }

Output:
Enter n value for number of students
2
Enter 3 Subjects marks of student[1]:
60
70
80
Grade : A
Enter 3 Subjects marks of student[2]:
80
50
40
Grade : B

/*Q12 C program to find the trace of a matrix (sum of diagonal elements)*/
#include<stdio.h>
#include<conio.h>
void main()
  {
      int matrix[3][3],i,j,sum=0;
      clrscr():
      printf("\nEnter 9 matrix elements\n");
      for(i=0;i<3;i++)
      {
              for(j=0;j<3;j++)
              {
                  scanf("%d",&matrix[i][j]);
              }
      }
      for(i=0;i<3;i++)
      {
              for(j=0;j<3;j++)
              {
                  if(i==j)
                         sum=sum+matrix[i][j];
              }
      }
      printf("\n The trace of a matrix is : %d", sum);
      getch();
  }

Output:
Enter 9 matrix elements
1
2
3
1
2
3
1
2
3
The trace of a matrix is : 6

/* Q15 C program to generate fibonacci series*/
#include<stdio.h>
#include<conio.h>
void main()
  {
      int n,i,f1=0,f2=1,fn;
      clrscr():
      printf("\nEnter n value for generating number of fibonacci values\n");
      scanf("%d",&n);
      printf("\n The Fibonacci Series : \n”);
      printf("\n 0\n 1");
      for(i=2;i<n;i++)
      {
              fn=f1+f2;
              printf("\n %d",fn);
              f1=f2;
              f2=fn;
      }
   getch();
   }

Output:
Enter n value for generating number of fibonacci values
6
The Fibonacci Series :
0
1
1
2
3
5

/* Q16 C program for transpose of a matrix */
#include<stdio.h>
#include<conio.h>
void main()
  {
      int matrix[3][3],i,j;
      clrscr():
      printf("\nEnter 9 matrix elements\n");
      for(i=0;i<3;i++)
      {
              for(j=0;j<3;j++)
              {
                  scanf("%d",&matrix[i][j]);
              }
      }
      printf(" \n The given matrix :\n");
      for(i=0;i<3;i++)
      {
              printf("\n");
              for(j=0;j<3;j++)
              {
                  printf("  %d", matrix[i][j]);
              }
      }
      printf(" \n Transpose of a matrix :\n");
      for(i=0;i<3;i++)
      {
              printf("\n");
              for(j=0;j<3;j++)
              {
                  printf("  %d", matrix[j][i]);
              }
      }

      getch();
  }

Output:
Enter 9 matrix elements
1
2
3
1
2
3
1
2
3
The given matrix :
1   2   3
1   2   3
1   2   3
Transpose of a matrix :
1   1   1
2   2   2
3   3   3


 /* Q17 C program for Tower of Hanoi using Recursion */
  #include <stdio.h>
  #include <conio.h> 
  void towers(int, char, char, char);
 
  void main()
  {
      int num;
      clrscr();
      printf("Enter the number of disks : ");
      scanf("%d", &num);
      printf("The sequence of moves involved in the Tower of Hanoi are :\n");
      towers(num, 'A', 'C', 'B');
      getch();
  }
  void towers(int num, char frompeg, char topeg, char auxpeg)
  {
      if (num == 1)
      {
          printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
          return;
      }
      towers(num - 1, frompeg, auxpeg, topeg);
      printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
      towers(num - 1, auxpeg, topeg, frompeg);
  }

Output:
Enter the number of disks :
3
The sequence of moves involved in the Tower of Hanoi are :
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C

Move disk 1 from peg A to peg C


/*Q19 program to reverse the given number & check if the number is palindrome*/
#include<stdio.h>
#include<conio.h>
void main()
 {
   int num, rev=0, temp,rem;
   clrscr();
   printf("\n Enter an integer number : \n");
   scanf("%d",&num);
   temp=num;
   while(num>0)
    {
       rem=num%10;
       rev=(rev*10)+rem;
       num=num/10;
    }
   if(temp==rev)
       printf("\n The number is palindrome");
   else
       printf("\n The number is NOT a palindrome");
   getch();
 }


Output:
Enter an integer number :
234
The number is NOT a palindrome
Enter an integer number :
232
The number is a palindrome


/* Q 21 Program to find the no. of words, lines, characters and special characters*/
#include<stdio.h>
#include<conio.h>
void main()
 {
    int w=0, lin=0, spc=0, c=0;
    char ch;
    clrscr();
    printf("\n Enter text -  At the end of the text press Ctrl+Z keys and press enter key\n");
    while((ch=getchar())!=EOF)
    {
            if (ch==' ')
                w++;
            else if (ch=='\n')
                {
                   lin++;
                   w++;
                }
            else if(((ch>=65)&&(ch<=90))||((ch>=97)&&(ch<=122)))
                c++;
            else
                spc++;
     }
     printf("\n \nNo. of Words: %d\n No. of Lines: %d\n No. of Characters: %d\n No. of Special Characters: %d",w,lin,c,spc);
     getch();
  }

OUTPUT:
Enter text -  At the end of the text press Ctrl+Z keys and press enter key

St. PiousX Degree & P.G College For Women
Snehapuri Coloney
Nacharam
^Z
No. of Words: 11
No. of Lines: 3
No. of Characters: 55

No. of Special Characters: 2


/* Q 28 Program to swap2 numbers without using 3rd variable */
#include<stdio.h>
#include<conio.h>
void main()
 {
    int a,b;
    clrscr();
    printf("\n Enter two values for swapping\n");
    scanf("%d%d",&a,&b);
    printf("\n Values before swapping :\n a=%d \t b=%d \n",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("\n Values after swapping :\n a=%d \t b=%d \n",a,b);
 getch();
 }

OUTPUT:
Enter two values for swapping
5
10
Values before swapping :
a=5      b=10
Values after swapping :

a=10    b=5


/* Q29 Program to add, subtract, multiply and divide two numbers using functions */
#include<stdio.h>
#include<conio.h>
void add(int x, int y)
     {
       printf("\n SUM : %d",x+y);
     }
void sub(int x, int y)
     {
       printf("\n DIFFERENCE : %d",x-y);
     }
void mul(int x, int y)
     {
       printf("\n PRODUCT : %d",x*y);
     }
void div(int x, int y)
     {
       printf("\n QUOTIENT : %d",x/y);
     }
void main()
{
 clrscr();
 printf("\n The given numbers are x = 10 & y = 5\n");
 add(10,5);
 sub(10,5);
 mul(10,5);
 div(10,5);
 getch();
}

OUTPUT:
The given numbers are x = 10 & y = 5
SUM : 15
DIFFERENCE :5
PRODUCT :50

QUOTIENT :2


/* Q 30 program to generate prime numbers till the given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
  int n,j,i,ch;
  clrscr();
  printf(" Enter the number to generate prime numbers\n");
  scanf("%d", &n);
  printf("\n The list of Prime Numbers Less than %d are :",n);
  printf("\n\t2\n\t3");  // first two prime numbers
  for(i=4;i<=n;i++)   //   generating next prime numbers
  {
    ch=1;
    for(j=2;j<=i/2;j++)
       if(i%j==0)
       ch=0;
    if(ch==1)
     printf("\n\t%d",i);
  }
  getch();

 }
OUTPUT:
Enter the number to generate prime numbers
10
The list of Prime Numbers Less than %d are :
2
3
5
7

/* Q 41 program to convert number from one base to any other base(2,8,10,16)  
 */
#include<stdio.h>
#include<conio.h>
long signed int other_to_decimal(long signed int,long signed int);
long signed int decimal_to_other(long signed int,long signed int);
void main()
{
long signed int inputbase,outputbase,inputnumber,outputnumber,s;
clrscr();
printf("Enter the number and its base:");
scanf("%ld%ld",&inputnumber,&inputbase);
printf("Enter the base you want to convert the number to:");
scanf("%ld",&outputbase);
if(outputbase==10)
{
 outputnumber=other_to_decimal(inputnumber,inputbase);
}

else

{

 if(inputbase==10)

 {
  outputnumber=decimal_to_other(inputnumber,outputbase);
 }
 else
 {
  s=other_to_decimal(inputnumber,inputbase);
  outputnumber=decimal_to_other(s,outputbase);
 }
}
printf("The converted number is %ld",outputnumber);
getch();
}
long signed int other_to_decimal(long signed int inputnumber,long signed int inputbase)
{
long signed int k,op,s,p;                   
op=0;
k=inputnumber;
p=1;
while(k!=0)
{
 s=k%10;
 k=k/10;
 op=op+s*p;
 p=p*inputbase;
}
return op;
}
long signed int decimal_to_other(long signed int outputnumber,long signed int outputbase)
{
long signed int op=0,p,k,s;
k=outputnumber;
p=1;
while(k!=0)
{
s=k%outputbase;
k=k/outputbase;
op=op+s*p;
p=p*10;
}
return op;
}
OUTPUT:
Enter the number and its base:5 10
Enter the base you want to convert the number to:2
The converted number is 101

/*Q 34 Write a simple code for binary search in c programming language

#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0,l,u,mid;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i<n;i++){
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    while(l<=u){
         mid=(l+u)/2;
         if(m==a[mid]){
             c=1;
             break;
         }
         else if(m<a[mid]){
             u=mid-1;
         }
         else
             l=mid+1;
    }
    if(c==0)
         printf("The number is not found.");
    else
         printf("The number is found.");

    return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11

The number is found.

/* Q 47  Write a C program to determine if the year is a leap  year.*/

#include <stdio.h>

int main()
{
  int year;

  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);

  if ( year%400 == 0)
    printf("%d is a leap year.\n", year);
  else if ( year%100 == 0)
    printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 )
    printf("%d is a leap year.\n", year);
  else
    printf("%d is not a leap year.\n", year); 

  return 0;
}

Output:
Enter a year to check if it is a leap year
2012
2012is a leap year


/*Q45  Write a C program to find the GCD of two integer values.*/

#include <stdio.h>

int main() {
  int a, b, x, y, t, gcd, lcm;

  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);

  a = x;
  b = y;

  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
  gcd = a;
  printf("Greatest common divisor of %d and %d is %d\n", x, y, gcd);
  return 0;
}

OUTPUT:

Enter two integers
9
24
common divisor of  9 and 24 is 3



/* Q 46 Write a C program to evaluate the polynomial  shown as
3X2 + 5X2 + 6   if  x=5 */
#include <stdio.h>
#define  f(x)   ((3*x*x)+(5*x*x)+6)
void  main()
{
    printf(“\n The value of  f(5) is %d”, f(5));
    getch();
}

OUTPUT:
The value of  f(5) is 206