C Prog 20prog Prectical Answer(2)

Embed Size (px)

Citation preview

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    1/23

    1.WAP to print the grade according to following specifications:

    Marks Grade

    >=90 A

    >=75 & =60 & =50 & =90)

    {

    printf("Enterd Mark is %d and Grade is A",a);

    }

    else if(a>=75 && a=60 && a

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    2/23

    else if(a>=50 && a

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    3/23

    2) WAP to reverse a given number using the do while loop.

    #include

    #includevoid main()

    {

    int num;

    clrscr();

    printf("please enter any arbitary number");

    scanf("%d",&num);

    printf("Enterd number is %d",num);

    int rev=0;

    int n=num;

    do

    {

    int rem;

    rem=n%10;

    rev=rev*10+rem;

    n=n/10;

    }

    while(n>0);

    printf("the reverse of number %d is %d",n,rev);

    getch();

    }

    3) WAP to check whether a given number is Armstrong or not. (153 =1*1*1+5*5*5+3*3*3).

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    4/23

    #include

    #include

    #includemain()

    {

    int n,no,r,sum=0;

    clrscr();

    printf("Enter any no.\n");

    scanf("%d",&n);

    no=n;

    while(n!=0)

    {

    r=n%10;

    sum=sum+pow(r,3);

    n=n/10;

    }

    if(sum==no)

    printf("The given no. %d is Armstrong No.\n",no);

    else

    printf("The given no. is NOT Armstrong No.\n");

    getch();

    return(0);

    }

    4) WAP to print the Fibonacci series 1 1 2 3 5 8 13

    #include

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    5/23

    #include

    #include

    main()

    {

    int i,a,b,fibo,n;

    clrscr();

    printf("1\t1\t");

    a=1;

    b=1;

    fibo=a+b;for(i=1;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    6/23

    void main()

    {

    void swap(int,int);

    int a=0,b=20;clrscr();

    printf("\n \t Before calling swap a=%d \t b=%d",a,b);

    swap(a,b);

    printf("\n \t After calling swap a=%d /t b=%d",a,b);

    getch();

    }

    void swap(int x,int y)

    {

    int temp;

    temp=x;

    x=y;

    y=temp;

    printf("\n \t In swap x=%d \t y=%d",x,y);

    }

    6) '' '' '' '' call by reference.

    #include

    #include

    void main()

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    7/23

    {

    void swap(int,int);

    int a,b;

    printf("please enter two no's to swap");scanf("%d %d",&a,&b);

    swap(a,b);

    getch();

    }

    void swap(int x,int y)

    {

    int temp=x;

    x=y;

    y=temp;

    printf("after swap the nos are a=%d,b=%d",x,y);

    }

    7) WAP to find power of given number using recursion.

    #include

    #include

    int pow(int a, int b)

    {

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    8/23

    if(b==0)

    return (1);

    else

    {return(a* pow(a,b-1));

    }

    }

    void main()

    {

    int a,b,p;

    clrscr();

    printf("Enter two numbers");

    scanf("%d %d",&a,&b);

    p=pow(a,b);

    printf("the result is %d",p);

    getch();

    }

    8) WAP to find factorial of a number using recursion.

    #include#include

    void main()

    {

    long n,factorial;

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    9/23

    long fact(long);

    printf("Enter any number\n");

    scanf("%l",&n);

    factorial=fact(n);printf("Factorial is %l\n",factorial);

    getch();

    }

    long fact(long n)

    {

    long res=1,i;

    for (i=n;i>=1;i--)

    res=res*i;

    return(res);

    }

    9) WAP to find GCD of 2 numbers.

    #include

    #include

    void main()

    {

    int i=1,a,b,num;

    clrscr();

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    10/23

    printf("\nEnter two numbers.");

    scanf("%d%d",&a,&b);

    while(a%i==0&&b%i==0){

    num=i;

    i++;

    }

    printf("GCD of two numbers is %d\n",num);

    getch();

    }

    10) WAP to multiply 2 matrices.

    #include

    #include

    void main()

    {

    int i,j,k;

    int a[3][3],b[3][3],c[3][3];

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    11/23

    clrscr();

    printf("Enter the elements of first matrix");

    for(i=0;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    12/23

    11) WAP to find transpose of two matrices.

    #include#includevoid swap(int *x,int *y);main(){int i,j,k,m,n;int matrix[3][3];printf("Enter the matrix: \n\n");for (i=0;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    13/23

    }printf("\n\n");for (j=0;j

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    14/23

    for(i=0;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    15/23

    {

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

    }

    for(i=0;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    16/23

    14) WAP to check a given string is Palindrome or not.

    #include

    #include

    #include

    void main()

    {

    clrscr();

    char S1[20],S2[20];

    printf("enter any string\n");

    gets(S1);

    strcpy(S2,S1);

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    17/23

    strrev(S2);

    if((strcmp(S1,S2))==0)

    printf("\nstring %s is not palindrome",S1);

    elseprintf("\nstring %s is palindrome",S1);

    getch();

    }

    15) WAP to reverse a given string without using strrev ().

    #include #include #include

    void main(){

    char str[10],temp;int i,len;printf("Enter String : ");

    scanf("%s",str);len=strlen(str)-1;for(i=0;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    18/23

    printf("%s",str);getch();

    }

    16) )WAP to concatenate two string using strcat ().#include

    #include

    #include

    void main()

    {

    int i,j,k;

    char a[10], b[10], c[20];

    clrscr();

    printf ("Enter two strings; ");

    scanf ("%s%s", &a,&b);

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

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    19/23

    {

    c[i]=a[i];

    }

    for (j=i, k=0;b[k]!='\0';k++,j++){

    c[j]= b[k];

    }

    c[j] ='\0';

    printf("The concatenction is %s" ,c);

    getch();

    }

    17) WAP to copy one file to another file

    #include

    #include

    #include

    void main()

    {

    FILE *fp,*ft;

    char ch;clrscr();

    fp=fopen("d:/original.txt","r");

    ft=fopen("d:/copy.txt","w");

    while (1)

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    20/23

    {

    ch=fgetc(fp);

    if(ch==EOF)

    {printf("Contents are written successfully into the file");

    getch();

    exit(1);

    }

    fputc(ch,ft);

    }

    getch();

    }

    18) WAP to append content of 1 file to another file.

    /*String.c string variable*/#include < stdio.h >main(){char month[15];printf (Enter the string);gets (month);printf (The string entered is %s, month);}

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    21/23

    19) WAP to check a given number is prime or not

    #include

    #include

    void main()

    {

    int i=1,a,b,num;

    clrscr();

    printf("\nEnter two numbers.");

    scanf("%d",&a);

    while(a%i==0&&a%1==0)

    {

    num=i;

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    22/23

    i++;

    }

    printf("GCD of two numbers is %d\n",num);

    getch();}

    20) WAP to print the following pattern.

    #include

    #include

    void main()

    {

    int i,j,k,l;

    clrscr();

    for(i=0;i

  • 8/8/2019 C Prog 20prog Prectical Answer(2)

    23/23