Monday, 11 November 2013

Answers to basic programming.
 
#include <stdio.h>
int main()
{
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %f", a);
      if(b>=a && b>=c)
         printf("Largest number = %f", b);
      if(c>=a && c>=b)
         printf("Largest number = %f", c);
      return 0;
}


number 2
#include <stdio.h>
int main()
{
    char a[4];
    int b=0;
    char c;
printf("Enter the four digit number \n");
scanf("%s",&a);

for (b=0;b<=3;b++)
{
    c = a[b];
        printf("%c, ",c);
}
return 0;
}
number 3
#include <stdio.h>
int main()
{
    int n; /*is the number of times we are looping*/
    int b; /* b is the loop counter.*/
    int c; /* c is the number we are entering*/
printf("Enter n  \n");
scanf("%d",&n);

for (b=0;b<=(n-1);b++)
{
    printf("Enter a number  \n");
    scanf("%d",&c);

    if (c<0)
    {
        break;
    }
}
return 0;
}
number 4
#include<stdio.h>
int main()
{
    int r;/*number of rows*/
    int c;/*number of columns */
    int d=0;/*number of stars to print*/

    for (c=0;c<=5;c++)/*loop*/
    {

        for (r=0;r<=d;r++)/*loop*/
        {
        printf("*");
        }
        d++;/*increment d from one star to the next*/
        printf("\n");/*next line*/

    }
    /* second part */
    d=5;
    for (c=5;c>=0;c--)/*loop*/
    {

        for (r=0;r<=d;r++)/*loop*/
        {
        printf("*");/*the stars to be printed*/
        }
        d--;/*decrease no of rows.*/
        printf("\n");/*next line*/
    }
    return 0;
    }
number5
#include <stdio.h>
#include <math.h>

void main()
{
    int p;
    int x;
    int y;
    int s;
    float total;
    printf("enter x");
    scanf("%d",&x);
    printf("enter y");
    scanf("%d",&y);
    p=x*y;
    s=x+y;
    total = s*s + p *(s-x) * (p+y);
    printf("Total is: %f",total);
}
number 6
#include <math.h>
#include <stdio.h>
int main()
{
    int x,x1;
    int a,b,c;
    printf("enter a b and c");
    scanf("%d %d %d",&a,&b,&c);
    /*if a and b are zero=>no solution*/
    /*if a is zero=>one root(-c/b)*/
    /*if b*b-4ac is negative=>no roots*/
    /*otherwise=> two roots*/
    x= (-b - sqrt((b*b)-(4*a*c)))/(2*a);
    x1 =( -b+ sqrt((b*b)-(4*a*c)))/(2*a);

    printf("Root one: %d and root two is: %d",x,x1);
    return 0;
}

No comments:

Post a Comment