c programs with solutions-2

Upload: victor-othugadi

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 c Programs With Solutions-2

    1/48

    C PROGRAMS WITH SOLUTIONS- 2

    NOTE: THIS ARE THE CONFIDENTAL AND INTERNAL PROGRAMS WITH

    OUTPUTS.

    C PROGRAMS FOR BEGINEERS(FRESHERS) FOR THE INTERVIEW POINT OFVIEW.

    1.C program to perform arithemetic operations*/

    #include

    main()

    {

    int a,b;

    clrscr();printf("Enter value for a:");

    scanf("%d",&a);

    printf("/nEnter value for b:");

    scanf("%d",&b);

    printf("/n%d+%d=%d",a,b,a+b);

    printf("/n%d-%d=%d",a,b,a-b);

    printf("/n%dX%d=%d",a,b,a*b);

    printf("/n%d/%d=%d",a,b,a/b);

    }

    o/p:

    Enter value for a:5

    Enter value for b:2

    5+2=75-2=3

    5X2=10

    5/2=2

    2. C program to calculate simple interest*/

    #include

    main()

    {

    float p,n,r,ci;

    clrscr();

    printf("Enter princiole :");

    scanf("%f",&p);

    printf("Enter Time Period:");

    scanf("%f",&n);

    printf("Enter Rate of Interest:");

    scanf("%f",&r);

    ci=(p*n*r)/100;

    printf("Simple Interest=%f",ci);

    }

  • 8/3/2019 c Programs With Solutions-2

    2/48

    o/p:

    Enter principle :5700.50

    Enter Time Period:1.6

    Enter Rate of Interest:2.5

    Simple Interest=228.020004

    3. C program to calculate Compound interest*/

    #include

    #include

    main()

    {

    float p,n,r,ci,d;

    clrscr();

    printf("Enter principle :");

    scanf("%f",&p);

    printf("/nEnter Time Period:");

    scanf("%f",&n);

    printf("Enter Rate of Interest:");

    scanf("%f",&r);

    d=(1+r*0.10);

    ci=p*pow(d,n);

    printf("Simple Interest=%f",ci);

    }

    o/p:

    Enter principle :2000

    Enter Time Period:2

    Enter Rate of Interest:2

    Simple Interest=2880.000244

    4.Distance Travelled*/

    #include

    main()

    {

    float a,u,t,dis;

    clrscr();

    printf("\nEnter velocity, acceleration and time:");

    scanf("%f%f%f",&a,&u,&t);

    dis=(u*t)+(0.5*a*t*t);

    printf("Distance travelled=%f",dis);

    getch();

    }

    5. C program to convert Temparature From Faranheat To Celsiuls*/

    #include

    main()

    {

    float ft,ct;

    clrscr();

  • 8/3/2019 c Programs With Solutions-2

    3/48

    printf("Enter Faran Heat Temparature:");

    scanf("%f",&ft);

    ct=(ft-32)*5/9;

    printf("/ncelsius Temparature=%f",ct);

    }

    6. C program to convert Temparature From Celsius To Faranheat*/

    #include

    main()

    {

    float ft,ct;

    clrscr();

    printf("Enter celsius Temparature:");

    scanf("%f",&ct);

    ft=(ct*9/5)+32;

    printf("/nFaran Heat Temparature=%f",ft);

    }

    7. C program to calculate cube of the number*/

    #include

    main()

    {

    int a;

    clrscr();

    printf("Enter value for a:");

    scanf("%d",&a);

    printf("%d^3=%d",a,a*a*a);

    }

    o/p:Enter value for a:5

    5^3=125

    8. *Swapping 2 nos Without Using Third Variable*/

    #include

    main()

    {

    int a,b;

    clrscr();

    printf("/nEnter values for a,b:");

    scanf("%d%d",&a,&b);printf("/nBefore swaping a=%d b=%d",a,b);

    a=a+b;

    b=a-b;

    a=a-b;

    printf("/nAfter swaping a=%d b=%d",a,b);

    getch();

    }

    o/p:

  • 8/3/2019 c Programs With Solutions-2

    4/48

    Enter values for a,b:10 15

    Before swaping a=10 b=15

    After swaping a=15 b=10

    9. Even Number Odd Number Checking in C*/#include

    main()

    {

    int a;

    printf("enter values for a");

    scanf("%d",&a);

    if(a%2==0)

    {

    printf("%d is even"a);

    }

    }

    10. Finding Roots Of Quadratic Equations*/

    #include

    #include

    main()

    {

    int a,b,c;

    int alpha,beta,root;

    clrscr();

    printf("\nEnter a,b,c:");

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

    root=sqrt((b*b)-4*a*c);

    alpha=(-b+root)/(2*a);beta=(-b-root)/(2*a);

    printf("alpha=%d",alpha);

    printf("beta=%d",beta);

    getch();

    }

    11. /*Comparing 2 Numbers Using Simple if Condition in C*/#include

    main()

    {

    int a,b;

    printf("enter values for a,b");scanf("%d%d",&a,&b);

    if(a>b)

    {

    printf("%d is big"a);

    }

    if(a

  • 8/3/2019 c Programs With Solutions-2

    5/48

    if(a==b)

    {

    printf("%d and %d are equal"a,b);

    }

    }

    12. /*Leap Year Using Simple if-else Condition in C*/#include

    main()

    {

    int year;

    clrscr();

    printf("/nEnter year:");

    scanf("%d",&year);

    if(year%4==0)

    printf("/nLeap year");

    else

    printf("/nNon Leap year");

    getch();

    }

    13. /*Triangle Checking Using Simple if-else Condition in C*/#include

    main()

    {

    int a,b,c;

    clrscr();

    printf("Enter 3 angles:");

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

    if(a+b+c==180)

    printf("/nTriangle is valid");else

    printf("/nTriangle is not valid");

    getch();

    }

    o/p:

    Enter 3 angles:60

    60

    60

    Triangle is valid

    Enter 3 angles:45

    45

    80

    14. /*Biggest Number Among 4 Numbers*/#include

    main()

    {

    int a,b,c,d;

    clrscr();

  • 8/3/2019 c Programs With Solutions-2

    6/48

    printf("enter values for a,b,c,d");

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

    if(a>b && a>c && a>d)

    printf("a(%d) is big",a);

    else if(b>c && b>d)

    printf("b(%d) is big",b);

    else if(c>d)

    printf("c(%d) is big",c);

    else

    printf("d(%d) is big",d);

    getch();

    }

    15. /*Leap Year Using Simple if-else Condition in C*/

    #include

    main()

    {

    int a,b,c,d;

    clrscr();

    printf("enter values for a,b,c,d");

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

    if(a>b && a>c && a>d)

    printf("a(%d) is big",a);

    else if(b>c && b>d)

    printf("b(%d) is big",b);

    else if(c>d)

    printf("c(%d) is big",c);

    else

    printf("d(%d) is big",d);

    getch();

    }

    16. /*program to check whether a point lies on x-axis or y-axis or onthe origin*/

    #include

    main()

    {

    int x,y;

    clrscr();

    printf("Enter a point(x,y)");

    scanf("%d%d",&x,&y);

    if(x==0 && y==0)

    printf("/nPoint lies on the origin");

    else if(x!=0 && y==0)

    printf("/nPoint lies on x-axis");

    else

    printf("/nPoint lies on y-axis");

    getch();

    }

    o/p:

    Enter a point(x,y)0 0

  • 8/3/2019 c Programs With Solutions-2

    7/48

    Point lies on the origin

    Enter a point(x,y)2 0

    Point lies on x-axis

    Enter a point(x,y)0 10

    Point lies on y-axis

    17. /*program to check whether a point lies inside or outside or on thecircle*/

    #include

    main()

    {

    int x,y,r,d,ds;

    clrscr();

    printf("Enter a point(x,y)");

    scanf("%d%d",&x,&y);

    printf("/nEnter radius of the circle");scanf("%d",&r);

    d=r*r;

    ds=((x*x)+(y*y));

    if(d==ds)

    printf("/point lies on the circle");

    else if(ds>d)

    printf("/nPoint lies outside of the circle");

    else

    printf("/nPoint lies inside of the circle");

    getch();

    }

    18. /*Program to find whether entered character is in uppercase orlowercase*/

    #include

    main()

    {

    char ch;

    clrscr();

    printf("/nEnter a character:");

    scanf("%c",&ch);

    if(ch>=65 && ch=97 && ch=48 && ch

  • 8/3/2019 c Programs With Solutions-2

    8/48

    19. /*Finding ASCII Values of characters using while loop*/

    #include

    main()

    {

    int i=65;

    clrscr();while(i

  • 8/3/2019 c Programs With Solutions-2

    9/48

    printf("Sum of digits of %ld =%ld",m,sum);

    }

    o/p:

    Enter any number:92346

    Sum of digits of 92346 =64329

    22. /*Palindromme Number Using While Loop*/

    #include

    main()

    {

    long int n,d,sum=0,m;

    clrscr();

    printf("Enter any number:");

    scanf("%ld",&n);

    m=n;

    while(n!=0)

    {

    d=n%10;

    sum=(sum*10)+d;

    n=n/10;

    }

    if(m==sum)

    printf("%d-Palindrome",m);

    else

    printf("%d-Not Palindrome",m);

    }

    o/p:

    Enter any number:1234

    1234-Not Palindrome

    Enter any number:121

    121-Palindrome

    23. /*Amstrong Number Using While Loop*/

    #include

    main()

    {

    long int n,d,sum=0,m;

    printf("Enter any number:");

    scanf("%ld",&n);

    m=n;

    while(n!=0)

    {d=n%10;

    sum=sum+(d*d*d);

    n=n/10;

    }

    if(m==sum)

    printf("%ld is amstrong number",m);

    else

    printf("%ld is not amstrong number",m);

  • 8/3/2019 c Programs With Solutions-2

    10/48

    }

    o/p:

    Enter any number:153

    153 is amstrong number

    Enter any number:235

    235 is not amstrong number

    24. /*strong number: The sum of factorial of individual digits should be equal to the

    original no. eg:145=(1!+4!+5!)*

    #include

    main()

    {

    long int n,d,sum=0,m,i,f;

    clrscr();

    printf("Enter any number:");

    scanf("%ld",&n);

    m=n;

    while(n!=0){

    d=n%10;

    f=1;

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

    f=f*i;

    sum=sum+f;

    n=n/10;

    }

    if(m==sum)

    printf("%ld is strong number",m);

    else

    printf("%ld is not strong number",m);

    getch();

    }

    o/p:

    Enter any number:145

    145 is strong number

    Enter any number:568

    568 is not strong number

    25. /*strong number: The sum of factorial of individual digits should be equal to the

    original no. eg:145=(1!+4!+5!)*/

    #include

    main()

    {

    long int n,d,sum=0,m,i,f;

    clrscr();

    printf("Enter any number:");

    scanf("%ld",&n);

    m=n;

  • 8/3/2019 c Programs With Solutions-2

    11/48

    while(n!=0)

    {

    d=n%10;

    f=1;

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

    f=f*i;

    sum=sum+f;

    n=n/10;

    }

    if(m==sum)

    printf("%ld is strong number",m);

    else

    printf("%ld is not strong number",m);

    getch();

    }

    o/p:

    Enter any number:145

    145 is strong number

    Enter any number:568568 is not strong number

    26. /*GCD of two numbers using while loop*/

    #include

    main()

    {

    int a,b,l;

    clrscr();

    printf("/nEnter nos:");

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

    l=gcd(a,b);printf("GCD=%d",l);

    getch();

    }

    gcd(int a,int b)

    {

    int c=0;

    while(1)

    {

    c=a%b;

    if(c==0)

    return b;

    a=b;

    b=c;}

    }

    o/p:

    Enter nos:200 800

    GCD=200

  • 8/3/2019 c Programs With Solutions-2

    12/48

    27. /*LCM of two numbers using while loop*/

    #include

    main()

    {

    int a,b,l;

    clrscr();printf("/nEnter nos:");

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

    l=lcm(a,b);

    printf("Lcm=%d",l);

    getch();

    }

    lcm(int a,int b)

    {

    int i=1;

    while(1)

    {

    if(i%a==0 && i%b==0)

    return i;

    i++;

    }

    }

    o/p:

    Enter nos:35 50

    Lcm=350

    28. *Printing Table using while loop*/

    #include

    main()

    {

    int n,i=1,t=1;

    clrscr();

    printf("/nEnter a table:");

    scanf("%d",&n);

    while(i

  • 8/3/2019 c Programs With Solutions-2

    13/48

    5 X 5=25

    5 X 6=30

    5 X 7=35

    5 X 8=40

    5 X 9=45

    5 X10=50

    29. /*Printing First 20 Odd Numbers using while loop*/

    #include

    main()

    {

    int count=0,i=1;

    clrscr();

    while(count!=20)

    {if(i%2!=0)

    {

    printf("%d\n",i);

    count++;

    }

    i++;

    }

    getch();

    }

    30. /*Decimal To Binary Conversion using while loop*/

    #includemain()

    {

    int n,i=0,a[20],d,j,m;

    clrscr();

    printf("Enter decimal number:");

    scanf("%d",&n);

    m=n;

    while(n!=0)

    {

    d=n%2;

    a[++i]=d;

    n=n/2;

    }printf("Binary equivalent of %d=",m);

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

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

    getch();

    }

    31./*Binary To Decimal Conversion using while loop*/

  • 8/3/2019 c Programs With Solutions-2

    14/48

    #include

    #include

    main()

    {

    long int n,m;

    int i=0,sum=0,d,j;

    clrscr();

    printf("Enter binary number:");

    scanf("%ld",&n);

    m=n;

    while(n!=0)

    {

    d=n%10;

    sum=sum+(d*pow(2,i));

    n=n/10;

    i++;

    }

    printf("Decimal equivalent of %ld=%d",m,sum);

    getch();

    }

    32. /*Menu Driven Program For Arithematic Operations*/

    #include

    main()

    {

    int a,b,res;

    char ch;

    printf("\n Enter values for a and b:");

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

    printf("\nEnter choice:");

    scanf("%s",&ch);

    switch(ch)

    {

    case '+': res=a+b; break;

    case '-': res=a-b; break;

    case '*': res=a*b; break;

    case '/': res=a/b; break;

    case '%': res=a%b; break;

    default: printf("\n Enter correct choice:");

    }

    printf("Result=%d",res);

    getch();

    }

    33. /*Menu driven program having the following options 1. Factorial of a number 2.

    prime number 3. Even number*/main(){

    int n,f,np,a,count=0,ch,i;

    do

    {

    printf("/n1.Factorial of a Number");

  • 8/3/2019 c Programs With Solutions-2

    15/48

    printf("/n2.Prime Number or Not");

    printf("/n3.Even Number or Not");

    printf("/n4.Exit");

    printf("/nEnter choice:"); scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    printf("/nEnter number:"); scanf("%d",&n);

    f=fact(n);

    printf("/n%d!=%d/n",n,f);

    break;

    case 2:

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

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

    f=f*i;

    return f;

    }

    34.About U/*Printing Number And its Squares Using For Loop*/s|

    s|

    http://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/AboutUs.htmlhttp://www.yellowpen.co.in/FAQS.htmlhttp://www.yellowpen.co.in/FAQS.htmlhttp://www.yellowpen.co.in/AboutUs.html
  • 8/3/2019 c Programs With Solutions-2

    16/48

    Contact Usmain(){

    int i;

    clrscr();

    printf("Number/tSquare");

    for(i=1;i

  • 8/3/2019 c Programs With Solutions-2

    17/48

    o/p:

    1

    2 2

    3 3 3

    4 4 4 4

    5 5 5 5 5

    37. /*Printing Format Using For Loop */main()

    {

    int i,j;

    clrscr();

    for(i=1;i

  • 8/3/2019 c Programs With Solutions-2

    18/48

    45

    5

    39. /*Printing Format Using For Loop 1

    2 1

    3 2 1

    4 3 2 1

    5 4 3 2 1

    6 5 4 3 2 1

    7 6 5 4 3 2 1

    8 7 6 5 4 3 2 1

    9 8 7 6 5 4 3 21

    main(){

    int i,j,n;clrscr();printf("Enter number:");

    scanf("%d",&n);for(i=1;i=1;j--)

    printf("%3d",j);printf("\n");

    }getch();

    }

    o/p:Enter number:9

    12 13 2 14 3 2 15 4 3 2 16 5 4 3 2 17 6 5 4 3 2 18 7 6 5 4 3 2 19 8 7 6 5 4 3 2 1

  • 8/3/2019 c Programs With Solutions-2

    19/48

    40. /*Printing Format Using For Loop 5 4 3 2 1

    4 3 2 1

    3 2 1

    2 1

    1 */

    main()

    {

    int i,j,n,m;

    clrscr();

    printf("Enter number:");

    scanf("%d",&n);

    m=n;

    for(i=1;i=1;j--)printf("%3d",j);

    printf("\n");

    n=n-1;

    }

    getch();

    }

    o/p:

    Enter number:5

    5 4 3 2 1

    4 3 2 1

    3 2 1

    2 1

    1

  • 8/3/2019 c Programs With Solutions-2

    20/48

    41. /*Printing Format Using For Loop 1 2 3 4 5 6 78 9

    1 2 3 4 5 6 7 8

    1 2 3 4 5 6 7

    1 2 3 4 5 6

    1 2 3 4 5

    1 2 3 4

    1 2 3

    1 2

    1 */main(){

    int i,j,n,m;clrscr();printf("Enter number:");scanf("%d",&n);m=n;for(i=1;i

  • 8/3/2019 c Programs With Solutions-2

    21/48

    printf("Enter the range:");

    scanf("%d",&n);

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

    printf("%2d",i);

    for(i=1;i

  • 8/3/2019 c Programs With Solutions-2

    22/48

    {

    t=n*i;

    printf("%2dX%2d=%d/n",n,i,t);

    }

    getch();

    }

    o/p:

    Enter a table:5

    5 X 1= 5

    5 X 2=10

    5 X 3=15

    5 X 4=20

    5 X 5=25

    5 X 6=30

    5 X 7=35

    5 X 8=40

    5 X 9=45

    5 X10=50

    46. /*Printing Table Using For Loop */main()

    {

    int n,i,count=0;

    clrscr();

    printf("Enter number:");

    scanf("%d",&n);

    for(i=1;i

  • 8/3/2019 c Programs With Solutions-2

    23/48

    }

    if(count==2)

    printf("%5d",i);

    count=0;

    }

    }

    o/p:

    2 3 5 7 11 13 17 19 23 29 31 37

    41 43 47 53 59 61 67 71 73 79 83 89

    97.

    48. /*Printing Prime Numbers From m t0 n Using For Loop */main()

    {

    int m,n;

    int i,j,count=0;

    clrscr();

    printf("Enter Starting Number:");

    scanf("%d",&m);

    printf("Enter Ending Number:");scanf("%d",&n);

    for(i=m;i

  • 8/3/2019 c Programs With Solutions-2

    24/48

    else

    {

    printf("\nEnter non -ve exponent:");

    scanf("%d",&n);

    goto xy;

    }

    xy:

    printf("\nGeometric Progression:\n");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    25/48

    printf("/n Small=%d",s);

    }

    o/p:

    Enter Range:5

    Enter elements:52 58 0 -8 6

    Large=58

    Small=-8

    52. /*Finding Sum of even and odd Nos in Array*/main()

    {

    int n,i,ds=0,es=0,a[10];

    clrscr();

    printf("Enter Range:");

    scanf("%d",&n);

    printf("/n Enter elements:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    26/48

    temp=a[i];

    a[i]=a[j];

    a[j]=temp;

    }

    }

    }

    printf("/nElements after sorting:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    27/48

    }

    55.

    /*Subtraction Of Matrices Using Arrays*/main()

    {

    int i,j,r,c;

    static int a[20][20],b[20][20],sum[20][20];

    clrscr();

    printf("Enter no.of rows:");

    scanf("%d",&r);

    printf("Enter no.of columns:");

    scanf("%d",&c);

    printf("Entet Matrix A:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    28/48

    printf("Enter Matrix B:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    29/48

    2 Column Sum=6

    58./*Greatest Row Sum in the Array*/

    main()

    {

    static int a[10][10],b[10][10],rs[10];

    static int i,j,r,c,l;

    clrscr();

    printf("/nEnter No.of rows:");

    scanf("%d",&r);

    printf("/nEnter No.of columns:");

    scanf("%d",&c);

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    30/48

    {

    if(i==j)

    a[i][j]=0;

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

    }

    printf("/n");

    }

    }

    o/p:

    Enter No.of rows:3

    Enter No.of columns:3

    Enter elements:

    1 2 3

    4 5 6

    7 8 9

    0 2 3

    4 0 6

    7 8 0

    60. /*Identity Matrix Using Arrays*/

    main()

    {

    static int a[10][10],b[10][10];

    static int i,j,r,c;

    printf("/nEnter No.of rows:"); scanf("%d",&r);

    printf("/nEnter No.of columns:"); scanf("%d",&c);

    printf("/nEnter elements:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    31/48

    printf("/nEnter No.of columns:");

    scanf("%d",&c);

    printf("/nEnter elements:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    32/48

    char s[20];

    static int i,v,c,sc;

    clrscr();

    printf("/nEnter the string:");

    gets(s);

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

    {

    if(isalpha(s[i]))

    {

    if(s[i]=='a' || s[i]=='e' || s[i]=='i'||

    s[i]=='o' || s[i]=='u')

    v++;

    else

    c++;

    }

    esle

    sc++;

    }

    printf("/nVowels=%d",v);

    printf("/nConsonants=%d",c);

    printf("/nSpecial Characters=%d",sc);getch();

    }

    o/p:

    Enter the string:this is the computer lab

    Vowels=7

    64. /*Program to count Number of lines, Words and characters in the text*/#include

    main()

    {

    char str[81], ctr;

    int i=0,c=0,words= 0,lines = 0;

    clrscr();

    printf("Enter the string:");

    while(1)

    {

    while((ctr=getchar()) != '\n')

    str[i++] = ctr;

    str[i] = '\0';

    if(str[0] == '\0')

    break ;

    else

    {

    words++;

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

    c++;

    if(str[i] == ' ' || str[i] == '\t')

    words++;

    }

    }

    lines = lines +1;

    }

    printf("\nNumber of lines = %d\n", lines);

  • 8/3/2019 c Programs With Solutions-2

    33/48

    printf("Number of words = %d\n", words);

    printf("Number of characters = %d\n", c);

    getch();

    }

    65. /*Program to check the character in the text*/main()

    {

    char s[100],c;

    int i;

    clrscr();

    printf("/nEnter the string:");

    gets(s);

    printf("/nEnter character to search:");

    scanf("%c",&c);

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

    {

    if(s[i]==c)

    {

    printf("/n %c found at %d",c,i+1);break;

    }

    }

    if(s[i]=='/0')

    printf("/n%c not found",c);

    getch();

    }

    o/p:

    Enter the string:this is c programming

    Enter character to search:c

    c found at 9

    Enter the string:this is c programming

    Enter character to search:d

    d not found

    66. /*Program to find number of occurances of a charactermain()

    {

    char s[100],c;

    int i,count=0;

    clrscr();

    printf("/nEnter the string:");

    gets(s);

    printf("/nEnter character to search:");

    scanf("%c",&c);

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

    if(s[i]==c)

    count++;

    }

    printf("/n%c found %d times",c,count);

    getch();

    }

    o/p:

    Enter the string:this is c programming

  • 8/3/2019 c Programs With Solutions-2

    34/48

    Enter character to search:m

    m found 2 times

    67.strlen()

    68.strrev()

    69.strcpy()

    70.strcat()

    71.strcmp()

    72.strupr()

    73.strlwr()

    74. /*Finding String Length Without Using Library Function */

    main(){

    char s[20];

    static int i,len;

    clrscr();

    printf("/nEnter string:");

    gets(s);

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

    len++;

    printf("Length of the string=%d",len);

    getch();

    }

    o/p:

    Enter string:this is c programmingLength of the string=21

    75. /*String Reverse Without Using Library Function */main()

    {

    char s[100],c[100];

    static int i,len,j;

    clrscr();

    printf("/nEnter string:");

    gets(s);

    len=strlen(s);

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

    {

    c[j++]=s[i];}

    printf("Length of the string=%s",c);

    getch();

    }

    o/p:

    Enter string:this is c programming

    Length of the string=gnimmargorp c si siht

    http://www.yellowpen.co.in/c_notes/c_programming/strings/strlen.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strlen.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strrev.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcpy.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcpy.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcat.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcmp.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strupr.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strlwr.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strlwr.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strlen.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strrev.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcpy.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcat.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strcmp.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strupr.htmlhttp://www.yellowpen.co.in/c_notes/c_programming/strings/strlwr.html
  • 8/3/2019 c Programs With Solutions-2

    35/48

    76. /*String Copy Without Using Library Function */

    main()

    {

    char s[100],c[100];

    static int i,len;

    clrscr();

    printf("/nEnter string:");

    gets(s);

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

    {

    c[i]=s[i];

    }

    c[i]='/0';

    printf("copied string=%s",c);

    getch();

    }

    o/p:

    Enter string:this is c programmingcopied string=this is c programming

    77. /*String Concatenation Without Using Library Function */main()

    {

    char s[100],s1[100],c[100];

    static int i,j;

    clrscr();

    printf("/nEnter first string:");

    gets(s);

    printf("/nEnter second string:");

    gets(s1);

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

    c[i]=s[i];

    for(j=0;s1[j]!='/0';j++)

    c[i++]=s1[j];

    c[i]='/0';

    printf("string concatenation=%s",c);

    getch();

    }

    o/p:

    Enter first string:college

    Enter second string:bus

    string concatenation=collegebus.

    78. /*Program to check whether the given string is palindrome or not*/main()

    {

    char s[100],c[100];

    static int i,j,len,count;

    clrscr();

    printf("/nEnter first string:");

    gets(s);

  • 8/3/2019 c Programs With Solutions-2

    36/48

    len=strlen(s);

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

    c[j++]=s[i]; /*reversing two strings*/

    c[i]='/0';

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

    {

    if(s[i]==c[i]) /*Comparision of two strings*/

    count++;

    }

    if(len==count)

    printf("palindrome");

    else

    printf("Not palindrome");

    getch();

    }

    o/p:

    Enter first string:madam

    palindrome

    Enter first string:college

    Not palindrome

    79. /*Program to convert given string into uppercase*/

    main()

    {

    char s[100],c[100];

    static int i,j,len,count;

    clrscr();

    printf("/nEnter first string:");

    gets(s);

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

    {

    c[i]=(s[i]>='a' && s[i]

  • 8/3/2019 c Programs With Solutions-2

    37/48

    printf("/nEnter first string:");

    gets(s);

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

    {

    c[i]=(s[i]>='A' && s[i]

  • 8/3/2019 c Programs With Solutions-2

    38/48

    {

    if(s[i]!=' ')

    {

    s[i]=s[j++];

    }

    else

    {

    s[i]=s[i+1];

    s[i]=s[j++];

    }

    }

    s[i]='\0';

    printf("\nString after %d characters deletion

    from %d position:",n,p);

    puts(s);

    getch();

    }

    o/p:

    Enter string: This is beatiful world

    Enter no.of characters to delete: 4

    Enter the position:5string after 4 characters deletion from 5 position:

    thisbeatiful world.

    83. Sorting Stringsmain()

    {

    char s[20][100],temp[20];

    static int i,j,n;

    printf("/nEnter no.of strings:");

    scanf("%d",&n);

    for(;i

  • 8/3/2019 c Programs With Solutions-2

    39/48

    main()

    {

    clrscr();

    compread();

    compwrite();

    compadd();

    compmul();

    getch();

    }

    compread()

    {

    printf("\nEnter Complex No1:");

    scanf("%d%d",&comp.real,&comp.img);

    printf("\nEnter Complex No2:");

    scanf("%d%d",&comp1.real,&comp1.img);

    }

    compwrite()

    {

    printf("\nFirst Complex Number:");

    printf("%di%d\n",comp.real,comp.img);printf("\nSecond Complex Number:");

    printf("%di%d\n",comp1.real,comp1.img);

    }

    compadd()

    {

    struct complex cadd;

    cadd.real=comp.real+comp1.real;

    cadd.img=comp.img+comp1.img;

    printf("\nAddtion of two complex nos:");

    printf("%di%d",cadd.real,cadd.img);

    }

    compmul()

    {

    struct complex cmul;

    cmul.real=(comp.real*comp1.real)-(comp.img*comp1.img);

    cmul.img=(comp.real*comp1.img)+(comp.img*comp1.real);

    printf("\nMultiplication of two complex nos:");

    printf("%di%d",cmul.real,cmul.img);

    }

    85. /*Calculating Power Using Functions*/main()

    {

    int b,p;

    long int res;

    clrscr();

    printf("/nEnter base,power:");scanf("%d%d",&b,&p);

    res=power(b,p);

    printf("/n%d^%d=%d",b,p,res);

    getch();

    }

    /*power function*/

    power(int b,int p)

    {

    long int i,res=1;

  • 8/3/2019 c Programs With Solutions-2

    40/48

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

    f=f*i;

    return f;

    }

    88. /* Finding LCM of two numbers*/#include

    main()

  • 8/3/2019 c Programs With Solutions-2

    41/48

    {

    int a,b,l;

    clrscr();

    printf("/nEnter nos:");

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

    l=lcm(a,b);

    printf("Lcm=%d",l);

    getch();

    }

    lcm(int a,int b)

    {

    int i=1;

    while(1)

    {

    if(i%a==0 && i%b==0)

    return i;

    i++;

    }

    }

    o/p:

    Enter nos:35 50Lcm=350

    89. /*Finding GCD of two numbers*/#include

    main()

    {

    int a,b,l;

    clrscr();

    printf("/nEnter nos:");

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

    l=gcd(a,b);

    printf("GCD=%d",l);

    getch();

    }

    gcd(int a,int b)

    {

    int c=0;

    while(1)

    {

    c=a%b;

    if(c==0)

    return b;

    a=b;

    b=c;

    }

    }

    o/p:

    Enter nos:200 800

    GCD=200

    90. /*Evaluating expression*/#include

    #include

    main()

  • 8/3/2019 c Programs With Solutions-2

    42/48

    {

    int x;

    double sum=0;

    clrscr();

    printf("\nEnter x value:");

    scanf("%d",&x);

    sum=1-pow(x,2)/fact(2)+pow(x,4)/fact(4)-pow(x,6)/fact(6)

    +pow(x,8)/fact(8)-pow(x,10)/fact(10);

    printf("%f",sum);

    getch();

    }

    /*fact function*/

    fact(int f)

    {

    long int res=1,i;

    for(i=1;i

  • 8/3/2019 c Programs With Solutions-2

    43/48

    printf("\n");

    }

    }

    92. /*Matrices Multiplication Using Functions*/#include

    main()

    {

    int ra,ca,rb,cb;

    clrscr();

    printf("Enter no.of rows for Matrix A:");

    scanf("%d",&ra);

    printf("Enter no.of columns:");

    scanf("%d",&ca);

    printf("Enter no.of rows for Matrix B:");

    scanf("%d",&rb);

    printf("Enter no.of columns for Matrix B:");

    scanf("%d",&cb);

    if(ca!=rb)

    printf("Matrix Multiplication Not Possible");

    else

    matmul(ra,ca,rb,cb);getch();

    }

    /*Matmul() function*/

    matmul(ra,ca,rb,cb)

    int ra,ca,rb,cb;

    {

    static int i,j,k,b[20][20],a[20][20],mul[20][20];

    printf("Enter Matrix A:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    44/48

    {

    int n,i;

    float a[10],res;

    clrscr();

    printf("/n enter no.of values");

    scanf("%d",&n);

    printf("/nenter values for array a:");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    45/48

    printf("/nEnter number:");

    scanf("%d",&n);

    printf("/n%d!=%d",n,fact(n));

    getch();

    }

    int fact(n)

    int n;

    {

    if(n==0 || n==1)

    return 1;

    else

    return (n*fact(n-1));

    }

    o/p:

    Enter number:6

    6!=720

    96. /*Generating Fibnacci Series Using Recurssion*/main()

    {

    int n,f,i;clrscr();

    printf("/nEnter no.of terms:");

    scanf("%d",&n);

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    46/48

    int r,f;

    r=x-(x/y * y);

    if(r==0)

    return(y);

    else

    f=hcf(y,r);

    return f;

    }

    98. /*Addition Of Two Values Using Pointors in C*/

    main()

    {

    int *a,*b,*c;

    clrscr();

    printf("/nEnter 2 values:");

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

    *c=*a+*b;

    printf("/nsum=%d",*c);

    getch();

    }99. /*Comparing Two Values Using Pointors in C*/main()

    {

    int *a,*b;

    clrscr();

    printf("/nEnter 2 values:");

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

    if(*a>*b)

    printf("/n%d is big",*a);

    else

    printf("/n%d is big",*b);

    getch();

    }

    100. /*program to copy elements of array into another in reverseorder using pointors*/

    main()

    {

    static int a[10],b[10];

    int i,n=5,j;

    clrscr();

    printf("/nenter elements");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    47/48

    enter elements 2 3 5 6 0

    Elements in the reverse order

    0 6 5 3 2

    101. /*program To check if a[0]=a[n-1], a[1]=a[n-2] so on in the array

    of elements a[n]*/

    main()

    {

    static int a[10];

    int i,n=5;

    clrscr();

    printf("/nenter elements");

    for(i=0;i

  • 8/3/2019 c Programs With Solutions-2

    48/48

    o/p:

    enter elements

    2

    3

    6

    -6

    -3

    smallest element in the array-6

    103. /*program To Reverse The String Using Pointors*/#include

    main()

    {

    char *s,*c;

    static int i=0,j=0,len;

    clrscr();

    printf("/nEnter string:");

    gets(s);

    len=strlen(s);

    for(i=len-1;i>=0;i--){

    *(c+j)=*(s+i);

    j++;

    }

    *(c+j)='/0';

    printf("/nReverse string:");

    puts(c);

    getch();

    }

    o/p:

    Enter string:abc

    Reverse string:cba