Fghjk

Submitted by: Submitted by

Views: 61

Words: 600

Pages: 3

Category: Business and Industry

Date Submitted: 08/31/2014 11:31 AM

Report This Essay

C Programming Sample Questions and Answers

Question 1:

Write a program to accept two numbers. Multiply bo

th numbers and divide that result by two.

Print the result of that calculation.

Solution:

#include<stdio.h>

#include<conio.h>

void main ()

{

int x, y, result;

printf(“Enter first number: “);

scanf(“%d”, &x);

printf(“Enter second number: “);

scanf(“%d”, &y);

result = (x * y ) / 2;

printf(“Answer is %d”, result);

getch();

}

Question 2:

Write a program to accept a score. If this score i

s not equal to 100 then display “No Distinction”,

otherwise display “Distinction”.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int score;

printf(“Accept score: “);

scanf(“%d”, &score);

if (score != 100)

printf(“No Distinction”);

else

printf(“Distinction”);

getch();

}

Question 3:

Write a program to accept a number. Use the case s

tatement to display “Burger King” when

number is 1, “McDonalds” when number is 2, and “Wen

dy’s”, when number is 3. Display “No

such number” if number wasn’t any of the above.

Solution:

#include<stdio.h>

#include<conio.h>

void main ()

{

int number;

printf(“Enter a number: “);

scanf(“%d”, &number);

switch (number)

{

case 1 : printf(“Burger King”);

break;

case 2 : printf(“McDonald’s”);

break;

case 3 : printf(“Wendy’s”);

break;

default : printf(“No such number”);

}

getch();

}

Question 4:

Write a program to display “Shagadelic” 10 times us

ing a do while loop. Display “Yeah baby” at

the end of this do while loop.

Solution:

#include<stdio.h>

#include<conio.h>

void main ()

{

int x = 1; //set a counter variable called x to 1

do {

printf(“Shagadelic\n”);

x ++; // Add 1 to the counter variable

} while (x <= 10);

printf(“Yeah Baby”);

getch();

}

Question 5:

Write a program to count backwards, from 10 to 1 (u

sing while loop) and print...

More like this