Tuesday, October 30, 2018

CP UNIT-V QUESTIONS

How to define a global constant?
Write a statement to open a file in reading mode.
Explain FILE structure in detail.
Describe formatted input output statements.
What is the problem with getchar ()?
Define Stream. List different types of Streams.
Explain fprintf() and fscanf() functions with an example program.
Define Preprocessor directives. Discuss Marco replacement with an example.
Write a C program that reads characters from the keyboard and writes them to a disk file until the user types a dollar sign.
What is Flushing a Stream? Discuss function like Macro With an example.
Define a file.
What is the use of the preprocessor directives?
Describe the following file functions.
(a) fopen().(b) fclose().(c) getc(). (d) putc(). (e) feof().
How to use fseek() for random access of the file content?
Explain about Macros with an example.
What is a pre-processor?
What role does the fseek() plays and how many arguments does it Have?
Write different built-in (library) functions provided by ‘C’ language for handling I/O operations on files.
What are the features of C preprocessor? Give the differences between macros and functions
Write a sample C program to demonstrate the control string of scanf() function.
Discuss the types of streams.
Write a C program to read name and marks of N number of students from user and store them in a file.
Write a C program to demonstrate the use of fscanf and fprintf functions.
Write a program in C to illustrate the use of rewind and fseek functions.
Write a program in C that reads files and displays them on the screen.

Tuesday, October 23, 2018

CP UNIT 4 QUESTIONS


Define command line arguments.
 Declare a struct name containing field’s first_name, middle_name, last_name within a struct student.
Differentiate between structure and unions.
Write a program using command line to print the statement “I am proud of my country”.
Define function prototype. Give the general syntax of function prototype.
Why do we need structures?

Define structures. Write a C program using functions to return the sum of two complex numbers passed as parameters.
Explain enumerated data types with an example.
Define union. Differentiate between union and structures.
With an example program, explain structure within structure.
What is the role of stack in recursion?
Why to use typedef?
Write a recursive function in C to find the sum of array elements.
Explain the following:(i) Structure with in structure.
(ii) Self reference structure.
Write the recursive function to compute
f(n)=n/2 when n is even f(n)=3n+1 when n is odd
How to pass the structures to functions as an argument? Explain with a suitable example.
Write any two differences between union and structure.
What is the use of typedef?
What is the difference between a structure declaration and a structure initialization?
Define a function & explain why function prototype is essential
Illustrate the need of structures with an example.
Differentiate between structure and union types.
Write a C program to demonstrate the use of array of structures.
What is union? Write a C program to store information in a union and display it
Illustrate recursion with an example.
What is the use of typedef in C?
Explain all the function prototypes with examples.
Demonstrate with a suitable example, the use of structure within a structure.

Monday, October 22, 2018

Unit 3 CP QUESTIONS

Explain call by reference.
What does this statement indicate p = **a; where p and a are variables?
How many maximum arguments can be passes by return ( )?
Write a function to swap two integer elements.
Explain pointers and arrays with some example programs.
Discuss the problems associated with pointers.
 Define type qualifiers
Define Pointer. Write a C program to find the sum of the all elements in given array using pointers.
Define scope. Briefly explain the scope, life time and visibility of Identifier.
 List the different storage classes in C and explain each one of them.
Write a C program to exchange the value of two integers using call by reference.
Give syntax to create a pointer to function.
 What is the role of stack in recursion?
Give a detailed note on pointer expressions.
List and explain the storage classes with examples.
What is meant by storage class of variable?
What is pointer? Give its General form.
What is meant by the scope of variables and summarize types of storage class in C?
Write a program to assign any number at random to an integer variable k and display the same through pointer.
Discuss any two storage class specifiers.
What are the problems with pointers?
Explain dynamic memory allocation functions of C with a suitable example.
Compare call by value with call by reference and explain using a suitable example.
Write a C program for call by reference.
Illustrate with a suitable example the use of return statement.
Explain malloc() function.
Write a C program to calculate the sum of all elements of an array using pointers as arguments.
What is a pointer? Write a program in C to reflect the concept of pointers to functions.

Saturday, October 20, 2018

CP LAB PROGRAMS JNTUA R15

Swapping of two numbers using pointers

#include<stdio.h>
void swap(int*,int*);

int main() {
   int num1, num2;

   printf("\nEnter the first number : ");
   scanf("%d", &num1);
   printf("\nEnter the Second number : ");
   scanf("%d", &num2);

   swap(&num1, &num2);

   printf("\nFirst number  : %d", num1);
   printf("\nSecond number : %d", num2);

   return (0);
}
void swap(int *num1, int *num2) {
   int temp;
   temp = *num1;
   *num1 = *num2;
   *num2 = temp;
}
Enter the first number  : 12
Enter the Second number : 22
First number  : 22
Second number : 12

Factorial of number using recursive function
#include <stdio.h>
int fact(int n);

int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("Factorial of %d = %ld", n, fact(n));
    return 0;
}
 int fact(int n)
{
    if (n >= 1)
        return n*fact(n-1);
    else
        return 1;
}
Enter a positive integer: 6
Factorial of 6 = 720

Finding GCD of two numbers using recursive function
#include <stdio.h>
int gcd(int n1, int n2);
int main()
{
   int n1, n2;
   printf("Enter two positive integers: ");
   scanf("%d %d", &n1, &n2);

   printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1,n2));
   return 0;
}

int gcd(int n1, int n2)
{
    if (n2 != 0)
       return gcd(n2, n1%n2);
    else
       return n1;
}
Enter two positive integers: 366
60
G.C.D of 366 and 60 is 6.

 find LCM of two numbers using recursion


#include <stdio.h>


/* Function declaration */
int lcm(int a, int b);


int main()
{
    int num1, num2, LCM;

    /* Input two numbers from user */
    printf("Enter any two numbers to find lcm: ");
    scanf("%d%d", &num1, &num2);
 
 
    if(num1 > num2)
        LCM = lcm(num2, num1);
    else
        LCM = lcm(num1, num2);
     
    printf("LCM of %d and %d = %d", num1, num2, LCM);
 
    return 0;
}



int lcm(int a, int b)
{
    static int multiple = 0;
 
 
    multiple += b;
    if((multiple % a == 0) && (multiple % b == 0))
    {
        return multiple;
    }
    else
    {
        return lcm(a, b);
    }
}
Enter any two numbers to find lcm
LCM of 12 and 30 = 60
    Length of the string without using string functions
#include <stdio.h>
int main()
{
    char s[1000];
    int i;

    printf("Enter a string: ");
    scanf("%s", s);

    for(i = 0; s[i] != '\0'; ++i);

    printf("Length of string: %d", i);
    return 0;
}
Enter a string: Programming
Length of string: 11
Reversing the string without using string functions
#include<stdio.h>
#include<string.h>

int main() {
   char str[100], temp;
   int i, j = 0;

   printf("\nEnter the string :");
   gets(str);

   i = 0;
   j = strlen(str) - 1;

   while (i < j) {
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
      i++;
      j--;
   }

   printf("\nReverse string is :%s", str);
   return (0);
}
Enter the string  : Pritesh
Reverse string is : hsetirP
Write a Program to count vowels, consonants by entering line of characters
#include <stdio.h>

int main()
{
    char line[150];
    int i, vowels, consonants, digits, spaces;

    vowels =  consonants = digits = spaces = 0;

    printf("Enter a line of string: ");
    scanf("%[^\n]", line);

    for(i=0; line[i]!='\0'; ++i)
    {
        if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
           line[i]=='o' || line[i]=='u' || line[i]=='A' ||
           line[i]=='E' || line[i]=='I' || line[i]=='O' ||
           line[i]=='U')
        {
            ++vowels;
        }
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
        {
            ++consonants;
        }
        else if(line[i]>='0' && line[i]<='9')
        {
            ++digits;
        }
        else if (line[i]==' ')
        {
            ++spaces;
        }
    }

    printf("Vowels: %d",vowels);
    printf("\nConsonants: %d",consonants);
    printf("\nDigits: %d",digits);
    printf("\nWhite spaces: %d", spaces);

    return 0;
}
Output

Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
WAP to read matrix and find sum of its diagonals

/* C Program to find Sum of Diagonal Elements of a Matrix */

#include<stdio.h>

int main()
{
  int i, j, rows, columns, a[10][10], Sum = 0;

  printf("\n Please Enter Number of rows and columns  :  ");
  scanf("%d %d", &i, &j);

  printf("\n Please Enter the Matrix Elements \n");
  for(rows = 0; rows < i; rows++)
  {
  scanf("%d %d", &i, &j);

    for(columns = 0;columns < j;columns++)
    {
      scanf("%d", &a[rows][columns]);
    }
  }
   
  for(rows = 0; rows < i; rows++)
  {
    Sum = Sum + a[rows][rows];
  }

  printf("\n The Sum of Diagonal Elements of a Matrix =  %d", Sum );

  return 0;
}
OUTPUT

Please Enter Number of rows and columns  : 3 3
 Please Enter the Matrix Elements
1 2 3
4 5 6
7 8 9
The Sum of Diagonal Elements of a Matrix=15

Example: Program to Find Transpose of a Matrix
#include <stdio.h>

int main()
{
    int a[10][10], transpose[10][10], r, c, i, j;
    printf("Enter rows and columns of matrix: ");
    scanf("%d %d", &r, &c);

    // Storing elements of the matrix
    printf("\nEnter elements of matrix:\n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("Enter element a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }

    // Displaying the matrix a[][] */
    printf("\nEntered Matrix: \n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("%d  ", a[i][j]);
                printf("\n");
        }

    // Finding the transpose of matrix a
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            transpose[j][i] = a[i][j];
        }

    // Displaying the transpose of matrix a
    printf("\nTranspose of Matrix:\n");
    for(i=0; i<c; ++i)
        for(j=0; j<r; ++j)
        {
            printf("%d  ",transpose[i][j]);
           )
                printf("\n")
        }

    return 0;
}
Output

Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix:
2  3  4

5  6  4


Transpose of Matrix:
2  5

3  6

4  4
Matrix multiplication in C language
#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter elements of first matrix\n");

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

  printf("Enter number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if (n != p)
    printf("The matrices can't be multiplied with each other.\n");
  else
  {
    printf("Enter elements of second matrix\n");

    for (c = 0; c < p; c++)
      for (d = 0; d < q; d++)
        scanf("%d", &second[c][d]);

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of the matrices:\n");

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++)
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  return 0;
}
Example: Program to Find sum of even and odd elements in given matrix
#include <stdio.h>

int main()
{
    int a[10][10], transpose[10][10], r, c, i, j,se=0,so=0;
    printf("Enter rows and columns of matrix: ");
    scanf("%d %d", &r, &c);

    // Storing elements of the matrix
    printf("\nEnter elements of matrix:\n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("Enter element a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }

    // Displaying the matrix a[][] */
    printf("\nEntered Matrix: \n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("%d  ", a[i][j]);
                printf("\n");
        }

 
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
        if(a[i][j]%2==0)
        se=se+a[I][j];
      else
      so=so+a[I][j];

        }
    printf("\nsum of even and odd elements of matrix%d     %d\n",se,so);
 

    return 0;
}
Output

Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix:
2  3  4

5  6  4
nsum of even and odd elements of matrix 16 07

Monday, October 8, 2018

Computer Programming Lab Programs I Sem 2018-19



PRIORITY & ASSOCIATIVITY OF OPERATORS USING EXPRESSIONS
AIM:
The main aim this programs is  To write a C program to understand the priority and associativity of operators using expressions.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
            int r;
            clrscr();
            r=2+3*8;
            printf("\n Result of the above expression is=%d",r);
           r=4*8/2+8;
            printf("\n Result of the above expression is=%d",r);
}
INPUT & OUTPUT:

RESULT:

DIFFERENT LIBRARY FUCNCTIONS IN C
AIM:
The main aim this programs is  to Write a C program to use different library functions of C language
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void main()
{
            int n;
            float f;
            char name[20];
            clrscr();
            printf("\n Enter a number:");
            scanf("%d",&n);
            f=sqrt(n);
            printf("\n Square root of number=%6.4f",f);
            printf("\n Ceil value of above result= %f",ceil(f));
            printf("\n Floor value of above result= %f",floor(f));
            flushall();
            printf("\n Enter a name :");
            scanf("%s",name);
            printf("\n Length of the name=%d",strlen(name));
            printf("\n Given name in uppercase= %s",strupr(name));
            printf("\n Given name in lowercase= %s",strlwr(name));
            printf(" \n Reverse of the name= %s",strrev(name));
}
INPUT & OUTPUT:

RESULT:

GENERATE SERIES OF PRIME NUMBERS
AIM:
The main aim this programs is to write a C program to generate all the prime numbers between 1 and n .
PROGRAM:
#include<stdio.h>
#include<conio.h>
void  main()
{
               int n,i,j,k=0;
               clrscr();
               printf("Enter n value upto where prime numbers to be found: ");
               scanf("%d",&n);
               if(n==0)
               {
                               printf(" \n Invalid number entered");
               }
               printf(" \n Required prime numbers are :");
               for(i=2;i<=n;i++)
                 {
                               k=0;
                               for(j=1;j<=i;j++)
                               {

                                              if(i%j==0)
                                                              k++;
                               }
                                   if(k==2)
                                      printf("\t%d",i);
               }
getch();
}
INPUT & OUTPUT:

RESULT:

FIBONACCI SERIES
AIM:
The main aim this programs is  to write a C program to generate the Fibonacci numbers in the given range.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
            int n,i,f1=0,f2=1,f;
            clrscr();
            printf(" \n Enter how many fibonacci numbers you want to print : ");
scanf("%d",&n);
            printf("\n Fibonacci series is :\n");
            printf("%d\t%d",f1,f2);
            for(i=3;i<=n;i++)
            {
                        f=f1+f2;
                        printf("\t%d",f);
                        f1=f2;
                        f2=f;
            }
            getch();
}
INPUT & OUTPUT:

RESULT:

MAXIMUM & MINIMUM OF SET OF NUMBERS
AIM:
The main aim this programs is  to write a C program to find maximum and minimum of set of numbers.

#include<conio.h>
PROGRAM:
#include<stdio.h>
void main()
{
            int a[20],i,min,max,n;
            clrscr();
            printf("\n Enter no. of elements:");
            scanf("%d",&n);
            printf("\n Enter the elements :");
            for(i=1;i<=n;i++)
                        scanf("%d",&a[i]);
            min=max=a[1];
            for(i=2;i<=n;i++)
            {
                         if(a[i]>max)
                            max=a[i];
                         if(a[i]<min)
                            min=a[i];
            }
            printf("\n Given elements are:\n");
            for(i=1;i<=n;i++)
                        printf("%5d",a[i]);
            printf("\n Maximum Value is=%d",max);
            printf("\n Minimum Value is=%d",min);
            getch();
}
INPUT & OUTPUT:

RESULT:
SUM OF POSITIVE & NEGATIVE NUMBERS IN A GIVEN SET
AIM:
The main aim this programs is  to write a program to find the sum of positive and negative numbers in a given set of numbers.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
            int a[20],n,sp=0,sn=0,i;
            clrscr();
            printf("\n Enter size of the array :");
            scanf("%d",&n);
            printf("\n Enter the values of array :");
            for(i=1;i<=n;i++)
                        scanf("%d",&a[i]);
            for(i=1;i<=n;i++)
            {
                        if(a[i]<0) // negative
                                    sn=sn+a[i];
                        else // positive
                                    sp=sp+a[i];
            }
            printf("\n In given numbers,sum of positive values is :%d",sp);
            printf("\n In given numbers,sum of negative values is :%d",sn);
}
INPUT & OUTPUT:

RESULT:

SUM OF SERIES
AIM:
The main aim this programs is  to write a program to evaluate the sum of the given series = 1+x+/2!+/3!+/4!+----
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
            float sum=1.0;
            int n,x,i,fact=1;
            clrscr();
            printf("\n enter x and n values:");
            printf("\n x=");
            scanf("%d",&x);
            printf("\n n=");
            scanf("%d",&n);
            for(i=1;i<n;i++)
            {
                        fact=fact*i;
                        sum=sum+(float)pow(x,i)/fact;
            }
            printf("\n sum of the given series is %f",sum);
            getch();
}
INPUT & OUTPUT:

RESULT: