Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

C Programming and Applications: Questions and Solutions for Practice - 2

This is a continuation from the this blog page


5. What is the difference between local variable and global variable in C?   
                             
Global Variable is a variable which is declared before main () and Local Variable is a variable which is declared inside the main ().


6. Write down the name of three relational operators and three arithmetic operators used in C.
                                                                                                                                          
Relational Operators: less than, greater than, equal to
Arithmetic Operators: Addition, Subtraction, Multiplication 


7. What is the difference between break and continue statements? Explain the difference with examples.

The break statement will immediately jump to the end of the current block of code.
The continue statement will skip the rest of the code in the current loop block and will return to the evaluation part of the loop. In a do or while loop, the condition will be tested and the loop will keep executing or exit as necessary.

#include <stdio.h>                                         
#include <conio.h>

main ()
{int i;
 for(i=0; i<100; i++)
  {
  printf("\n%d",i);
    if(i==10)
  break; 
}
getch ();
}
------------------------------------------------------------
#include <stdio.h>
#include <conio.h>

main ()
{int i;

 for(i=0; i<100; i++)
  {
    if(i==10)
  continue; 
  printf("\n%d",i);}


8. Write a complete program which will display the consecutive digits from 0 to 9 with one digit on each line using do-while statement.

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int digit=0;
     do {printf("%d\n",digit++);}
     while (digit<=9);
     getch ();}


9.  Fill in the blanks of the following program written in Turbo C. Here the program will take a number from the user and mention whether it is even or odd.

 #include <stdio.h>
#include <conio.h>
main ()
{
int x;
printf("enter a number");
scanf(___"%d",&x________);
odd:
printf("\nThe number is %d odd",x);
if(_____x%2==0________)
{goto even;}
elseif
{_____goto odd;_______;}
___even_____:
printf(_____"\nThe number is %d even",x_____________);

getch ();
}


10. Write a complete program to print the following multiplication table using two dimensional array.


MULTIPLICATION TABLE

Showing multiplication table













#include <stdio.h>
#include <conio.h>
#define ROWS 10
#define COLUMNS 10

main ()
{
     int row,column,product[ROWS][COLUMNS];
     int i,j;
     printf("\t\t MULTIPLICATION TABLE \n\n");
     printf("\t");
     for(j=1; j<=COLUMNS; j++)
     {printf("%d\t",j);}
     printf("\n");
     printf("________________________\n");
     for(i=0; i<ROWS; i++)
     {
              row=i+1;
              printf("%d|\t",row);
              for(j=1; j<=COLUMNS; j++)
              {
                       column=j;
                       product[i][j]=row*column;
                       printf("%d \t",product[i][j]);
                       }
                       printf("\n");
                       }
                       getch ();}

No comments:

Post a Comment