Pseudo code is one of the methods that could be used to represent an algorithm. It is not written in a specific syntax that is used by a programming language and therefore cannot be executed in a computer. It just used for human understand. Basically it acts as a bridge between the program and the algorithm or flowchart.
Machine language is the only language that is directly understood by the computer. The machine language code is written as string of 1’s and 0’s. In this not need any translator program.
When one of the many alternatives is to be selected the switch statement allows a program to select one statement for execution out of a set of alternatives and the remaining statements will be skipped.
Syntax:
switch(expression)
{
case choice1:
statements;
break;
case choice2:
statements;
break;
case choice N:
statements;
break;
default:
statements;
}
The expression must be any integer or character type. The choice 1, choice 2 and choiceN are the possible values which we are going to test with the expression value.
The C program depends upon some header files for function declaration that are used in the program. So, header file is collection of identifiers and generally contains declaration of predefined function. For example, the function printf() and scanf() are declared in stdio.h header file.
An array is finite collection of homogeneous (same type) elements. An array is a data structure that can store multiple elements of same type under one name.
“So, an array is a linear collection of similar data type elements, stored in unique and successive memory locations.”
DECLARATION:
Data_Type Variable_Name [Size];
EXAMPLE:
int arr[7];
The strcat() function joins two strings together. When we combine two strings, this function adds the character of one string to the end of other string.
Syntax:
strcat(String1, String2);
When the function strcat() is executed String2 is appended to String1. The string String2 remains unchanged.
The malloc() function is used to allocate memory space in bytes to the variable of different data types. The function reserves bytes of determined size and return the base address to pointer variable.
Syntax:
Pointer_Variable= (Data_Type *) malloc (Block Size);
The variable which is created or declared inside the any block is called local variable. The local variables only exist inside the specific function that creates them and they are unknown to other functions. The scope of local variable is limited to the block in which it is declared.
The variable which is declared outside the any block is called global variable. The scope of global variable is global and exists throughout the program.
int a = 45;//Global Variable.
void main()
{
int b = 15;//Local Variable.
}
First of all, both structure and union are user defined data type but there are few differences between them, they are:
This function is used for writing characters, strings, integers, float etc. to the file. The fprintf()function contains one more parameter that is file pointer, which points the opened file.
Graphical representation of any problem is called flowchart. A flowchart is a visual representation of the sequence of steps and decisions needed to perform a process. In flowchart various types of graphical symbol is used for represent different activities.
Logical operators are used to check logical relation between two expressions. The expressions may be variables, constants.
OPERATOR | NAME | EXAMPLE | RESULT |
&& | Logical AND | 5>3 && 5<10 | 1 |
|| | Logical OR | 8>5 || 8<2 | 1 |
! | Logical NOT | !(8!=9) | 0 |
The loop is a block of statements that are executed again and again till a specific condition is satisfied. On the basis of condition checking, the loops are divided into two types, entry-control and exit-control loop. The while loop is entry-control and do-while is exit-control loop.
THE while LOOP:
In while loop the test condition is indicated at the top and it tests the value of the expression before processing the body of the loop.
//Syntax:
while(condition)
{
Statement;
Statement;//Body of Loop.
Statement;
}
//Example:
//WAP TO PRINT “Suraku Academy” 5 TIMES.
void main()
{
int i=1;
while(i<=5)
{
printf("Suraku Academy ");
i++;
}
}
//Syntax:
do
{
Statement;
Statement;//Body of Loop.
Statement;
} while (condition);
//Example:
//WAP TO PRINT “Suraku Academy” 5 TIMES.
void main()
{
int i=1;
do
{
printf("Suraku Academy ");
i++;
} while(i<=5);
}
Write a code in ‘C’ to compute factorial of a number using recursion.
//WAP to Calculate Factorial of Given Number Using Recursion.
long int fact(int);//Function Declaration.
void main()
{
int num;
long int f;
clrscr();
printf("Enter The Number: ");
scanf("%d",&num);
f=fact(num);//Function Calling.
printf("Factorial of Given Number: %ld",f);
getch();
}
long int fact(int n)//Function Definition.
{
long int f;
if(n>0)
{
f=n*fact(n-1);
return(f);
}
return(1);
}
OPEN A FILE:
A file has to be opened before read and write operations. Opening of file means load the file in RAM. For opening the file, we use the predefined function fopen(). In fopen() function we pass name of the file and its mode of operation as an argument.
CLOSE A FILE:
The file is opened by the fopen() should be closed after the work is over, i.e. we need to close the file after reading and writing operations.
Programming languages exist to enable programmers to develop software effectively. But how efficiently programmers can write software depends on the usability of the languages and tools that they develop with. In order to understand the various constructs of a programming language and its capabilities, it is useful to know some evaluation criteria. Some of the language criterias to evaluate a programming language are:
Readability:
The first and most obvious criteria is certainly readability. Coding should be simple and clear to understand.
Writability:
Writability is a measure of how easily language can be used to code.Most of the language characteristics that affect readability also affect writability.
Reliability:
It refers to type checking (It is testing for type error, either at compile or run time. For example, float height; is more desirable as compare to int height;.), exception handling (It is the ability of program to handle run time error. Remember, handling runtime error are more expensive than compile errors.), aiasing (It is same memory location having more than one name. Which is causes confusion), etc.
Cost:
Total cost of programming should be minimum. For example, cost of trainer, Cost of writing algorithm, Cost of compiling program in the language, Cost of hardware required for program, Cost of maintenance, etc.
Generality:
Language should not be limited to specific application only.
Extensibility:
The programming language should be flexible, must be able to add new constructs.
Standardability:
The programming language should be platform independent.
//C Program:
void main()
{
int a=0,b=1,next,i=3,n;
printf("Enter Value of N : ");
scanf("%d",&n);
printf("The Series ");
printf("%d %d ",a,b);
while(i<=n)
{
next=a+b;
printf("%d ",next);
a=b;
b=next;
i++;
}
getch();
}
OUTPUT
Enter Value of N : 8
The Series
0 1 1 2 3 5 8 13
C Program:
void main()
{
int n,a,square;
printf("Enter Value of N : ");
scanf("%d",&n);
printf("The Series ");
for(a=1;a<=n;a++)
{
square=a*a;
printf("%d ",square);
}
getch();
}
OUTPUT
Enter Value of N : 7
The Series
1 4 9 16 25 36 49
The C program statements generally execute sequentially in the order in which they are written in the program but in some cases, we may have to change the order of execution of statements.
Decision making statements in a programming language help the programmer to transfer the control from one part to other part of the program. Thus, these decision-making statements facilitate the programmer to determining the flow of control. There are five (5) decision control or selection statements.
The Simple if Statement :
The ‘C’ language uses the keyword if to execute a set of statements or block of statements when the logical condition is true. It has only one option that means it cares only true part.
//Programming Example : 1
//DEMONSTRATION OF SIMPLE IF STATEMENTS.
void main()
{
int num;
printf("Enter The Number : ");
scanf("%d",&num);
if(num>100)
printf("The Given Number is Greater Than 100");
getch();
}
//Programming Example : 2
//WAP TO CHECK GIVEN NUMBER IS POSITIVE, NEGATIVE OR ZERO.
void main()
{
int num;
printf("Enter A Number : ");
scanf("%d",&num);
if(num>0)
printf("Given Number is Positive");
if(num<0)
printf("Given Number is Negative");
if(num==0)
printf("Given Number is Zero");
getch();
}
THE if else STATEMENT :
The if else statement takes care of the true and false conditions. It has two blocks. One block is for if and it is executed when the condition is true. The other block is for else and it is executed when the condition is false.
//Programming Example : 3
//WAP FOR CHECK THE GIVEN NUMBER IS EVEN OR ODD.
void main()
{
int num;
printf("Enter The Number : ");
scanf("%d",&num);
if(num%2==0)
printf("The Number %d is Even",num);
else
printf("The Number %d is Odd",num);
getch();
}
THE NESTED if else STATEMENTS :
In this kind of statement, we check condition within the condition for executing various statements.
//Programming Example : 4
//WAP TO FIND LARGEST AMONG THREE NUMBER.
void main()
{
int a,b,c;
printf("Enter Any Three Number : ");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("A is Largest");
else
printf("C is Largest");
}
else
{
if(b>c)
printf("B is Largest");
else
printf("C is Largest");
}
getch();
}
THE if else if LADDER STATEMENT :
The if else if ladder statement helps user to decide one option from multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the else if ladder is skipped. If none of the conditions is true, then the final else statement will be executed.
Programming Example : 5
//WAP TO CHECK GIVEN YEAR IS LEAP YEAR OR NOT.
void main()
{
int year;
printf("Enter Year: ");
scanf("%d",&year);
if(year%100!=0&&year%4==0)
printf("Given Year is Leap Year");
else if(year%400==0)
printf("Given Year is Leap Year");
else
printf("Given Year is Not Leap Year");
getch();
}
THE switch case STATEMENT :
When one of the many alternatives is to be selected the switch statement allows a program to select one statement for execution out of a set of alternatives and the remaining statements will be skipped.
//Programming Example : 6
//DEMONSTRATION OF SWITCH CASE STATEMENT.
void main()
{
int choice;
printf("Enter the Number (1-7) : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Enter the Valid Choice");
}
getch();
}
A collection of data or information that has a name, called the file. Almost all information stored in a computer must be in a file. File is a collection of numbers, symbols and text placed onto the disk.
To work with file handling in C or wants to save program data permanently in secondary storage, C provides the new data type called FILE.
There are four steps to process a file in 'C' language.
DECLARATION OF FILE TYPE POINTER :
The 'C' communicates with files using a new data type called a FILE.
OPEN THE FILE :
A file has to be opened before read and write operations. Opening of file means load the file in RAM. For opening the file we use the predefined function fopen(). In fopen () function we pass name of the file and its mode of operation as an argument.
OPERATION WITH FILE :
Once the file is opened using fopen() function, then file is ready for operation. The FILE type pointers point to the first character of the file. Basically, three types of operation perform with file.
CLOSING A FILE :
The file is opened by the fopen() should be closed after the work is over, i.e. we need to close the file after reading and writing operations.
//Programming Examples:
//WAP TO WRITE "SURAKU ACADEMY" IN A FILE.
void main()
{
int length,i;
char std[]="SURAKU ACADEMY";
FILE *fp;
fp=fopen("save.txt ","w");
if(fp==NULL)
{
printf(“File not created”);
exit(1);
}
length=strlen(std);
for(i=0;i fputc(std[i],fp);
fclose(fp);
getch();
}
//WAP TO READ CONTENT FROM "save.txt" FILE.
void main()
{
FILE *fp;
char ch;
fp=fopen("save.txt","r");
if(fp==NULL)
{
printf("File Not Found");
exit(1);
}
while(!feof(fp))
{
ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
getch();
}
OUTPUT
SURAKU ACADEMY
In C, variables are used to hold data values during the execution of the program. Every variable when declared occupies certain memory location. For example, integer-type variable takes two bytes of memory, character type one byte and float type four bytes of memory.
A pointer is a special variable that stores memory address rather than value. It is called pointer because it points to a particular location in memory by storing the address of that location.
POINTER DECLARATION :
The declaration of pointer variable is similar to normal variable declaration but preceding with asterisk (*) symbol.
Syntax:
Data_Type *Pointer_Name;
Example:
int *ptr;
Here type of variable ptr is ‘pointer to int’ or (int *), or we can say that base type of ptr is int.
INITIALIZATION OF POINTER :
Providing address (memory location) to the pointer is known as initialization of pointer. The Address of (&) operator is used for initialize the pointer variable.
//Example:
int *ptr;
int num=26;
ptr=&num ;
INDIRECTION OPERATOR :
//Programming Examples:
//DEMONSTRATION OF POINTER VARIABLE-1.
void main()
{
int num=20,*ptr;
ptr=&num ;
printf("Value of NUM: %d ",num);
printf("Address of PTR: %u ",&ptr);
printf("Address of NUM: %u ",&num);
printf("Value of PTR: %u ",ptr);
printf("Value of NUM: %d ",*ptr);
*ptr=26;
printf("Value of NUM: %d ",num);
printf("Value of NUM: %d ",*&num);
getch();
}
OUTPUT
Value of NUM: 20
Address of PTR: 65522
Address of NUM: 65524
Value of PTR: 65524
Value of NUM: 20
Value of NUM: 26
Value of NUM: 26
//DEMONSTRATION OF POINTER VARIABLE-2.
void main()
{
int a=10,b=20, *x,*y;
x=&a;
y=&b;
*y=*x;
*x=*y-6;
a=20+*y-8;
x=a+*y;
printf("%d %d %d %d",a,*x,b,*y);
getch();
}
OUTPUT
32
32
10
10
Click Here to Know More About Pointer in Details.
Thank you so much for taking the time to read my Computer MCQs Section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.