Abhinav Psp

download Abhinav Psp

of 25

Transcript of Abhinav Psp

  • 7/30/2019 Abhinav Psp

    1/25

    SUBMITTED BY:-

    ABHINAV KUMAR ROY

    Email id:-

    [email protected]

    COURSE IN-CHARGE:-

    Mr.NITESH SAHANI

  • 7/30/2019 Abhinav Psp

    2/25

    1. WAP to calculate the ranges of char, int, long int, unsigned char,unsigned int, and unsigned long int.

    Answer:-

    #include

    #include

    int main()

    {

    printf(" Range of char -> %d to %d\n", CHAR_MIN, CHAR_MAX);

    printf(" Range of int -> %i to %i\n", INT_MIN, INT_MAX);

    printf(" Range of long int -> %ld to %ld\n", LONG_MIN,LONG_MAX);

    printf(" Range of unsigned char -> 0 to %u\n", UCHAR_MAX);

    printf(" Range of unsigned int -> 0 to %u\n", UINT_MAX);

    printf(" Range of unsigned long int -> 0 to %lu\n", ULONG_MAX);

    return 0;

    }

    Output:-

    Range of char -> -128 to 127

    Range of int -> -2147483648 to 2147483647Range of long int -> -2147483648 to 2147483647

    Range of unsigned char ->0 to 255

    Range of unsigned int -> 0 to 4294967295Range of unsigned long int -> 0 to 4294967295

    2. WAP to display the bit pattern of any integer. Your program shouldhandle the case of negative numbers efficiently.

    Answer:-

    #include#include

  • 7/30/2019 Abhinav Psp

    3/25

    main()

    {

    int j,a[8],n,g=0,i=0,k,m;

    printf("Enter a number :");

    scanf("%d", &n);

    if(n>0)

    {

    printf("Bit Pattern of %d is ", n);

    while(n!=0)

    {

    if(n%2==0)

    a[i]=0;

    elsea[i]=1;

    n=n/2;

    i++;

    }

    for(j=i-1; j>=0; j--)

    printf("%d", a[j]);}

    else

    {

    n=n*-1;

    while(n!=0)

    {

    if(n%2==0)

    a[i]=0;else

    a[i]=1;

    n=n/2;

    i++;

    }

    for(j=i-1;j>=0;j--)

    {

    if(a[j]==0){

  • 7/30/2019 Abhinav Psp

    4/25

    a[j]=1;

    }

    else

    a[j]=0;

    }

    for(k=0;k=0; j--)

    printf("%d", a[j]);

    }

    return 0;

    }

    Output:-Enter Number = -5

    Its binary form is 101

    3. A palindrome is a word , phrase or sentence that reads the sameway either forward or backward.(example-noon, madam ).If we

    disregard punctuation and blank spaces then the sentence Rise to

  • 7/30/2019 Abhinav Psp

    5/25

    Vote, sir! Is also a palindrome. Write a C program that will enter a

    line of text and determine whether or not the text is a palindrome.

    Answer:-

    #include

    #include

    #include

    main()

    {

    char a[100];

    int i=0,j,l,flag=0;printf("Enter the string:\n");

    gets(a);

    l=strlen(a);

    j=l-1;

    while(i

  • 7/30/2019 Abhinav Psp

    6/25

    j--;

    }

    if(flag)

    printf("Text is not Palindrome\n");else

    printf("Text is a palindrome\n");

    }

    Output:-

    Enter the string arora

    Text is a palindrome

    4. WAP a program to convert several lines of text to uppercase.Answer:-

    #include

    #include

    main()

    {

    char a[100][100];

    int i,j,n;

    printf("How many paragraph you want to enter :\n");

    scanf("%d",&n);

    for(i=0;i

  • 7/30/2019 Abhinav Psp

    7/25

    Output:-

    How many paragraph you want to enter

    1

    My name is abhinav

    MY NAME IS ABHINAV

    5. Read about the Towers of Hanoi game on Wikipedia. Use 4 discsand display the moves that are required to finish the game.

    Answer:-

    #include

    int tower_hanoi(int);

    main()

    {

    int n,t;

    printf("Enter the no. of discs:\n");

    scanf("%d",&n);

    t=tower_hanoi(n);

    printf("Discs can be arranged in %d ways",t);

    }

    int tower_hanoi(int n)

    {

    int r;

    if(n==1)

    return 1;

    else

    {

    r=2*tower_hanoi(n-1)+1;

    return r;

  • 7/30/2019 Abhinav Psp

    8/25

    }

    }

    Output:-

    Enter the no. of discs

    3

    Discs can be arranged in 7 ways

    6. WAP a menu driven program that performs the followingoperations on 3x3 matrices: add, multiply, multiplication by a

    number, and transpose.

    Answer:-

    #include

    #include

    #include

    void add_fuc();

    void mulmat_fuc();

    void mul_fuc();

    void trans_fuc();

    enum {add = 1, mulmat, mul, trans, over};

    int i, j, k, a[3][3], b[3][3], c[3][3];

    int main()

    {

    int choice = 0;

    while(choice != over)

    {

  • 7/30/2019 Abhinav Psp

    9/25

    printf("\n1.Add two matrices");

    printf("\n2.Multiply two matrices");

    printf("\n3.Multiply a matrix by a constant");

    printf("\n4.Find the transpose of the given matrix");printf("\n5.EXIT");

    printf("\nEnter your choice [1..5]: ");

    scanf("%d", &choice);

    if(choice>=1&&choice

  • 7/30/2019 Abhinav Psp

    10/25

    printf("Resultant matrix is: \n");

    for(i=0; i

  • 7/30/2019 Abhinav Psp

    11/25

    }

    void trans_fuc()

    {

    printf("Transposed matrix is: \n");for (i = 0; i < 3; i++,printf("\n"))

    for (j = 0; j < 3; j++)

    printf("%d\t", a[j][i]);

    }

    Output:-

    1. Add two matrices2. Multiply two matrices3. Multiply a matrix by a constant4. Find the transpose of the given matrix5. EXIT

    Enter your Choice 3

    2 2 2

    2 2 2

    Enter Multiplication factor 3

    6 6 6

    6 6 6

    7. WAP a program arrange a list of 15 names in alphabetical order.Answer:-

    #include

    #include

    main()

    {

    char a[15][100],temp[100];

  • 7/30/2019 Abhinav Psp

    12/25

    int i,j;

    printf("Enter 15 Names\n");

    for(i=0;i

  • 7/30/2019 Abhinav Psp

    13/25

    Answer:-

    #include

    main()

    {

    int a[100];

    int *p,i,j,temp,n;

    printf("Enter the size of the array:-\n");

    scanf("%d",&n);

    printf("Enter the array(1-D):-\n");

    for(i=0;i

  • 7/30/2019 Abhinav Psp

    14/25

    compare two strings. At least two functionality should be

    implemented using pointers.

    Answer:-

    #include

    #include

    void catf();

    void copyf();

    void revf();

    void lenf();void cmpf();

    char c[100]="", d[100]="", e[200]="";

    enum ch {cat=1,copy,rev,len,cmp,xit};

    int main()

    {

    int choice = 0;

    while(choice!=xit)

    {

    printf("\n1.Type 1 to Concatenate two strings");

    printf("\n2.Type 2 to copy one string into another");

    printf("\n3.Type 3 to Reverse a string");

    printf("\n4.Type 4 to find the length of a given string");

    printf("\n5.Type 5 to compare two strings");

    printf("\n6.EXIT");

    printf("\nEnter your choice [1..6]: ");

    scanf("%d", &choice);

    getchar();

    switch(choice)

    {

    case cat:

  • 7/30/2019 Abhinav Psp

    15/25

    catf();

    break;

    case copy:

    copyf();break;

    case rev:

    revf();

    break;

    case len:

    lenf();

    break;case cmp:

    cmpf();

    break;

    case xit:

    choice = xit;

    break;

    default:printf("Input not Supported!\n");

    }

    }

    return 0;

    }

    void catf()

    {

    printf("Enter 1st strings to concatenate: ");

    gets(c);

    printf("Enter 2st strings to concatenate: ");

    gets(d);

    strcpy(e,c);

    strcat(e,d);

    printf("Concatenated string is: \n");

  • 7/30/2019 Abhinav Psp

    16/25

    puts(e);

    }

    void copyf(){

    printf("Enter string to be copied: ");

    gets(c);

    char * f = c;

    char * g = d;

    while(*g++ = *f++);

    printf("Copied string is: \n");puts(d);

    printf("String copy successful\n");

    }

    void revf()

    {

    printf("Enter the string to be reversed: ");gets(c);

    strcpy(d,strrev(c));

    printf("Reversed string is: \n");

    puts(d);

    }

    void lenf()

    {

    printf("Enter a string: ");

    gets(c);

    char * cp = c;

    int i;

    for(i=0; *(i+cp); i++);

    printf("Length of string is %d\n", i);

    }

  • 7/30/2019 Abhinav Psp

    17/25

    void cmpf()

    {

    printf("Enter 1st strings: ");gets(c);

    printf("Enter 2nd strings: ");

    gets(d);

    if(strcmp(c,d)0)

    printf("Two strings are unidentical and second string comes before

    first string\n");

    }

    Output:-

    Type 1 to Concatenate two strings

    Type 2 to copy one string into another

    Type 3 to Reverse a string

    Type 4 to find the length of a given string

    Type 5 to compare two strings

    Type 6 for EXIT

    Enter your choice [1..6]:

    Enter 1st strings to concatenate: ABHINAV

    Enter 2nd strings to concatenate: kumar

    Concatenated String is: ABHINAVkumar

    10. WAP to (i) copy a text file, and (ii) copy a binary file.Answer:-

  • 7/30/2019 Abhinav Psp

    18/25

    a)

    #include

    main()

    {

    FILE *fw,*fr;

    char c;

    fw=fopen("E:/psp assignment/main.txt","w");

    fr=fopen("E:/psp assignment/nishant.txt","r");

    while((c=fgetc(fr))!=EOF)

    {

    fprintf(fw,"%c",c);

    }

    fclose(fw);

    fclose(fr);

    return 0;

    }

    11. WAP to remove all comments from a C program. Also writea complementary program to insert the comments back in place.

    Answer:-

    /*Program to find comment*/

    #include

    main(){

    char c,c1;

    int line=1;

    FILE *fp;

    /*First comment*/

    fp=fopen("Ans 11.c","r");

    while((c=fgetc(fp))!=EOF)

  • 7/30/2019 Abhinav Psp

    19/25

    {

    if(c=='\n')

    line++;

    /*Second Comment*/if(c=='/')

    {

    c1=fgetc(fp);

    if(c1=='*')

    {

    printf("Comment found at line %d",line);

    /*Third comment*/while(1)

    {

    c1=fgetc(fp);

    if(c1=='\n')

    line++;

    if(c1=='/')

    break;}

    }

    else

    putchar(c1);

    }

    else

    putchar(c);

    }

    fclose(fp);

    }

    12. WAP calculate the frequency of each word in a paragraph.The program should ask the user to input a paragraph and then it

  • 7/30/2019 Abhinav Psp

    20/25

    should count the number of words (sequence of characters

    separated by blank).

    Answer:-

    #include

    #include

    int main()

    {

    printf("Enter a paragraph: ");

    char str[500], a[500][500], f[500];fgets(str,500,stdin);

    str[strlen(str)-1] = '\0';

    //gets(str);

    int i=0, j=0, k=0, n=strlen(str);

    while(i < n)

    {

    while(i

    < n && str[i] == ' ') i++;

    if(i == n) break;

    k = 0;

    while(i < n && str[i] != ' ') a[j][k] = str[i], i++, k++;

    j++;

    }

    for(i = 0; i < j; i++)

    {

    for(k = 0; k < j; k++)

    {

    if(!strcasecmp(a[i],a[k]))

    ++f[i];

    }

    }

    for(i = 0; i < j - 1; i++)

  • 7/30/2019 Abhinav Psp

    21/25

    {

    for(k = i+1; k < j; k++)

    {

    if(!strcasecmp(a[i], a[k]))f[k] = -1;

    }

    }

    for(i = 0; i < j; i++)

    if(f[i]!=-1)

    printf("%s: %d\n", a[i], f[i]);

    //printf("%d\t", f[i]);return 0;

    }

    Output:

    Enter the Paragraph

    Baadal Singh Singh

    Baadal: 1

    Singh: 2

    13. Read about Binary Search and WAP for searching anelement in array of size N using Binary search.

    Answer:-

    #include

    main(){

    int a[100];

    int i,n,item,m1,m2,mid,flag=1,pos;

    printf("Enter the size of the array:\n");

    scanf("%d",&n);

    printf("Enter the array:\n");

    for(i=0;i

  • 7/30/2019 Abhinav Psp

    22/25

    scanf("%d",&a[i]);

    printf("Enter the item to be searched:\n");

    scanf("%d",&item);

    m1=0;m2=n-1;

    while(m1a[mid])

    m1=mid+1;

    else if(item

  • 7/30/2019 Abhinav Psp

    23/25

    14. WAP a program to test if a character from the keyboard is alower case letter.

    Answer:-

    #include

    #include

    main()

    {

    char ch;

    printf("Enter the character:\n");

    scanf("%c",&ch);if(islower(ch))

    printf("The character is in lowercase");

    else

    printf("The character is not in lowercase");

    }

    Output:-

    Enter the character

    baadal

    The character is in lowercase

    15. Write a program to read the coordinates of the end points ofa line and to find its length. Use structure variable named line to

    store the relevant information about its end points.

    Answer:-

    #include

    #include

    struct line

    {

    int x1;

    int x2;

    int y1;

  • 7/30/2019 Abhinav Psp

    24/25

    int y2;

    }x;

    main()

    {float dis=0.0;

    printf("Enter x1 :");

    scanf("%d",&(x.x1));

    printf("Enter y1 :");

    scanf("%d",&(x.y1));

    printf("Enter x2 :");

    scanf("%d",&(x.x2));printf("Enter y2 :");

    scanf("%d",&(x.y2));

    dis=sqrt(pow(x.x2-x.x1,2)+pow(x.y2-x.y1,2));

    printf("Distance between (%d ,%d) and (%d ,%d) is

    %f",x.x1,x.y1,x.x2,x.y2,dis);

    return 0;

    }

    Output:-

    Enter x1: 2

    Enter y1:3

    Enter x2:3Entery2:1

    Distance between (2,3) and (3,1) is 2.236068

    16. WAP to generate 100 random numbers lying in the range0.0000 to 1.0000. Evaluate their mean.

    Answer:-

  • 7/30/2019 Abhinav Psp

    25/25

    #include

    int main ()

    {

    int i=100;double d,sum=0;

    srand(time(NULL));

    while(i--)

    {

    d = rand();

    while(d>1)d/=10;

    sum += d;}

    printf ("Mean of 100 random numbers: %lf\n", sum/100);

    return 0;

    }

    Output:-

    Mean of 100 random numbers: 0.331611