Quick Starter Questions for Understanding Basics of C
Concepts Enfolded:
Input & Output
Variables (Local and Global)
Decision Making (if-else statements)
Loops (for loop)
Functions
String Operations
Arrays
Recursion
1) Write a program that prints “Hello World”.
Description:
In C, all lines that start with pound (#) signs are called directives.
#include<stdio.h> tells the compiler to include the header file for Standard Input. Output file which contains declarations of all the standard input/output library functions.
int main() is used to declare a function named "main" which returns data of integer type.
printf() function is used to print a character stream of data on the stdout console.
Everything within " " is displayed to the output device.
Source code:
2) Write a program that takes the input of two numbers and stores the value in another variable. (Local and Global).
Description:
In C, programming language, variables defined within some function are known as Local Variables and
Variables which are defined outside of the function block and are accessible to the entire program are known as Global Variables.
Source code:
3) Write a program that takes input from the users to fill the following form:
Name: __________
Roll no.: _________
Age: ______
Blood group: _____
Description:
Strings in C are actually arrays of characters. The[50] tells the size of the array.
char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and
stores it into the string pointed to by str.
sizeof is a much-used operator in the C. It is a compile time unary operator which can be used to compute the size of its operand.
The C library function int scanf(const char *format, ...) reads formatted input from stdin.
Source code:
4) Write a program to check a given number is odd or even.
Description:
The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.
Step by step descriptive logic to check whether a number is even or odd.
Input a number from the user.
Store it in some variable say num.
Check if number modulo division is equal to 0 or not i.e. if(num % 2 == 0) then the
number is even otherwise odd.
Source code:
5) Write a program to find the sum of n natural numbers.
Description:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax:
for ( init; condition; increment ) {
statement(s);
}
The init step is executed first, and only once.
Next, the condition is evaluated. If it is true, the body of the loop is executed.
After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement.
The condition is now evaluated again.
Source code:
6) Write a program to print all the elements of an array.
Source code:
7) Write a program to find the largest and smallest element in an array.
Description:
The limits.h header determines various properties of the various variable types.
The macros defined in this header, limits the values of various variable types like char, int, and long.
Source code:
8) Make a function that will return the sum of two numbers.
Description:
In C, we can divide a large program into the basic building blocks known as function.
The function contains the set of programming statements enclosed by {}.
A function can be called multiple times to provide reusability and modularity to the C program.
In this program, the function will return the sum of two Numbers.
Source code:
9) Make a function in C to find the square of any number.
Description:
The program asks the user to enter the value. After getting the value from the user it will call a function square() to calculate the square of the entered number.
The function square() is using an arithmetic multiplication operator to calculate the square of the input number.
Source code:
10) Write a program to check whether a given number is prime or not.
Description:
The program takes the value of n (entered by the user) and passes this value while calling isPrime() function.
This function checks whether the passed value is prime or not, if the value is prime then it returns true, else it returns false.
Source code:
11) Write a program to print the Fibonacci series using both function and Recursion.
Description:
In case of the Fibonacci series, the next number is the sum of the previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. The first two numbers of the Fibonacci series are 0 and 1.
There are two ways to write the Fibonacci series program:
Fibonacci Series using function
Fibonacci Series using recursion
Source code:
12) Write a program to find the factorial of a number using recursion.
Description:
It is the process of repeating items in a self-similar way.
In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
The C programming language supports recursion, i.e., a function to call itself.
Source code:
13) Write a program to find the length of the string and print the string.
Description:
Strings in C are actually arrays of characters.
%s is the format specifier for the strings.
strlen() function calculates the length of a given string.The strlen() function is defined in string.h header file.
Source code:
Congratulations!🎉🎊
You have successfully solved all the problems mentioned above. Now, you can go to the Quiz Section to test your knowledge of the basics of C programming.