C Programs.problems

download C Programs.problems

of 8

Transcript of C Programs.problems

  • 7/31/2019 C Programs.problems

    1/8

    Program to find the biggest number in given three numbers:

    #include

    main()

    {int a,b,c;

    clrscr();

    printf(Enter any three numbers=);

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

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

    {

    printf(A is big);

    }

    elseif (b>c)

    {printf(B is big);

    }else

    {printf(C is big);

    }

    getch();}Program to use switch statement:

    #include

    main()

    {int a,b,c,choice;

    clrscr();

    printf(Choice of Demonstration\n);

    printf(1.Addition\n);printf(2.Subtraction\n);

    printf(3.Multiplication\n);

    printf(4.Division\n);

    printf(Enter the number corresponding to your choice=);

    scanf(%d,&choice);

    switch(choice)

    {case 1:

    printf(Enter any two numbers=);

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

  • 7/31/2019 C Programs.problems

    2/8

    printf(A+B=%d\n,c);

    break;

    case 2:

    printf(Enter any two numbers=);

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

    c=a-b;printf(A-B=%d\n,c);

    break;

    case 3:

    printf(Enter any two numbers=);

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

    c=a*b;

    printf(A*B=%d\n,c);

    break;

    case 4:

    printf(Enter any two numbers=);scanf(%d%d,&a,&b);

    c=a/b;

    printf(A/B=%d\n,c);

    break;

    default:printf(You have entered the wrong number please try again\n);

    break;

    }getch();

    }

    Program for prime number or not

    #include

    main()

    {

    int n, c = 2;

    printf("Enter a number to check if it is prime\n");

    scanf("%d",&n);

    for ( c = 2 ; c

  • 7/31/2019 C Programs.problems

    3/8

    Program to print a table:#include

    {

    int a=0,b=1,n;

    clrscr();

    do

    {

    scanf(%d,&n);

    b=b*n;

    a++;

    }while(a

  • 7/31/2019 C Programs.problems

    4/8

    Program to print factorial of given number:

    #include

    main()

    {

    int a,b;

    long fact = 1;

    clrscr();

    printf(Enter a number=);

    scanf(%d,&a);

    b=1;

    do

    {

    fact = fact*b;b++;

    }while(b

  • 7/31/2019 C Programs.problems

    5/8

    Printing given number Table:

    #include

    main()

    {

    int a,b;clrscr();

    printf(Enter the number\n=);

    scanf(%d,&a);

    for(b=1;b

  • 7/31/2019 C Programs.problems

    6/8

    Functions adding two numbers#include

    void main()

    {int sum(int a,int b);

    int c;

    clrscr();

    printf("\nEnter a & b values=");

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

    c=add(a,b);

    printf("Result is=%d",c);

    }

    int add(int x,int y){

    int c;

    c=a+b;

    return c;

    }Reading & writing of array elements:

    #include

    void main ()

    {int a[5]={5,8,3,1,6};

    int i;

    clrscr();

    for(i=0;i

  • 7/31/2019 C Programs.problems

    7/8

    }

    Reading & writing of 2 dimensional arrays

    #include

    void main(){

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

    clrscr();

    printf("\nEnter any nine elements:");

    for(r=0;r

  • 7/31/2019 C Programs.problems

    8/8

    Strings: strings are arrays of characters i.e. they are characters arranged

    one after another in memory. To mark the end of the string, C uses the null

    character. Strings in C are enclosed with in double quotes.

    #include

    main()

    {

    char string[] = "Hello World";

    printf("%s\n", string);

    }

    Output of program: