c programs for under graduates

Upload: userscrybd

Post on 04-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 C Programs for Under Graduates

    1/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 1

    1.AIM:To write a program to find the area and circumference of the circle.

    ALGORITHM:

    Step-1 Start the program.Step-2 Input the radius of the circle.Step-3 Find the area and circumference of the circle using the

    formulaArea=3.14*r*rCircumference=2*3.14*r

    Step-4 Print the area and the circumference of the circleStep-5 Stop

    PROGRAM:

    /*TO FIND THE AREA AND CIRCUMFERENCE OF THE CIRCLE*/

    #includemain(){

    float rad,area,circum;printf( \nEnter the radi us of the circle); scanf(%f,&rad); area=3.14*rad*rad;circum=2*3.14*rad;printf( \ nArea=%f,area); printf( \ nCircumference=%f,circum);

    }

    SAMPLE INPUT AND OUTPUT:

    Enter the radius of the circle5Area=78.500000Circumference=31.400000

    2. AIM:To write a program to insert an element in an array in the given position

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the size of the arrayStep-3 Enter the elements of the arrayStep-4 Print the elements of the arrayStep-5 Enter the element to be inserted and its position in the

    arrayStep-6 Set a loop up to the position you enterStep-7 Push the order of the position by one, which are greater,

    then the position you entered

  • 8/13/2019 C Programs for Under Graduates

    2/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 2

    Step-8 Insert the element in the position entered by youStep-9 Print the array after insertion of the elementStep-10 Stop

    PROGRAM:

    /*INSERTING AN ELEMENTS INTO THE VECTOR*/#includemain(){

    int a[100],no,i,pos,item;printf( \ nEnter the size of the matrix); scanf(%d,&no); printf( \nEnter the ele ments of the matrix); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    3/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 3

    Step-4 Set a loop up to the array size minus oneStep-5 Set a inner loop up to the size of the arrayStep-6 Check whether the next array element is greater than or notStep-7 If greater than exchange their positionStep-8 If not greater than then go to the loopStep-9 After the execution of the inner loop the outer loop is

    executedStep-10 Print the ascending order of the given arrayStep-11 Print the descending order of the given arrayStep-12 Stop

    PROGRAM:

    /*ASCENDING AND DESCENDING ORDER OF THE GIVEN NUMBERS*/#includemain(){

    int num[100],no,i,j,a;printf(Enter Upper Limit...); scanf(%d,&no); printf(Enter the numbers); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    4/51

  • 8/13/2019 C Programs for Under Graduates

    5/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 5

    Step-4 Set a loopStep-5 Find the value of cosine using the formula

    temp=temp*pow((double)(-1),(double)(2*i-1))*x*x/(2*i*(2*i-1));sum=sum+temp;

    Step-6 After the execution of the loop print the cosine valueStep-7 Stop

    PROGRAM:

    // TO FIND THE COSINE VALUE#include#includemain(){

    float x,a,sum,temp;int i,no=20,mul;printf( \nEnter the value o f x);

    scanf(%f,&x); a=x;x=x*3.1412/180;temp=1;sum=1;for(i=1;i

  • 8/13/2019 C Programs for Under Graduates

    6/51

  • 8/13/2019 C Programs for Under Graduates

    7/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 7

    Enter the number 5The factorial of 5 is 120

    8.AIM:To write a program to convert the Celsius into Fahrenheit

    ALGORITHM:

    Step-1 Start the program.Step-2 Enter the Celsius value.Step-3 Calculate the Fahrenheit value by using the formula given

    below.Fahreheit=(1.8*Celsius)+32

    Step-4 Print the Fahrenheit valueStep-5 Stop

    PROGRAM:

    //CONVERT THE CELCIUS INTO FAHRENTEIET#includemain(){

    float cel,faren;printf(Enter the Celsius value...); scanf(%f,&cel); faren=(1.8*cel)+32;printf(The fahrenteiet value of the given %f celsius value is

    %f,cel,faren); }

    SAMPLE INPUT AND OUTPUT

    Enter the Celsius value...45The fahrenteiet value of the given 45.000000 celsius value is 113.000000

    9.AIM:To write a program to generate the fibbonaci series

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the numberStep-3 Check the number whether the number is zero or not. If zero

    print Zero value. If not zero go further.Step-4 Set a loop up to the given number.Step-5 fib=fib+a;

    a=b;b=c;

    Step-6 Every increment in the loop prints the value of fib.Step-7 After the execution of the loop stops the program

    PROGRAM:

  • 8/13/2019 C Programs for Under Graduates

    8/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 8

    //TO PRINT THE FIBBONACI SERIES UPTO GIVEN NUMBERS#includemain(){

    int num,fib=0,a=0,b=1,i;printf(Enter the number); scanf(%d,&num); printf( \n FIBBONACI SERIES\ n); if(num==0)

    printf(0); else{

    for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    9/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 9

    while((c=getchar())!=EOF)putc(c,fp);

    fclose(fp);printf(The entered data is....); fp=fopen(Write,r); while((c=getc(fp))!=EOF)

    printf(%c,c); fclose(fp);

    }

    SAMPLE OUTPUT:

    Enter the text.... Welcome to C language ^Z The entered data is.... Welcome to C language

    11.AIM:Program to illustrate functions without arguments and no return values.

    ALGORITHM:

    Step-1 Start the programStep-2 Declare the functionStep-3 Call the functionStep-4 Enter the StringStep-5 Print the stringStep-6 End the program in the calling function

    PROGRAM:

    //FUNCTIONS WITH OUT ARGUMENTS AND NO RETURN VALUES#includemain(){

    void message(void);message();

    }void message(){

    char str[100];printf(Enter a string........); scanf(%s,str); printf(WELCOME TO...%s,str);

    }

    SAMPLE OUTPUT:

    Enter a string LAKWELCOME TO LAK

    12.AIM:To write a program to illustrate function with arguments and no return

    value.

  • 8/13/2019 C Programs for Under Graduates

    10/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 10

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the two numbersStep-3 Call the function with arguments passing to the calling

    functionStep-4 Add the two numbers in the calling functionStep-5 Print the addition of two valuesStep-6 End the program in the calling function

    PROGRAM:

    //FUNCTIONS WITH ARGUMENTS BUT NO RETURN VALUES#includemain(){

    int a,b;printf(Enter two numbers....); scanf(%d%d,&a,&b); add(a,b);

    }add(int a,int b){

    int c;c=a+b;printf(The addition of two numbers %d and %d is %d,a,b,c);

    }

    SAMPLE OUTPUT

    Enter two numbers....10 20The addition of two numbers 10 and 20 is 30

    13.AIM:Program to illustrate parameter passed to the function.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the size of the arrayStep-3 Enter the elements of the arrayStep-4 Print the array elementsStep-5 Call the function with base address of the array passed to itStep-6 In the calling function gets the base address in the pointer

    variableStep-7 Add the array elementsStep-8 Return the value from the calling function to the variable in

    the called functionStep-9 Print the sum in the called functionStep-10 End the program in the main function

    PROGRAM:

  • 8/13/2019 C Programs for Under Graduates

    11/51

  • 8/13/2019 C Programs for Under Graduates

    12/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 12

    //FUNCTION WITH ARGUMENTS AND RETURN VALUES#includemain(){

    int a,b,c;printf(Enter the two numbers...); scanf(%d %d,&a,&b); c=add(a,b);printf(The addition of two numbers %d and %d is %d,a,b,c);

    }add(int x,int y){

    int z;z=x+y;return(z);

    }

    SAMPLE OUTPUT:

    Enter the two numbers... 5 6The addition of two numbers 5 and 6 is 11

    15.AIM:To write a program to find the largest and smallest of the given array.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the size of arrayStep-3 Enter the elements of the array

    Step-4 Print the array elementsStep-5 Initialize the large and small is equal to the first elementof the array

    Step-6 Set a loop up to the array sizeStep-7 Check the next element greater then the larger.

    If greater then as sign next element to the largeStep-8 Check the next element smaller then the larger. If smaller

    then assign next element to the smallStep-9 Print the value of large and small after the execution of the

    loopStep-10 Stop

    PROGRAM:

    //FIND THE LARGEST AND SMALLEST OF THE GIVEN ARRAY#includemain(){

    int a[100],i,small,large,no;printf(In how many numbers you want to find....); scanf(%d,&no); printf(Enter the elements of the array....); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    13/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 13

    scanf(%d,&a[i]); printf( \ nThe elements of the array ); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    14/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 14

    printf(Enter the rows and column of two matrixes... \ n); scanf(%d %d,&m,&n); printf( \nEnter the elements of A matrix. ..); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    15/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 15

    2 4 68 10 1214 16 18

    17.AIM:To write a program to multiply two matrixes.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the row and column of the A matrixStep-3 Enter the row and column of the B matrixStep-4 Enter the elements of the A matrixStep-5 Enter the elements of the B matrixStep-6 Print the elements of the A matrix in matrix formStep-7 Print the elements of the B matrix in matrix formStep-8 Set a loop up to rowStep-9 Set a inner loop up to columnStep-10 Set another inner loop up to columnStep-11 Multiply the A and B matrix and store the element in the C

    matrixStep-12 Print the resultant matrixStep-13 Stop

    PROGRAM:

    // MULTPLICATION OF TWO MATRIX#includemain(){

    int a[25][25],b[25][25],c[25][25],i,j,k,r,s;int m,n;printf( \ nEnter the Rows and Columns of A matrix...); scanf(%d %d,&m,&n); printf( \nEnter the Rows an d Columns of B matrix...); scanf(%d %d,&r,&s); if(m!=r)

    printf( \ nThe matrix cannot multiplied); else{

    printf( \ nEnter the elements of A matrix); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    16/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 16

    for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    17/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 17

    To find sum of Digits, Reverse and the given Number is Palindrome or not.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the numberStep-3 Set a loop upto the number is not equal to zeroStep-4 Find the digit of the numberStep-5 Find the sum of digitStep-6 Find the reverse numberStep-7 After the end of the loop print the sum and reverse number of

    the digitStep-8 Find whether the reverse number is equal to the given

    number or not. If equal the number is palindromeStep-9 If not equal the given number is not palindromeStep-10 Stop

    PROGRAM:

    /* PROGRAM TO FIND THE SUM AND REVERSE OF THE GIVEN NUMBER*/#includemain(){

    unsigned long int a,num,sum=0,rnum=0,rem;printf( \ nEnter the number...); scanf(%ld,&num); a=num;while(num!=0){

    rem=num%10;sum=sum+rem;rnum=rnum*10+rem;num=num/10;

    }printf( \nThe sum of the digits of %ld is %ld\ n,a,sum); printf( \ nThe reverse number of the %ld is %ld,a,rnum); if(a==rnum)

    printf( \nThe given number is a pali ndrome); else

    printf( \ nThe given number is not a palindrome); }

    Sample output:

    Enter the number...12321The sum of the digits of 12321 is 9The reverse number of the 12321 is 12321The given number is a palindrom

    19.AIM:To write a program to find the roots of the quadratic equation.

  • 8/13/2019 C Programs for Under Graduates

    18/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 18

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the value of a, b, cStep-3 Find the value of d by using the formula D=b*b-4*a*cStep-4 If D is greater then or equal to zero then find the two roots

    as root1=(-b+sqrt(d))/(2*a);root2=(-b-sqrt(d))/(2*a);Print the two roots

    Step-5 If the D is not greater then or equal to zero then print thestatement the roots are imaginary.

    Step-6 Stop

    PROGRAM:

    // To find the roots of the quadratic equation#include

    #includemain(){

    int a,b,c,d;float root1,root2;printf(Enter the values of a,b,c \ n); scanf(%d %d %d,&a,&b,&c); d=b*b-4*a*c;if(d>=0){

    root1=(-b+sqrt(d))/(2*a);root2=(+b+sqrt(d))/(2*a);printf(The roots of the values a=%d,b=%d,c=%d

    are\ n %f %f,a,b,c,root1, root2); }else

    printf(The roots are imagenary); }

    SAMPLE OUTPUT:

    Enter the values of a,b,c 1 0 -9The roots of the values a=1,b=0,c=-9 are 3.000000 3.000000

    20.AIM:To write a program to find the factorial of the given number using

    recursion

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the numberStep-3 Call the recursive function passing the number to the

    recursive function as an argument.Step-4 If the entered number is equal to one then return one to main

    function.

  • 8/13/2019 C Programs for Under Graduates

    19/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 19

    Step-5 If the number is less greater then one then call recursiveStep-6 Print the factorial value of the number.Step-7 Stop

    PROGRAM:

    #includemain(){

    int num,a;printf(Enter the number); scanf(%d,&num); a=recur(num);printf(The factorial of the number %d is %d,num,a);

    }recur(int no){

    int fact=1;if(no==1)

    return(1);else

    fact=no*recur(no-1);}

    SAMPLE OUTPUT:

    Enter the number 5The factorial of the number 5 is 120

    21.AIM:To write a program to find whether the string is palindrome or not.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the stringStep-3 Find the string length using the strlen() functionStep-4 Print the string lengthStep-5 Set a loop up to the half of the string lengthStep-6 Compare every character above the middle character with the

    below character of the middle characterStep-7 If any character equal prints the given string is palindromeStep-8 If the character is not equal then print the given string is

    not a palindromeStep-9 Stop

    PROGRAM:

    //TO FIND WHETHER THE GIVEN STRING IS PALINDROME OR NOT#include#includemain(){

  • 8/13/2019 C Programs for Under Graduates

    20/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 20

    int len=0,i,j;char name[25];printf(Enter the string...); scanf(%s,name); while(name[len]!= \0')

    len++;printf( \ n%d,len); for(i=0,j=len-1;i

  • 8/13/2019 C Programs for Under Graduates

    21/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 21

    getch();}

    SAMPLE OUTPUT:

    Enter the string RAJAThe number of vowels is 2The number of consonants is 2

    23.AIM:To write the program to transpose the given matrix.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the row and column of the matrixStep-3 Enter the elements of the matrixStep-4 Print the elements of the matrix in the matrix formatStep-5 Set the loop up to rowStep-6 Set the inner loop up to columnStep-7 Print the matrix elements in the row wiseStep-8 Stop

    PROGRAM:

    //TRANSPOSE OF GIVEN MATRIX#includemain(){

    int i,j,a[25][25],row,col;

    printf( \ nEnter the number of rows and column of matrix); scanf(%d%d,&row,&col); printf( \ nEnter the elements of the matrix); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    22/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 22

    Enter the number of rows and column of matrix 3 3Enter the elements of the matrix 1 2 3 4 5 6 7 8 9The given matrix1 2 34 5 67 8 9The transpose of the given matrix1 4 72 5 83 6 9

    24.AIM:To write a program to find the sine value for the entered value.

    ALGORITHM

    Step-1 Start the programStep-2 Enter the values x and nStep-3 Convert the value x into radiansStep-4 Set a loop up to nStep-5 Find the value of the sine by using the formulaStep-6 Print the value of sine x after the execution of the loopStep-7 stop

    PROGRAM:

    // SINE SERIES#include#include

    main(){int no,i;float x,a,sum,b;printf(Enter the numbers); scanf(%f %d,&x,&no); b=x;x=x*3.141/180;a=x;sum=x;for(i=1;i

  • 8/13/2019 C Programs for Under Graduates

    23/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 23

    Sin(30.000000) value is 0.499914

    25.AIM:To write a program to print the student name, roll no, average mark and

    their grades.

    ALGORITHM:

    Step-1 Start the programStep-2 Initialize the structure variableStep-3 Enter the number of studentStep-4 Set a loop up to the number of studentStep-5 Enter the student name, roll no, average marksStep-6 Find their gradesStep-7 Print the student name, roll no, average and their gradeStep-9 Stop

    PROGRAM:

    //STUDENT RECORD USING POINTER AND STRUCT#includemain(){

    struct student{

    char name[25];char regno[25];int avg;char grade;

    } stud[50],*pt;

    int i,no;printf(Enter the number of the students...); scanf(%d,&no); for(i=0;iavggrade=C;

    else if(pt->avggrade=B;

    else

  • 8/13/2019 C Programs for Under Graduates

    24/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 24

    pt- >grade=A; }printf( \ n); printf(NAME REGISTER -NO AVERAGE GRADE\ n); for(pt=stud;ptname,pt->regno);printf(%10d \t %c\ n,pt ->avg,pt->grade);

    }}

    SAMPLE OUTPUT:

    Enter the number of the students3student[1] information:Enter the name MUNI

    Enter the roll no of the student 100Enter the average value of the student 95student[2] information:Enter the name LAKEnter the roll no of the student 200Enter the average value of the student 55student[3] information:Enter the name RAJAEnter the roll no of the student 300Enter the average value of the student 25NAME REGISTER-NO AVERAGE GRADEMUNI 100 95 ALKA 200 55 BRAJA 300 25 D

    26.AIM: To write a program to swap two number without using pointer

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the two numbersStep-3 Call the display functionStep-4 Pass the address of the two numbers to the calling functionStep-5 Get the address in the calling function in the pointerStep-6 Swap the number using temporary variableStep-7 Print the swamped values in the main functionStep-8 Stop

    PROGRAM:

    // SWAP THE NUMBER USING THE POINTER#includemain(){

    int x,y;

  • 8/13/2019 C Programs for Under Graduates

    25/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 25

    printf( \ nEnter the two numbers); scanf( %d %d,&x,& y);printf( \nThe entered number for x and y are,x=%d\ ty=%d,x,y); display(&x,&y);printf( \nAfter the swapping the value of,x=%d\ ty=%d,x,y);

    }display(int *a,int *b){

    int t;t=*a;*a=*b;*b=t;

    }

    SAMPLE OUTPUT:

    Enter the two numbers...10 20

    The entered number for x and y are, x=10 y=20After the swapping the value of, x=20 y=10

    27.AIM:To write a program to find the string length and concatenation of string.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the stringStep-3 Find the string length using the function strlen()Step-4 Print the string length of the entered stringStep-5 concatenation the two string using the function strcat()

    Step-6 Print the concatenated stringStep-7 Stop

    PROGRAM:

    // TO FIND THE STRING LENGTH OF THE STRING#include#includemain(){

    char str1[50],str2[]= WELCOME; int len;printf(Enter the string...); scanf(%s,str1); printf( \ nThe string length of %s is %d,str1,strlen(str1)); printf( \nTheconcatenation string length is %d and its string is

    %s,strlen(str1),strcat(str1,str2)); }

    SAMPLE OUTPUT:

    Enter the string... LAKThe string length of lak is 3

  • 8/13/2019 C Programs for Under Graduates

    26/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 26

    The concatenation string length is 16 and its string is LAK WELCOME W

    28.AIM: To write a program to print the pascal triangle

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the number of linesStep-3 Construct a loop up to the given numberStep-4 Construct another inner loopStep-5 Pirnt the numberStep-6 After the execution of the loopStep-7 Stop

    PROGRAM:

    //CONSTRUCT PASCAL TRIANGLE#includemain(){

    int noline,i,j,temp;printf(Enter the number of lines to print); scanf(%d,&noline); for(i=1;i

  • 8/13/2019 C Programs for Under Graduates

    27/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 27

    Step-1 Start the programStep-2 Enter the numberStep-3 set a loop up to number-1Step-4 Check the number is divide by any number other than one and

    the number itselfStep-5 If divide absolutely than print the number is not a primeStep-6 If not divide other then one and itself then print the number

    is prime.Step-7 Stop

    PROGRAM:

    //FIND THE GIVEN NUMBER IS PRIME OR NOT#include main(){

    int num,i=2;

    pr intf(Enter the number...); scanf(%d,&num); while(i

  • 8/13/2019 C Programs for Under Graduates

    28/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 28

    //REVERSE THE STRING USING THE RECURSION#includemain(){

    int len;void rev();printf(Enter the string length); scanf(%d,&len); printf( \ n%d,len); printf( \ n); rev(len);printf( \ n);

    }void rev(len)int len;{

    char c;if(len==1){

    c=getchar();c=getchar();putchar(c);

    }else{

    c=getchar();c=getchar();rev(-len);putchar(c);

    }return;}

    SAMPLE OUTPUT:

    Enter the string length 44A M A RR A M A

    31.AIM:To write a program to find the size of the data types.

    ALGORITHM:

    PROGRAM:

    //TO FIND THE SIZE OF DATAS#includemain()

  • 8/13/2019 C Programs for Under Graduates

    29/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 29

    {int i=10;float f=25.005;char name[]=welcome; printf( \ n The size of integer is %d,sizeof(i)); printf( \ n The size of float is %d,sizeof(f)); printf( \n The size of character i s %d,sizeof(name));

    }

    SAMPLE OUTPUT:

    The size of integer is...2The size of float is... 4The size of character is...8

    32.AIM:To find the sum and average of the given array

    ALGORITHM:

    PROGRAM:

    //FIND THE SUM AND AVERAGE OF THE GIVEN NUMBERS#includemain(){

    int a[100],i,no,sum=0;float avg=0;printf( \ nEnter the number of elements); scanf(%d,&no); printf(Enter the numbers); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    30/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 30

    ALGORITHM:

    PROGRAM:

    //CONVERT THE LOWER CASE TO UPPER CASE LETTER#includemain(){

    int i=0;char str[100];printf(Enter the string); scanf(%s,str);

    while(str[i]!= \0'){

    printf(%c,toupper(str[i])); i++;

    }}

    Sample Ouput

    Enter the string GuRuGURU

    34.AIM:To write a program to find the largest of the three numbers.

    ALGORITHM:

    Step-1 Start the programStep-2 Enter the three numbersStep-3 Assign large to first numberstep-4 Check the next number is greater then the large.

    If greater then assign large to next numberStep-5 Compare the next number with largeStep-6 Do the step-4Step-7 Print the larger value of the three numberStep-8 Stop

    PROGRAM:

    //TO FIND THE LARGEST OF THE THREE NUMBERS#includemain(){

    int a,b,c,big;printf(Enter the three numbers);

  • 8/13/2019 C Programs for Under Graduates

    31/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 31

    scanf(%d %d %d,&a,&b,&c); big=a;if(big

  • 8/13/2019 C Programs for Under Graduates

    32/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 32

    mean=avg(lis,no);printf( \n Mean of %3d elements is%10.2f\ n,no,mean); for(i=0;i

  • 8/13/2019 C Programs for Under Graduates

    33/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 33

    Step-13 If you choose the choice 3. Find whether the number is odd oreven

    Step-14 If the number is divide by 2 then print the number is evenStep-15 If the number is not dividing by 2 then print the number is

    oddStep-16 If you choose the choice 4.Exit the programStep-17 Continue the program

    PROGRAM:

    // MENU DRIVEN PROGRAM#includemain(){

    int num,o,fact=1,i;while(1){

    printf( \nE nter the number ); scanf(%d,&num); printf( \ nchoose one of the options given below); printf( \n1.Factorial of the given number\n2.Prime number or

    not\n3.Odd or Even \ n4.Exit); scanf(%d,&o); switch(o){

    case 1:for(i=1;i

  • 8/13/2019 C Programs for Under Graduates

    34/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 34

    }

    SAMPLE OUTPUT:

    Enter the number .. 5Choose one of the options given below1. Factorial of the given number2. Prime number or not3. Odd or Even4. Exit1The factorial of 5 is 120Enter the number..5Choose one of the options given below1.Factorial of the given number2.Prime number or not3.Odd or Even

    4.Exit2The given number is a primeEnter the number...5Choose one of the options given below1.Factorial of the given number2.Prime number or not3.Odd or Even4.Exit4

    37. AIM :To print Magic Suare numbers.

    Algorithm

    Step-1 Start the programStep-2 Declare x, y and z variables as int data type. Step-3 for i=1 to less than 10, x = x+ 1Step-3.1 for y:=z to less than or equal to 10, y =y+zStep-3.1.1 for z = 10 to less than or equal to 100, z=z+10

    print the Magic Square print x -z, x+z- y, x+y print x+y+z, x, x -y- z print x -y, x+y- z, x+z

    Step-4 Stop the program

    PROGRAM

    #include #includemain ( ){

    int x, y, z;for (x=0; x

  • 8/13/2019 C Programs for Under Graduates

    35/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 35

    for (z=9; z

  • 8/13/2019 C Programs for Under Graduates

    36/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 36

    SAMPLE OUTPUT

    Type N-no of lines in triangle 31

    2 2 23 3 3 3 3

    39.AIM:Program to find whether a year is Leap year.

    ALGORITHM

    PROGRAM

    #includemain(){

    int ye;printf ("Enter the year \n");scanf("%d", &ye);if (ye%4==0)

    printf("It is a Leap Year \n");else

    printf("It is Not a Leap Year\n");}

    SAMPLE OUTPUT

    Enter the year 2000It is a Leap Year

    40. AIM:Program to print the following Triangle.5

    4 53 4 52 3 4 51 2 3 4 5

    ALGORITHM

    Step-1 Start the programStep-2 Declare i, j and n as int data type Step-3 Read the number of linesStep-4 for i=n to greater than or equal to 0 Step-4.1 for j=i to less than n print y Step-5 Stop the program

    PROGRAM

    #include

  • 8/13/2019 C Programs for Under Graduates

    37/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 37

    main(){

    int i, j, n;printf("Enter number of lines\n");scanf("%d", &n);printf("\n\n\n");for (i=1;i

  • 8/13/2019 C Programs for Under Graduates

    38/51

  • 8/13/2019 C Programs for Under Graduates

    39/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 39

    printf(" Matrix \n") ;printf ("--------------------------\n");for (i=0;i

  • 8/13/2019 C Programs for Under Graduates

    40/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 40

    main(){

    int i,j,a[10][10],m,n,flag=1;printf("Enter the order of the matrix (m,n).....");scanf("%d%d", &m, &n);printf("\nEnter the matrix row wise.....");for (i=0; i

  • 8/13/2019 C Programs for Under Graduates

    41/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 41

    Step-12.1 Compute Sq[i]=Square (vec[i])Large=max (large, vec(i))

    Step-13 print Largest array element Step-14 print Element and its SquareStep-15 Stop the program.

    PROGRAM

    #include#define square(x) ((x)*(x))#define loop(index, max) for(index=0; index < max; index++)#define max(x, y) (((x)>(y))? (x):(y))

    main(){

    int a, b, i=0, n, large, vec[10], sq[10];

    printf("Program to compute : \n");printf("1. largest element in the array.\n");printf("2. square of each array element.\n\n");printf("Enter Size of the array..... ");scanf("%d", &n);printf("\nEnter %d elements of the array\n", n);loop(i,n)

    scanf ("%d", &vec[i]);loop(i, n)

    printf("%5d", vec[i]);large =0;loop(i,n){

    sq[i] = square(vec[i]);large = max(large, vec[i]);

    }printf("\n\nLargest array element is : %5d", large);printf("\nElement Square \n");printf ("----------------- \n");loop(i,n)

    printf("%5d %8d\n", vec[i], sq[i]);}

    SAMPLE OUTPUT

    Program to compute :1. largest element in the array.

    2. square of each array element.Enter Size of the array..... 5Enter 5 elements of the array 1 2 3 4 5

    1 2 3 4 5Largest array element is : 5Element Square-------------------------1 12 43 9

  • 8/13/2019 C Programs for Under Graduates

    42/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 42

    4 165 25

    45. AIM:Program to delete duplicates in a Vector

    ALGORITHM

    Step-1 Start the programStep-2 Declare the variables i, j, k, n, num and flag=0

    and declare array a(50) as Float data type. Step-3 Enter the size of vectorStep-4 Initialize num=nStep-5 Enter vector elements

    Step- 5.1 for i=0 to less than n Read a[i] Step-6 Print vector elements

    Step-6.1 for i=0 to less than n print a[i] Step-7 Removing duplicatesStep-7.1 for i=0 to less than n - 1 Step-7.1.1 for j = i+1 to les s than n Step-7.1.1 Check if (a[i]=a[j]) n=n-1Step-7.1.1 flag = 1Step-7.1.1 j = j-1Step-8 Check if (flag==0)

    Print No duplicates found in vector Step-9 Else

    Print Vector has no. of duplicates and print it Step- 10 Print Vector after deleting duplicates

    Step- 10.1 for i=0 to less than n printa[i] Step-11 Stop the program.

    PROGRAM

    #includemain(){

    int i,j,k,n, num, flag=0;float a[50];printf("Size of vector?");scanf("%d", &n);num = n;printf("\nEnter Vector elements ?\n");

    for (i=0; i

  • 8/13/2019 C Programs for Under Graduates

    43/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 43

    flag = 1;j--;

    }}

    if (flag==0)printf("\nNo duplicates found in vector \n");

    else{

    if((num-n)==1){

    printf("\nVector has only one duplicate\n");printf("Vector after deleting duplicates : \n");for (i=0;i

  • 8/13/2019 C Programs for Under Graduates

    44/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 44

    Step-12 for i=0 to less than nStep-12.1 print a[i]

    Step-13 Stop the Program

    PROGRAM

    #includemain(){

    int i,k,n, pos;float a[50], item;printf("Enter Size of Array : ");scanf("%d", &n);printf("\nEnter Array Elements : ");for (i=0; i=pos;k--)

    a[k] = a[k-1];a[--pos] = item;

    printf("\nArray of Elements after insertion :\n");for (i=0;i

  • 8/13/2019 C Programs for Under Graduates

    45/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 45

    Step-6 for i=0 to less than n Step-6.1 for j=0 to less than m Step-6.1.1 if (ij)

    print a[i][j] Lower Triangular Matrix Step-7 for i=0 to less than n Step-7.1 for j=0 to less than n Step-7.1.1 check if (ij) Print Step-8 Stop

    PROGRAM

    #includemain(){

    int a[10][10], i, j, m, n;printf("Enter Row and Column of the Matrix : ");scanf("%d %d", &n, &m);printf("\nEnter Elements of matrix : ");for(i=0; i

  • 8/13/2019 C Programs for Under Graduates

    46/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 46

    Program for Binary search using Recursion

    ALGORITHM:

    Step-1 Start the programStep-2 Declare an array a[50], i, n and loc as int data type Step-3 Declare bin( ) function as int data type Step-4 Enter the size of matrixStep-5 for i=0 to less than n

    Step-5.1 Read and placed in a[i]Step-6 for i=0 to less than n

    Step- 6.1 print a(i) Step-7 Enter element to be searchedStep-8 Assign loc=bin(a,o,n)Step-9 Check if (loc==0)

    Step- 9.1 Print unsuccessful search %d not found Step-10 else

    Step-10.1 Print Successful search found. Step-11 Stop the program

    Recursive Function

    Step-1 Global declaration of an array b(50) low and highStep-2 Declare mid as static int of local declaration and i as

    int data type. Step-3 mid=(low+high)Step-3.1 check if (key

  • 8/13/2019 C Programs for Under Graduates

    47/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 47

    if(loc==0)printf("Unsuccessful search. %d not found. \n", key);

    else{

    printf("Successful search.\n");printf("%d found at position %d. \n", key, loc);

    }}int bin(int b[],int low,int high){

    static int mid;int i;if(low

  • 8/13/2019 C Programs for Under Graduates

    48/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 48

    Step- 7.1 print Entracted string is Empty Step-8 elseStep-8.1 if (pos+count>len)Step-8.1.1 print Characters to be extracted exceed Length Step-8.1.2 number = len-pos+1Step-8.2 elseStep-8.2.1 num=countStep-8.3 j=0Step-8.4 for i = --pos to less than or equal to pos+num-1Step-8.4.1 substr[j] = mainstr[i]Step-8.4.2 j = j+1Step-8.5 print Substring Step-9 Stop the program

    PROGRAM

    #includemain(){

    char mainstr[50], substr[50];int count, pos, i, j, len, num;printf("Enter the main string \n");gets (mainstr);for (len = 0; mainstr[len] != '\0'; len++);printf ("It's length is : %d \n", len);printf("\nStarting position of substring ? \n");scanf("%d", &pos);printf("\nNumber of Characters in substring ? \n");scanf("%d", &count);if (pos len){

    printf("\n\nCharacters to be extracted exceed length \n");num = len-pos+1;

    }else

    num = count;j=0;for (i=--pos;i

  • 8/13/2019 C Programs for Under Graduates

    49/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 49

    SAMPLE OUTPUT

    Enter the main stringmuni rathnamIt's length is : 12Starting position of substring ? 1Number of Characters in substring ? 4Substring is : muni

    50. AIM:Program to Replace A portion of string

    ALGORITHMStep-1 Start the programStep-2 Declare mainstr[50], repstr[50], save[50] as Char

    data type and i, j, k, pos, num, last, last, len1, len2,len as int data type.

    Step-3 Enter the main string.Step-4 for len1=0 to mainstr[len1]! = \ 0 Step-4.1 Prin t The length of the string Step-5 Enter position from where it is to be replacedStep-6 Enter number of characters to be replacedStep-7 Enter replacing stringStep-8 for (len2=0 to repstr(len2)! = \ 0 Step-8.1 print the length; len2Step-9 Check if (pos>len1)Step-9.1 for (i=0) to less than len2Step-9.1.1 mainstr[len1+i-1] = repstr[i]Step-9.2 len = len1+len2Step-10 else

    Step-10.1 last = pos + len2-1Step-10.2 j = 0Step-10.3 for i = pos+num-1 to less than len1Step-10.3.1 save[j] = mainstr[i]Step-10.3.2 j++Step-10.4 len = jStep-10.5 j = 0Step-10.6 for i = --pos to less than lastStep-10.6.1 mainstr[i] = repstr[j]Step-10.6.2 j++Step-10.7 for i=0 to less than len

    Step-10.7.1 mainstr[last] = save[i]Step-10.7.2 Last++

    Step-10.8 for i = last to less than len1Step- 10.8.1 mainstr[i] =

    Step-11 Print REPLACED StringStep-12 Stop the program

    PROGRAM

    #includemain()

  • 8/13/2019 C Programs for Under Graduates

    50/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 50

    {char mainstr[50], repstr[50], save[50];int i, j, k, pos, num, last, len1, len2, len;printf("Enter main string : ");scanf("%[^\n]", mainstr);for(len1=0; mainstr[len1] != '\0'; len1++);printf("\nLength is %d\n", len1);printf("\nPosition from where it is to be replaced ? : ");scanf("%d", &pos);printf("\n\nNumber of characters to be replaced? : ");scanf("%d", &num);printf("\n\nEnter replacement string : ");%s", repstr);for (len2=0; repstr[len2] != '\0'; len2++);printf("\nLength is %d \n", len2);if (pos > len){

    for (i=0; i

  • 8/13/2019 C Programs for Under Graduates

    51/51

    GE2115 -COMPUTER PRACTICE LAB -

    PRACTICE PROGRAMS

    Enter main string : muni rathnamLength is 12Position from where it is to be replaced ? : 1Number of characters to be replaced? : 4Enter replacement string : maniLength is 4REPLACED string is : mani rathnam