Monday, October 31, 2016

C programming questions based on Array.

  • What are Arrays in C programming?
  • Give the importance of Array in C language.
  • What do you mean by subscript in C array?
  • Explain the difference between char and int array.
  • What do you mean by compile time initialization? Give suitable example of Compile time initialization of C Array.
  • Describe difference between runtime and compile time initialization of array in C program.
  • Give suitable example of 1-D array declaration and initialization.
  • Justify the statement: C compiler never check the array index out of bound error.
  • Write a C program to demonstrate 2-D array declaration and runtime initialization.
  • Describe the array index out of bound error in context of C array program.
  • What do you mean by multi dimensional array?
  • Give suitable example to access 2-D array elements.
  • Justify the statement: Array index always begins with zero.

In case of any doubt, you can contact us through contact us page of the blog.

* * * * *


C programming Questions based on File Management.

  • List various modes of opening a file in C program.
  • Give the difference between “r”, “w” and “a” mode of file opening in C program.
  • What is the difference between w and w+ mode?
  • State the use of fopen() and fclose() function in C program.
  • Give the name of function which can be used to read a single character from a file in C program.
  • What is the use of FILE in C file management program?
  • Give the name of function which can be used to read a line from a file in C program.
  • What is the use of fprintf() function in C program?
  • Give difference between fputc() and fputs() function.
  • State the use of fscanf() function in C program.
  • Give difference between fgets() and fgetc() function. 

In case of any doubt, you can contact us through contact us page of the blog.

* * * * *

C programming questions based on Basic Concepts.

  • What is the use of <stdio.h> in c program?
  • What do you mean by .exe file?
  • Give the difference between Source file and Executable file.
  • What do you mean by Data types in C program?
  • State the difference between int and float data type.
  • Give the list of basic data types used in C program.
  • Explain the difference between float, char and int data type with suitable example.
  • What is the memory size occupied by char type variable?
  • State the purpose of semicolon in C program.
  • Give the use of #include in a C program.
  • What are identifiers in C language? State the use of identifiers in C program.
  • What are keywords in C language? Give list of any 10 C keywords.
  • Give the difference between break and continue.
  • State the use of goto statement in C language with suitable example.
  • Describe the use of sizeof keyword in C program with suitable example.
  • What do you mean by unsigned int in C program?
  • State the examples of whitespace characters used in C program.
  • What are enumerated data types in C language?
  • Give examples of derived data types of C language.

In case of any doubt, you can contact us through contact us page of the blog.

* * * * *


Tuesday, October 25, 2016

Count occurrences of alphabets in the given paragraph.

//Count occurrences of alphabets in the given paragraph.
//This program will count only A to Z alphabets.

#include<stdio.h>
int main(){
     int i, counter[26]={}, ch;
     char str[100]; 
     puts("Enter your paragraph:");
     gets(str);
    
     for(i=0; i<strlen(str); i++)   
     {
           ch=str[i];
           counter[ch-65]++;
     }
     printf("Character and it's frequency is as under:\n");
     for(i=0; i<26; i++) 
     {
           if(counter[i]!=0)
                printf("%c = %d \n", i+65,counter[i]);
     }
     return 0;
}


Output of the program:

Enter your paragraph:
HELLO STUDENT HOW ARE YOU
Character and it's frequency is as under:
A = 1
D = 1
E = 3
H = 2
L = 2
N = 1
O = 3
R = 1
S = 1
T = 2
U = 2
W = 1
Y = 1


Wednesday, October 12, 2016

C program to print password as star ( ***** )


C program to print password as star ( ***** )


#include<stdio.h>
int main(){
     int i;
     char ch1, password[10];
     printf("Enter your password:");
     for(i=0; i<10; i++)
     {
             ch1=getch();
             password[i]=ch1;
      
             if(ch1!=13)      //13 is ASCII of Enter key
                   printf("*");
             if(ch1 == 13)
                   break;
      }
      password[i]='\0';
      printf("\n%s", password);
      return 0;
}



Output of the program:

Enter your password:*****
Your actual password is hello


Monday, October 10, 2016

C program to print text at given position on screen.



C program to print text at given position on screen.

#include <windows.h>
#include<stdio.h>
#include<conio.h>
COORD c = {0, 0};

void setxy (int x, int y)
{
 c.X = x; c.Y = y; // Set X and Y coordinates
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int main()
{
 int x, y, rollno;
 char name[10];

 printf("Enter your Roll No.:");
 scanf("%d", &rollno);

 printf("Enter your name:");
 scanf("%s", name);

 printf("Where you want to print on Screen?\n");
 printf("Enter your X coordinate:");
 scanf("%d", &x);

 printf("Enter your Y coordinate:");
 scanf("%d", &y);

 setxy(x,y);
 printf("Your Roll No. is %d.", rollno);

 setxy(x,y+1); //add 1 in y to print at new line
 printf("Your Name is %s.", name);

 return 0;
}



Output of the program:

Enter your Roll No.:101
Enter your name:Purva
Where you want to print on Screen?
Enter your X coordinate:10
Enter your Y coordinate:10





          Your Roll No. is 0.
          Your Name is Purva.



Saturday, October 8, 2016

C program to implement Stack using array.



#include<stdio.h>
void push();
void pop();
void display_stack();
int stack_pointer=0, stack[10];

int main()
{
  int n;
  do{
      printf("===== Stack Operation =====\n");
      printf("1. Push\n2. Pop\n3. Display Stack\n4. Exit\n");
      printf("Enter your choice:");
      scanf("%d", &n);
      switch(n){
        case 1: push();
                if(stack_pointer>=10)
                {
                  printf("Stack is full.\n");
                  exit(0);
                }
                break;
        case 2: pop();
                break;
        case 3: display_stack();
                break;
        case 4: printf("Thank you. Have a nice time..");
                break;
        default:
                printf("Select proper menu item.\n");
        }
  }while(n != 4);
  return 0; 
}
void push(){
   printf("\nPush Operation\n");
   printf("--------------\n");
   printf("Enter number to be pushed:");
   scanf("%d", &stack[stack_pointer++]);
   printf("Push Operation performed.\n");
   printf("-------------------------\n\n");
}
void pop(){
   printf("\nPop Operation performed.\n");
   printf("------------------------\n");
   printf("Popped out element from stack is %d\n\n", stack[stack_pointer-1]);
   stack_pointer--;
   getch();//developed by kp
}
void display_stack(){
   int i;
   if(stack_pointer==0)
       printf("Stack is empty.\n\n");
   else
   {
       printf("\nStack Elements are as under:\n");
       printf("----------------------------\n");
       for(i=0; i<stack_pointer; i++)
       {
           printf("%d\n", stack[i]);
       }
   }
   printf("\nPress any key to continue...\n\n");
   getch();//cprogrampracticals.blogspot.in
}


Output of the program:

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:1

Push Operation
--------------
Enter number to be pushed:5
Push Operation performed.
-------------------------

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:1

Push Operation
--------------
Enter number to be pushed:10
Push Operation performed.
-------------------------

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:3

Stack Elements are as under:
----------------------------
5
10

Press any key to continue...

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:2

Pop Operation performed.
------------------------
Popped out element from stack is 10

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:4
Thank you. Have a nice time..

Friday, October 7, 2016

Bubble sort algorithm implementation.

#include<stdio.h>
int main()
{
     int i, j, n, temp, data[10];
     printf("Enter total numbers to be sorted:");
     scanf("%d",&n);
     //This program will be able to sort max 10 numbers.
    
     for(i=0; i<n; i++)
     {
           printf("Enter data[%d]:",i);
           scanf("%d",&data[i]);
     }
     printf("Unsorted data:");
     for(i=0; i<n; i++)
           printf("%d, ", data[i]);
    
     for(i=0; i<n; i++)
     {
           for(j=0; j<n-i-1; j++)
           {
                if(data[j] > data[j+1])
                {
                     temp = data[j];
                     data[j] = data[j+1];
                     data[j+1] = temp;
                }
                    
           }
     }
    
     printf("\nSorted data:");
     for(i=0; i<n; i++)
           printf("%d, ", data[i]);
     return 0;
}



Output of the program:

Enter total numbers to be sorted:5
Enter data[0]:1
Enter data[1]:3
Enter data[2]:2
Enter data[3]:4
Enter data[4]:5
Unsorted data:1, 3, 2, 4, 5,
Sorted data:1, 2, 3, 4, 5,


Sunday, October 2, 2016

C function with an argument and no return value.


//C function with an argument and no return value.

#include<stdio.h>

void print_line(int);

int main()
{
     print_line(2);  //this will pass 2 two print_line() function
    
     printf("Hello World\n");
    
     print_line(3);  //this will pass 3 two print_line() function
    
     return 0; 
}

void print_line(int n)
{
     int i;
     for(i=0; i<n; i++)
     {
           printf("------------\n"); 
     }
}


Output of the program:

------------
------------
Hello World
------------
------------
------------


C function with no arguments and no return value.


//C Function with no arguments and no return value.

#include<stdio.h>

void print_line();

void main()
{
     print_line();
    
     printf("Hello World\n");
    
     print_line();
}
//void in the function indicates no return value. 
void print_line() 
{
     printf("------------\n");
}


Output of the programs:

------------
Hello World
------------

Saturday, October 1, 2016

C Programming Quiz - 1 (1st October 2016)


C Programming Quiz - 1 (1st October 2016) with answer


1. Predict the output:

int main()
{
            int number[5]={1,2,3,4,5};
            printf("%d\n", number[5]);
            return 0;
}

A. 5
B. 0
C. Error
D. Any random number

Answer: D

Description: This will print any random number as number [5] is out of boundary of declared array. In c language, it is programmer’s responsibility to track array index out of bound error.


2. Predict the output:

int main(){
            int matrixA[3][3]={};
            printf("MatrixA[1][0] = %d\n",  matrixA[1][0] );
            return 0;

}
A. 0
B. 3
C. Error
D. 10

Answer: A
MatrixA[1][0] automatically initialized to zero due to compile time initialization.


3. Predict the output:

#include<stdio.h>
#include<string.h>
int main()
{
            char str1[10]="abc", str2[10]="xyz", str3[10]="pqr", str4[10];
            printf("%s",strcat(strcat(strcpy(str4, str3),str1),str2));
            return 0;
}
A. pqrabcxyz
B. pqrxyz
C. abcxyzpqr
D. Error

Answer: A
Here, inner most function strcpy will be executed first; and then outer functions strcat will be executed.


4. Predict the output:

#include<stdio.h>
int main(){
    char *p, str[10]="Ahmedabad";
    p = &str;
    printf("%c",*p+1);
    return 0;
}
A. A
B. h
C. m
D. B

Answer: D
This will print B, *p is A (i.e. 65), when added 1 gives B (i.e. 66 )


5. Predict the output:

#include<stdio.h>
void main(){
    char arr[10]="The Indian Programmers";
    printf("%s",arr);
}
A. The Indian
B. The Indian Programmers
C. The
D. None of these

Answer: A
This will print first ten characters, as size of arr is 10.



C Programming Quiz for Experts






Submit your quiz to check your result.