Header Ads Widget

Ticker

6/recent/ticker-posts

Warmup programs in C language

 For start coding in C- language first of all we have to include Header files ( All the definitions and declaration of keywords are in the header files so it is essential to include it ). Generally we include to header files stdio.h and conio.h and other use like for mathematical operation we use math.h.  In some compilers if we does not use it then may be their will be no errors but these are essential to add . So by an example we will learn how we can include these header file and how can we start coding.

Before starting programming we should know about some keywords :-

int :- It is used for integer variable .

printf() : -  it is used for printing anything on the console (screen) . 

scanf() :- it is used for taking input from the user. 

1) Write a C program to print "Hello world " .

Code:-

// including header file
#include<stdio.h>

// Driver code
int main()
{
printf(" Hello world! ");
return 0;
}

Output:-


Hello world!

2) WAP (write a program ) to take input two numbers and print it on the console(screen).

Code:-
for talking input we will use scanf() function which is inbuilt function and given in the C-library.

#include<stdio.h>
int main()
{
// declaration of variable
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Numbers of %d and %d",a,b);
return 0;
}

Output:-
if we give the input 12 and 34 then output will be 12 and 34


Enter two numbers
12 34
numbers are 12 and 34


Some students here doubt that how can we know how many variables we have to declare because we don't know how many variables we will use. So don't worry about it we will declare the variable when we need any variable.

3) WAP to add two numbers. numbers are taken from the user.

Code:-

#include<stdio.h>
int main()
{
// declaration of variable
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Addition of %d and %d is %d",a,b,a+b);
return 0;
}

Output:-

Enter two numbers
2 5
Addition of 2 and 5 is 7

You can make the program for the subtraction , multiplication and division like this. By changing just sign of operator.

Some important points:-
  1. Semicolon are the terminator in the C-programming. And each statement are must be terminated by semicolon.
  2. Comments in the programming are very useful for understanding the code .when any other person is reading your code then that time comments are very useful for understanding your code.
  3. In C there are two methods for writing the comment . first by using '// ' (without quotes) but this is single line comment . And second method is  /*.................*/ and by using this method we can write multiline comments.
Example:-
#include<stdio.h>
int main()
{
// this is single line comment
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
/* this is multiple
line comment*/
printf("Addition of %d and %d is %d",a,b,a+b);
return 0;
}

Here I will give you some programs which you can practice for better understanding. So click on the links for the questions and if you want solution of the programs then you can search it .

<<Pre- C-- Tokens                                     Next-Data type in C>>

 





Here are some recommended posts for you

Recommended Post:

Hackerearth Problems:-

Hackerrank Problems:-
Data structure:-

Key points:-

 MCQs:-

Post a Comment

0 Comments