Programming in C Questions: 1. Write a program to demonstrate working of enum in C. 2. Write a C program to swap 2 values using call by reference. 3. Write a C program to demonstrate Example of recursion 4. Write a C program to illustrate pointers. 5. Write a C program to overcome the problem of a dangling pointer 6. Write a C program to store and display the student details using structure? 7. Can we compile a program without main() function in C? 8. Write a program to reverse a given number in C? 9. Write a C program to check palindrome number? 10. Write a C program to convert a number to a string with the help of sprintf() function? Program with Output: 1. Write a program to demonstrate working of enum in C. 2. #include <stdio.h> 3. enum week { Mon, Tue, Wed, Thur, Fri, Sat, Sun }; 4. 5. int main() 6. { 7. enum week day; 8. day = Wed; 9. printf("%d", day); 10. return 0; 11. } Output 2 2. Write a program to swap 2 values using call by reference: 1. #include <stdio.h> 2. void change(int*,int*); 3. int main() 4. { 5. int a=10,b=20; 6. change(&a,&b); // calling a function by passing references of variables. 7. printf("Value of a is: %d",a); 8. printf("\n"); 9. printf("Value of b is: %d",b); 10. return 0; 11. } 12. void change(int *x,int *y) 13. { 14. *x=13; 15. *y=17; 16. } Output: Value of a is: 13 Value of b is: 17 3. Write a program to demonstrate Example of recursion 1. #include <stdio.h> 2. int calculate_fact(int); 3. int main() 4. { 5. int n=5,f; 6. f=calculate_fact(n); // calling a function 7. printf("factorial of a number is %d",f); 8. return 0; 9. } 10. int calculate_fact(int a) 11. { 12. if(a==1) 13. { 14. return 1; 15. } 16. else 17. return a*calculate_fact(a-1); //calling a function recursively. 18. } Output: factorial of a number is 120 4. Write a program to illustrate pointers 1. #include <stdio.h> 2. int main() 3. { 4. int *p; //pointer of type integer. 5. int a=5; 6. p=&a; 7. printf("Address value of 'a' variable is %u",p); 8. return 0; 9. } Output: Address value of 'a' variable is 428781252 5. Write a program to overcome the problem of a dangling pointer 1. #include<stdio.h> 2. void main() 3. { 4. int *ptr = malloc(constant value); //allocating a memory space. 5. free(ptr); //ptr becomes a dangling pointer. 6. ptr=NULL; //Now, ptr is no longer a dangling pointer. } 6. Write a program to store and display the student details using structure? 1. #include <stdio.h> 2. struct student 3. { 4. char name[10]; 5. int age; 6. }s1; // structure members declaration. //structure variable 7. int main() 8. { 9. printf("Enter the name"); 10. scanf("%s",s1.name); 11. printf("\n"); 12. printf("Enter the age"); 13. scanf("%d",&s1.age); 14. printf("\n"); 15. printf("Name and age of a student: %s,%d",s1.name,s1.age); 16. return 0; 17. } Output: Enter the name shikha Enter the age 26 Name and age of a student: shikha,26 7. Can we compile a program without main() function? Yes, we can compile, but it can't be executed. But, if we use #define, we can compile and run a C program without using the main() function. 1. #include<stdio.h> 2. #define start main 3. void start() { 4. printf("Hello"); 5. } 8. Write a program to reverse a given number in C? 1. #include<stdio.h> 2. #include<conio.h> 3. main() 4. { 5. int n, reverse=0, rem; //declaration of variables. 6. clrscr(); // It clears the screen. 7. printf("Enter a number: "); 8. scanf("%d", &n); 9. while(n!=0) 10. { 11. rem=n%10; 12. reverse=reverse*10+rem; 13. n/=10; 14. } 15. printf("Reversed Number: %d",reverse); 16. getch(); // It reads a character from the keyword. 17. } Output: Enter a number: 123 Reversed Number: 321 9. Write a program to check palindrome number in C Programming? 1. #include<stdio.h> 2. #include<conio.h> 3. main() 4. { 5. int n,r,sum=0,temp; 6. clrscr(); 7. printf("enter the number="); 8. scanf("%d",&n); 9. temp=n; 10. while(n>0) 11. { 12. r=n%10; 13. sum=(sum*10)+r; 14. n=n/10; 15. } 16. if(temp==sum) 17. printf("palindrome number "); 18. else 19. printf("not palindrome"); 20. getch(); 21. } Output: enter the number=151 palindrome number enter the number=5621 not palindrome number 10. Write a program to convert a number to a string with the help of sprintf() function #include <stdio.h> #include <string.h> int main() { char res[20]; float a = 32.23; sprintf(res, "%f", a); printf("\nThe string for the num is %s", res); return 0; } Output The string for the num is 32.230000