Sie sind auf Seite 1von 10

C Programming Sample Questions and Answers Question 1: Write a program to accept two numbers.

Multiply both 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 is not more than 80 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>= 80)) printf(No Distinction); else printf(Distinction); getch(); } Question 3: Write a program to accept a number. Use the case statement to display Burger King when number is 1, McDonalds when number is 2, and Wendys, when number is 3. Display No such number if number wasnt 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(McDonalds); break; case 3 : printf(Wendys); break; default : printf(No such number); } getch(); } Question 4: Write a program to display Shagadelic 10 times using 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(); }

SP@PS_Page: 1

Question 5: Write a program to count backwards, from 10 to 1 (using while loop) and print Hello each time. Solution: #include<stdio.h> #include<conio.h> void main () { int x = 1; while (x >= 1) { printf(Hello\n); x--; } getch(); } Question 6: Write a program that displays Hello 10 times using the for-loop. Solution: #include<stdio.h> #include<conio.h> void main() { int x; // you dont need to set x at an initialising value, its taken care of in the for loop for (x=1; x<=10; x++) { printf(Hello\n); } getch(); } Question 7: Declare an array named Twix that can hold two floating point variables. Accept two numbers into this array and add both numbers and store the result in Answer. Display Answer when done. Solution: #include<stdio.h> #include<conio.h> void main() { float Twix[2], Answer; printf(enter first number: ); scanf(%f, &Twix*0+); printf(enter second number: ); scanf(%f, &Twix*1+); Answer = Twix[0] + Twix[1];

printf(Result is : %f, Answer); getch(); } Question 8: Write a program that calls a function named Pinky. Let Pinky display a message saying Hi. Solution: #include<stdio.h> #include<conio.h> void Pinky (void); void main() { Pinky(); getch(); } void Pinky (void) { printf(Hi); } Question 9: Write a program that accepts a number and sends it into a function named TRES. Let TRES multiply that number by three and return the result to main. Print that result in main. Solution: #include<stdio.h> #include<conio.h> int TRES (int); void main() { int num, result; printf(Enter a number: ); scanf(%d, &num); result = TRES (num); printf(Answer is %d, result); getch(); } int TRES (int num) { return (num * 3); }

SP@PS_Page: 2

Question 10: What is C language?


C language is a middle level programing language. It is also popular as structured, procedural programming language. C is widely used both for operating systems and application software. It is also called a system programming language. C is high level language.

Example1:
/* Recursive function-Fibonaci Series*/ #include <stdio.h> int fibonaci(int i) { if(i == 0) { return 0; } if(i == 1) { return 1; } return fibonaci(i-1) + fibonaci(i-2); } int { main() int i; for (i = 0; i < 10; i++) { printf("%d\t", fibonaci(i)); } return 0; } output 0 1 1 2 3 5 8 13 21 34

Question 11: What is the difference between "calloc(...)" and "malloc(...)"? Answer1
The difference between calloc and malloc are given below: malloc: malloc takes only the "size" of the memory block to be allocated as input parameter. malloc allocates memory as a single contiguous block. if a single contiguous block cannot be allocated then malloc would fail. calloc: calloc takes two parameters: the number of memory blocks and the size of each block of memory calloc allocates memory which may/may not be contiguous. all the memory blocks are initialized to 0. it follows from point 2 that, calloc will not fail if memory can be allocated in non-contiguous blocks when a single contiguous block cannot be allocated.

Example2:
/*Recursive fx Factorial Calculation*/ #include <stdio.h> int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 15; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; }

Answer2
malloc: malloc create the single block of given size by user calloc: calloc creates multiple blocks of given size both return void pointer(void *)so boh requires type casting malloc: eg: int *p; p=(int*)malloc(sizeof(int)*5) above syntax tells that malloc occupies the 10 bytes memeory and assign the address of first byte to P calloc: eg: p=(int*)calloc(5,sizeof(int)*5) above syntax tells that calloc occupies 5 blocks each of the 10 bytes memeory and assign the address of first byte of first block to P

Question 12: What is Recursion? Answer: Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows. SP@PS_Page: 3

Question 12: What is Structure? Structure is user defined data type available in C programming, which allows you to combine data items of different kinds unlike arrays. Arrays holds the data of similar type where as structure holds that data of mix types. Example: To define a structure, we must use the struct statement. The struct statement defines a new data type, with more than one member for your program. The format of the struct statement is this:
Syntax: struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables]; Examples: struct complex {

double real; double imag; }; struct complex { double real; double imag; } num1, num2; struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;

Now a program where structure is used:


#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; /*Declare Book1 of type Book */ struct Books Book2; /*Declare Book2 of type Book */ /* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info */ printf( "Book 1 title : %s\n", Book1.title); printf( "Book 1 author : %s\n", Book1.author); printf( "Book 1 subject : %s\n", Book1.subject); printf( "Book 1 book_id : %d\n", Book1.book_id); /* print Book2 info */

SP@PS_Page: 4

printf( printf( printf( printf(

"Book "Book "Book "Book

2 2 2 2

title : %s\n", Book2.title); author : %s\n", Book2.author); subject : %s\n", Book2.subject); book_id : %d\n", Book2.book_id);

return 0; } Output: When the above code is compiled and executed, it produces the following result: Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700

Now Observe the Different Way Using Function


#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books book ); int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info */ printBook( Book1 ); /* Print Book2 info */ printBook( Book2 ); return 0; } void printBook( struct Books book ) { printf( "Book title : %s\n", book.title); printf( "Book author : %s\n", book.author); printf( "Book subject : %s\n", book.subject); printf( "Book book_id : %d\n", book.book_id); }

Result:
SP@PS_Page: 5

Book Book Book Book Book Book Book Book

title : C Programming author : Nuha Ali subject : C Programming Tutorial book_id : 6495407 title : Telecom Billing author : Zara Ali subject : Telecom Billing Tutorial book_id : 6495700

Let us re-write above example using structure pointer, hope this will be easy for you to understand the concept:
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; /* function declaration */ void printBook( struct Books *book ); int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali"); strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info by passing address of Book1 */ printBook( &Book1 ); /* print Book2 info by passing address of Book2 */ printBook( &Book2 ); return 0; } void printBook( struct Books *book ) { printf( "Book title : %s\n", book->title); printf( "Book author : %s\n", book->author); printf( "Book subject : %s\n", book->subject); printf( "Book book_id : %d\n", book->book_id); }

When the above code is compiled and executed, it produces the following result:
Book Book Book Book Book Book Book Book title : C Programming author : Nuha Ali subject : C Programming Tutorial book_id : 6495407 title : Telecom Billing author : Zara Ali subject : Telecom Billing Tutorial book_id : 6495700

SP@PS_Page: 6

Question 14: What is Union?

Unions are declared, created, and used exactly the same as struts, EXCEPT for one key difference: o Structs allocate enough space to store all of the fields in the struct. The first one is stored at the beginning of the struct, the second is stored after that, and so on. o Unions only allocate enough space to store the largest field listed, and all fields are stored at the same space - The beginnion of the union. This means that all fields in a union share the same space, which can be used for any listed field but not more than one of them. In order to know which union field is actually stored, unions are often nested inside of structs, with an enumerated type indicating what is actually stored there. For example:

#include<stdio.h> int main(){ int n,i,sum; int min,max; printf("Enter the minimum No: "); scanf("%d",&min); printf("Enter the maximum No: "); scanf("%d",&max); printf("Perfect Nos. in range: "); for(n=min;n<=max;n++){ i=1; sum = 0; while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d ",n); } return 0; } Sample output: Enter the minimum range: 1 Enter the maximum range: 20 Perfect numbers in given range is: 6 3. C program to print perfect numbers from 1 to 100 #include<stdio.h> int main(){ int n,i,sum; printf("Perfect numbers are: "); for(n=1;n<=100;n++){ i=1; sum = 0; while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n)
SP@PS_Page: 7

Perfect number

1. C program to check perfect number #include<stdio.h> int main(){ int n,i=1,sum=0; printf("Enter a number: "); scanf("%d",&n); while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d is a perfect number",i); else printf("%d is not a perfect number",i); return 0; }
Definition of perfect number or What is perfect number? Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of

6 are 1, 2 and 3.
1 + 2+ 3 =6

Sum of its divisor is

Note: 6 is the smallest perfect number. Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28 Some more perfect numbers: 496, 8128

printf("%d ",n); } return 0; } Output: Perfect numbers are: 6 28


PALINDROME -String

return 0; }
PALINDROM IN A RANGE

/* Wap to check GIVEN STRING is palindrome OR NOT*/ #include<string.h> #include<stdio.h> int main(){ char *str,*rev; int i,j; printf("\nEnter a string:"); scanf("%s",str); for(i=strlen(str)-1,j=0;i>=0;i-,j++) rev[j]=str[i]; rev[j]='\0'; if(strcmp(rev,str)) printf("\nThe string is not a palindrome"); else printf("\nThe string is a palindrome"); return 0; }
NUMBER PALINDROME

#include<stdio.h> int main(){ int num,r,sum,temp; int min,max; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); printf("Palindrome numbers in given range are: "); for(num=min;num<=max;num++){ temp=num; sum=0; while(temp){ r=temp%10; temp=temp/10; sum=sum*10+r; } if(num==sum) printf("%d ",num); } return 0; } Sample output: Enter the minimum range: 1 Enter the maximum range: 50 Palindrome numbers in given range are: 1 2 3 4 5 6 7 8 9 11 22 33 44
PALINDROME USING FOR LOOP

/* Wap to check a number is palindrome*/ #include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num){ r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp);

#include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); for(temp=num;num!=0;num=num/10){ r=num%10; sum=sum*10+r; } if(temp==sum)
SP@PS_Page: 8

printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp); return 0; } Sample output: Enter a number: 1221 1221 is a palindrome
PALINDROME USING RECURSION

PRIME AND COMPOSITE NUMBERS

#include<stdio.h> int main(){ int num,i,count=0; printf("Enter a number: "); scanf("%d",&num); for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } Sample output: Enter a number: 5 5 is a prime number
PRIME FROM 1 TO 100

1. C program to check if a number is palindrome using recursion #include<stdio.h> int checkPalindrome(int); int main(){ int num,sum; printf("Enter a number: "); scanf("%d",&num); sum = checkPalindrome(num); if(num==sum) printf("%d is a palindrome",num); else printf("%d is not a palindrome",num); return 0; } int checkPalindrome(int num){ static int sum=0,r; if(num!=0){ r=num%10; sum=sum*10+r; checkPalindrome(num/10); } return sum; } Sample output: Enter a number: 25 25 is not a palindrome

#include<stdio.h> int main(){ int num,i,count; for(num = 1;num<=100;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d ",num); } return 0; }
PRIME FROM 1 TO N numbers

#include<stdio.h> int main(){ int num,i,count,n;


SP@PS_Page: 9

printf("Enter max range: "); scanf("%d",&n); for(num = 1;num<=n;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d ",num); } return 0; }
PRIME NUMNERS IN A RANGE

Sample output: Enter min range: 50 Enter max range: 100 53 59 61 67 71 73 79 83 89 97


ARMSTRONG NUMBER

#include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num!=0){ r=num%10; num=num/10; sum=sum+(r*r*r); } if(sum==temp) printf("%d is an Armstrong ",temp); else printf("%d isnt Armstrong ",temp); return 0; } Sample output: Enter a number: 153 153 is an Armstrong number
ARMSTRONG NUMBER USING FOR LOOP

#include<stdio.h> int main(){ int num,i,count,min,max; printf("Enter min range: "); scanf("%d",&min); printf("Enter max range: "); scanf("%d",&max); num = min; while(num<=max){ count = 0; i=2; while(i<=num/2){ if(num%i==0){ count++; break; } i++; } if(count==0 && num!= 1) printf("%d ",num); num++; } return 0; }

#include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); for(temp=num;num!=0;num=num/10){ r=num%10; sum=sum+(r*r*r); } if(sum==temp) printf("%d is Armstrong",temp); else printf("%d isnt Armstrong",temp); return 0; } Sample output: Enter a number: 370 370 is an Armstrong number Logic of Armstrong number in c

SP@PS_Page: 10

Das könnte Ihnen auch gefallen