C Programming Example

Post on 08-Jan-2017

64 views 0 download

Transcript of C Programming Example

C Programming Example

Question:- Write a program that represents Switch case, also add function in each case.

#include<stdio.h> int result; int add(int a, int b); int subtract(int a, int b); int multi(int a, int b);int main() { int x, y, choice; printf("\tEnter two number: ");scanf("%d%d", &x, &y); printf("\n"); printf("\tAddition. [1]\n"); printf("\tSubtraction .[2]\n"); printf("\tMultiplication.[3]\n\n");

switch(choice) { case 1: printf("\tTotal is %d\n", add(x, y)); break; case 2: printf("\tSubtraction is %d\n", subtract(x, y)); break;case 3: printf("\tMultiplication is %d\n", multi(x, y)); break; default: printf("\tYou entered wrong number!!\n"); break; } } int add(int a, int b) { result = a + b; return result; } int subtract(int a, int b) { result = a - b; return result; } int multi(int a, int b) { result = a * b; return result; }

Addition

Subtraction

Multiplication

Question:- C Program to check Whether a Number is Prime or Not?

Output

This program takes a positive integer from user and stores it in variable n. Then, for loop is executed which checks whether the number entered by user is perfectly divisible by i or not starting with initial value of i equals to 2 and increasing the value of i in each iteration.

If the number entered by user is perfectly divisible by i then, flag is set to 1 and that number will not be a prime number but, if the number is not perfectly divisible by i until test condition i<=n/2 is true means,it is only divisible by 1 and that number itself and that number is a prime number.

Code description:

#include <stdio.h>int main(){ double divi = 1, h = 0, sum = 0; double str[100]; int i = 0, j = 0, r, value;

printf("Division multiple integers.\n\n\n");printf("Enter the number of value = ");scanf("%d", &value);printf("\n\n");

Question:-How to divide multiple integers

Code:

for (j = 0; j < 1; j++){printf("enter %d%s value = ", j + 1, (j + 1 == 1) ? "st": (j + 1 == 2) ? "nd": (j + 1 == 3) ? "rd": " "); scanf("%lf", &str[j]); divi = str[j] / divi; printf("%.2lf\n", divi); }for (i = 1; i < value; i++){printf("enter %d%s value = ", i + 1, (i + 1 == 1) ? "st": (i + 1 == 2) ? "nd": (i + 1 == 3) ? "rd": "th"); scanf("%lf", &str[i]); divi = divi / str[i]; printf("%lf\n", divi);}getch();return 0;}

At first I use “stdio” header file, I use double data type and integer data type. now firstly the program want the number of value from the user that can be 5, 6, 7 and even more now the first for loop will get the first value from the user then the program put the value into "divi" that's because it can divide with the next value.

Now the second "for" loop will get the second value from user from the user and divide from the first value then the program do the same thing till the number of value that is given by the user. and al last it will show the result to the user. then terminated.

Code Description:

#include <stdio.h>int main(){ int qsn1, qsn2, qsn3, qsn4, qsn5, qsn6; printf ("1. What is the capital of Bangladesh?\n\n1. Dhaka\n2. Chittagong\n3. Khulna"); printf("\nEnter answer : "); scanf("%d", &qsn1); printf ("2. What is the best mobile company?\n\n1. Symphony\n2. Blackberry\n3. Apple"); printf("\nEnter answer : "); scanf("%d", &qsn2);printf ("3. Where is Bangladesh situated?\n\n1. Asia\n2. Africa\n3. America"); printf("\nEnter answer : "); scanf("%d", &qsn3); printf ("4. What is the capital of England?\n\n1. Dhaka\n2. London\n3. Khulna"); printf("\nEnter answer : "); scanf("%d", &qsn4);

Question:-A program that can check the given MCQ’s from the user.

printf (“5. What is the capital of Japan?\n\n1. Dhaka\n2. London\n3. Tokyo"); printf("\nEnter answer : "); scanf("%d", &qsn5); printf("\n\n");if (qsn1 == 1){ printf("1. True\n"); }else if (qsn1 == 3 || qsn1 == 2){ printf("1. False\n"); }if (qsn2 == 3){ printf("2. True\n"); }else if (qsn2 == 1 || qsn2 == 2){ printf("2. False.\n"); }if (qsn3 == 1){ printf("3. True\n"); }else if (qsn3 == 3 || qsn3 == 2){ printf("3. False.\n"); }

if (qsn4 == 2){ printf("4. True\n"); }else if (qsn4 == 1 || qsn4 == 3){ printf("4. False.\n"); } if (qsn5 == 3){ printf("5. True\n"); }else if (qsn5 == 1 || qsn5 == 2){ printf("5. False.\n"); } return 0;}

Output

Output