Showing posts with label Option. Show all posts
Showing posts with label Option. Show all posts

C Program to Multiple Choice Question and Answer

How to write a C Program to Multiple Choice Question and Answer ( Choose an option from 1-4 please ) in C Programming Language ?



Solution:

  1.  #include <stdio.h>
  2.    
  3.     main ()
  4.     {
  5.         int num;
  6.         int count;
  7.         int ans;        // integer for ans
  8.        
  9.         printf("              MAIN MENU           \n");
  10.         printf("\n Choose an option from 1-4 please.  ");
  11.         scanf("%d", &num);
  12.        
  13.         switch (num)
  14.         case 1:
  15.         {
  16.             {
  17.            
  18.             printf("Enter the number of questions to be asked for this round of the quiz! (Max of 5 questions allowed). \n");
  19.             scanf("%d", &num);
  20.             flushall();
  21.             }
  22.        
  23.         case 2:
  24.         {
  25.             {
  26.                 printf("What is 1 x 5?");
  27.                 scanf("%d", &ans);
  28.                 flushall();
  29.        
  30.             }
  31.         if (ans==5)
  32.         {
  33.             ans = 1 * 5;
  34.             printf(" Correct! Next question!", ans);
  35.              = count++;
  36.         }
  37.         else
  38.         {
  39.             printf("Wrong! ans is 5!");
  40.         }
  41.        
  42.          printf("\n What is 10 /2?");
  43.             scanf("%d", &ans);
  44.             flushall();
  45.        
  46.            
  47.         if(ans==6)
  48.         {  
  49.             ans = 12/2;
  50.             printf(" Correct! Next question!", ans);
  51.         }
  52.         else
  53.         {
  54.             printf("Wrong! ans is 6!");
  55.         }
  56.        
  57.         break;
  58.         }
  59.    
  60.         case 3:
  61.            
  62.         {
  63.          
  64.            printf("You have ansed %d correct ans",   count);
  65.         break;
  66.         }
  67.        
  68.         default:
  69.             printf("End");
  70.         break;
  71.         }
  72.    
  73.         getchar();
  74.     }

Menu driven program in C which performs the following operations on strings

How to write a menu driven program in C which performs the following operations on strings:
  1. Write separate function for each option 
  2. Check if one string is sub-string of another string 
  3. Count number of occurrences of a character in the string 
  4. Exit

Solution:

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void substring();
  void char_occurrence();
  void main()
{
  char str[30],str1[30];
  int ch;
  clrscr();
  do
{
  printf("\n****MENU****");
  printf("\n1:Sub String");
  printf("\n2:Character Occurrences");
  printf("\n3:Exit");
  printf("\nEnter your choice: ");
  scanf("%d",&ch);

  switch(ch)
{
  case 1:substring();
  break;

  case 2:char_occurrance();
  break;

  case 3:exit(0);
  break;
}
}
  while(ch!=3);
  getch();
}

  void substring()
{
  char str[30],ch[30];
  fflush(stdin);
  printf("\nEnter the first string: ");
  gets(str);
  fflush(stdin);
  printf("\nEnter the second string: ");
  gets(ch);
  if(strstr(str,ch))
{
  printf("\nThe given string is sub string");
}
  else
{
  printf("\nThe given string is not sub string");
}
}

  void char_occurrennce()
{
  char str2[30],ch;
  int i,count=0;
  fflush(stdin);
  printf("\nEnter the string: ");
  gets(str2);
  fflush(stdin);
  printf("\nEnter the character from string to find occurrence: ");
  scanf("%c",&ch);
  for(i=0;str2[i]!='\0';i++)
{
  if(ch==str2[i])
{
  count++;
}
}
  printf("\nThe occurrence of character is: %d",count);
}

//OUTPUT:
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit

//Enter your choice: 1

//Enter the first string: vinit

//Enter the second string: vinit

//The given string is sub string

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 2

//Enter the string: vinit

//Enter the character from string to find occurrence: v

//The occurrence of character is: 1

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 3