Thursday, 10 January 2019

I B.Sc (Computer Science) C - Lab Programs


PROGRAMMING IN C LAB
1.  Find out the given number is perfect number or not  using c program.
2.  Write a C program to check whether the given number is Armstrong or not.
3.  Write a C program to find the sum of individual digits of a positive integer.
4.  A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to print the Fibonacci series
5.  Write a C program to generate the first n terms of the Fibonacci sequence.
6.  Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user.
7.  Write a C program to find both the largest and smallest number in a list of integers.
8.  Write a C program that uses functions to perform the following:
a.   Addition of Two Matrices
b.   Multiplication of Two Matrices
9.  Write a program to perform various string operations
10. Write C program that implements searching of given item in a given list
11. Write a C program to sort a given list of integers in ascending order



1.    Find out the given number is perfect number or not  using c program. 

      Program:
#include<stdio.h>
int main(){
  int n,i=1,sum=0;

  printf("Enter a number: ");
  scanf("%d",&n);

  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

  return 0;
} 

INPUT & OUTPUT:
Enter a number: 6
6 is a perfect number

2.    Write a C program to check whether the given number is Armstrong or not.
#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0){
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

    return 0;
}

INPUT & OUTPUT:
Enter a number: 153
153 is an Armstrong number
3.    Write a C program to find the sum of individual digits of a positive integer.
#include<stdio.h>
int main(){
  int num,sum=0,r;
  printf("Enter a number: ");
  scanf("%d",&num);
  while(num){
      r=num%10;
      num=num/10;
      sum=sum+r;
  }
  printf("Sum of digits of number:  %d",sum);
  return 0;
}

INPUT & OUTPUT:
Enter a number: 123
Sum of digits of number:  6
4.    A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to print the Fibonacci series
#include <stdio.h>
 int main()
{
  int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
  scanf("%d", &n);
   printf("First %d terms of Fibonacci series are:\n", n);
   for (c = 0; c < n; c++)
  {
    if (c <= 1)
      next = c;
    else
    {
      next = first + second;
      first = second;
      second = next;
    }
    printf("%d\n", next);
  }
   return 0;
}
INPUT & OUTPUT:
Enter the number of terms :5
First 5 terms of fibonacci series are 0 1 1 2 3
5.    Write a C program to generate the first n terms of the Fibonacci sequence.
#include<stdio.h>
int f(int);
int main()
{
  int n, i = 0, c;
 scanf("%d", &n);
 printf("Fibonacci series terms are:\n");
   for (c = 1; c <= n; c++)
  {
    printf("%d\n", f(i));
    i++;
  }
   return 0;
}
 int f(int n)
{
  if (n == 0 || n == 1)
    return n;
  else
    return (f(n-1) + f(n-2));
}

INPUT &OUTPUT:
                        5
                        Fibonacci series terms are 0 1 1 2 3
6.    Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
   int n, i, j, count;
   clrscr();
   printf("Prime no.series\n");
   printf("Enter any number\n");
   scanf("%d", &n);
   printf("The prime numbers between 1 to %d\n",n);
   for(i = 1; i <= n; i++)
   {
      count = 0;
      for(j = 1; j <=i; j++)
      if(i % j == 0)
      {
     count++;
      }
      if(count == 2)
      {
     printf("%d\t", i);
      }
   }
   getch();
}

Input & Output:

Prime no. series 
Enter any number 
10
The prime numbers between 1 to 10
2  3  5  7
7.    Write a C program to find both the largest and smallest number in a list of integers.
#include <stdio.h>
#include <conio.h>
void main()
{
   int a[25], i, large, small, n;
   clrscr();
   printf("Enter the size of array(max 25)\n");
   scanf("%d", &n);
   printf("Enter any %d integer array elements\n",n);
   for(i = 0; i < n; i++)
   {
    scanf("%d", &a[i]);
   }
   large = a[0];
   small = a[0];
   for(i = 1; i < n ; i++)
   {
      if(a[i] > large)
      {
    large = a[i];
      }
      if(a[i] < small)
      {
    small = a[i];
      }
   }
   printf("The largest element from the given array is %d \nThe smallest element  from the given array is %d", large, small);
   getch();
}

Input & Output:

Enter the size of array(max 25)
5
Enter any 5 integers array elements  
10 2 3 1 5
The largest element from the given array is 10
The smallest element from the given array is 1
8.     Write a C program that uses functions to perform the following:
a.   Addition of Two Matrices
b.   Multiplication of Two Matrices

a. Addition of Two Matrices

#include <stdio.h>
#include <conio.h>
void main()
{
   int a[3][3], b[3][3], c[3][3], i, j;
   clrscr();
   printf("Enter the elements of 3*3 matrix a \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     scanf("%d", &a[i][j]);
      }
   }
   printf("Enter the elements of 3*3 matrix b \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
    scanf("%d", &b[i][j]);
      }
   }
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     c[i][j] = a[i][j] + b[i][j];
      }
   }
   printf("The resultant 3*3 matrix c is \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     printf("%d\t", c[i][j]);
      }
      printf("\n");
   }
   getch();
}
Input & Output:
Enter the elements of 3*3 matrix  a
1 2 3 4 5 6 7 8 9
Enter the elements of 3*3 matrix  b
1 2 3 4 5 6 7 8 9
The resultant 3*3 matrix  c is
2      4      6
8     10     12
14    16     18

b. Multiplication of Two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
   int a[3][3], b[3][3], c[3][3], i, j, k;
   clrscr();
   printf("Enter the elements of 3*3 matrix a \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     scanf("%d", &a[i][j]);
      }
   }
   printf("Enter the elements of 3*3 matrix b \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
    scanf("%d", &b[i][j]);
      }
   }
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     c[i][j] = 0
     for(k = 0; k < 3; k++)
     {
        c[i][j] = c[i][j] + (a[i][k] * b[k][j])
     }
      }
   }
   printf("The resultant 3*3 matrix c is \n");
   for(i = 0; i < 3; i++)
   {
      for(j = 0; j < 3; j++)
      {
     printf("%d\t", c[i][j]);
      }
      printf("\n");
   }
   getch();
}

INPUT & OUTPUT:

Enter the elements of 3*3 matrix  a
1 2 3 4 5 6 7 8 9
Enter the elements of 3*3 matrix  b
1 2 3 4 5 6 7 8 9
The resultant 3*3 matrix  c is
30    36      42
55    81      96
102   126     150

9.    Write a program to perform various string operations
#include<stdio.h>
#include<string.h>
main()
{
    int a,b;
    char s1[100],s2[100];
    printf("Enter a string.\n");
    scanf("%s",s1);
    //strlen(),strrev(),strcat(),strcpy(),strupr(),strlwr(),strcmp()
    printf("Enter the operation you wish to perform on the string:\n1. String Length\n2. String Reverse\n3. String Concatenation\n4. String Copy\n5. String Upper Case\n6. String Lower Case\n7. String Comparison\n");
    scanf("%d",&a);

    switch(a)
    {
    case 1:
        {
            b=strlen(s1);
            printf("String Length = %d",b);
            break;
        }
    case 2:
        {
            printf("String Reverse = %s",strrev(s1));
            break;
        }
    case 3:
        {
            printf("Enter the target string.\n");
            scanf("%s",s2);
            strcat(s2,s1);
            printf("Result - %s",s2);
            break;
        }
    case 4:
        {
            strcpy(s2,s1);
            printf("Copied String - %s",s2);
            break;
        }
    case 5:
        {
            printf("The Upper Case of String is %s",strupr(s1));
            break;
        }
    case 6:
        {
            printf("The Lower Case of String is %s",strlwr(s1));
            break;
        }
    case 7:
        {
            printf("Enter the string you wish to compare with the previous string.\n");
            scanf("%s",s2);
            a=strcmp(s1,s2);
            printf("String Compare %d",a);
            break;
        }

    default:
        {
            printf("You have not entered a valid option.");
        }
    }

}


INPUT & OUTPUT
Enter  a string.
hi

Enter the operation you wish to perform on the string:
1. String Length
2. String Reverse
3. String Concatination
4. String Copy
5. String Upper Case
6. String Lower Case
7. String Comparison
1
String Length = 2

10. Write C program that implements searching of given item in a given list
#include <stdio.h>
int main()
{
  int array[100], search, c, n;

  printf("Enter number of elements in array\n");
  scanf("%d", &n);

  printf("Enter %d integer(s)\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  printf("Enter a number to search\n");
  scanf("%d", &search);

  for (c = 0; c < n; c++)
  {
    if (array[c] == search)    /* If required element is found */
    {
      printf("%d is present at location %d.\n", search, c+1);
      break;
    }
  }
  if (c == n)
    printf("%d isn't present in the array.\n", search);

  return 0;
}

OUTPUT:
Enter number of elements in array: 5
Enter 5 integers: 50 10 30 62 5
Enter a number to search: 30
30 is present at location 3

11. Write a C program to sort a given list of integers in ascending order

#include<stdio.h>
#include<conio.h>
void main()
{
 int n, a[20], temp, i, j;
 clrscr();
 printf("Enter the size of the array\n");
 scanf("%d", &n);
 printf("Enter the array elements\n");
 for(i = 0; i < n; i++)
 {
  scanf("%d", &a[i]);
 }
 for(i = 0; i < n - 1; i++)
 {
  for(j = 0; j < n - 1; j++)
  {
   if(a[j] > a[j + 1])
   {
    temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
   }
  }
 }
 printf("The sorted array is\n");
 for(i = 0; i < n; i++)
  printf("%d\n", a[i]);
 getch();
}

Input & Output:

Enter the size of the array: 5
Enter the array elements: 50 40 30 20 10
The sorted array is: 10 20 30 40 50

5 comments: