It is an attempt to make the students of IT understand the basics of programming in C in a simple and easy way. Send your feedback for rectification/further development.
1. Introduction to ‘C’
Module 2.1
Compiled By :
Abhishek Sinha (MBA-IT, MCA)
Director – Academics
Concept Institute of Technology, Varanasi.
Website: www.conceptvns.org
2. Standard I/O Statements
What is an I/O statement?
Those statements which read data entered through
standard input device and display the values on
standard output device.
3. Types of Standard I/O Statement
Formatted I/O Statement Unformatted I/O Statement
Input Output Input Output
scanf() printf() getchar() putchar()
getche() putch()
getch()
gets() puts()
All these statements are stored in <stdio.h> header file.
Formatted Statements are those statements which facilitate the programmer
to perform I/O operations in all type of data type available in C, whereas in
Unformatted Statements, I/O operations can be performed in fixed format
(date type).
4. Syntaxes and Examples
scanf() : Standard Formatted Input Statement
syntax :
scanf(“<Format String>”,&<variable_name>)
or <Control String>
or <Format Specifier>
example : for single value input
int num;
scanf(“%d”,&num);
example : for multiple value input
int num1, num2;
float num3;
scanf(“%d%f%d”,&num1,&num3,&num2);
The use of scanf() statement provides interactivity and makes the program
user friendly.
5. Syntaxes and Examples
printf() : Standard Formatted Output Statement
syntax :
printf(“<Format String>”,<variable_name>)
or <Control String>
or <Format Specifier>
example : for single value output
int num=10;
printf(“%d”,num);
example : for multiple value output
int num1=10, num2=20;
float num3=30.5;
printf(“%d%f%d”,num1,num3,num2);
example : for message + value output
int num=10;
printf(“The vale of num is = %d”,num);
6. Syntaxes and Examples
getchar(), getche(), getch() : Standard Unformatted
Input Statement
syntax : example :
char ch;
<char_variable> = getchar(); ch = getchar();
<char_variable> = getche(); ch = getche();
<char_variable> = gethc(); ch = getch();
Here getch() & getche() are two functions which takes a character input and
doesn’t require ‘return key’ to be pressed. These functions return the
character that has been most recently typed. The ‘e’ in getche() function
means echoes (display) the character that you typed to the screen. As
against this getch() just returns the character without echoing it on the
screen. getchar() works similarly and echoes the character that you typed
on the screen but even requires enter key following the character you
typed.
7. Syntaxes and Examples
putchar(), putch() : Standard Unformatted
Output Statement
syntax : example :
char ch;
putchar(<char_variable>); putchar(ch);
putch(<char_variable>); putch(ch);
Here putch() & putchar() are two functions which gives a character output.
The difference between the two is that putch() doesnot translate linefeed
characters (‘n’) into carriage-return / linefeed pairs whereas putchar()
does.
Consider a situation: Output: for putch() for putchar()
printf(“RAM”); RAM RAM
putch(‘n’); or putchar(‘n’); SITA SITA
printf(“SITA”);
8. Task To Do – FORMULAE BASED
• WAP to perform addition on two integers.
• WAP to calculate the area of:
• Square
• Rectangle
• Triangle (with given three sides)
• WAP to calculate:
• Simple Interest
• Compound Interest
• WAP to convert temperature from:
• Fahrenheit to Celsius
• Celsius to Fahrenheit
• WAP to sum ‘n’ natural numbers.
9. /*addition of two integers*/
#include<stdio.h>
main()
{
int val1, val2, res;
clrscr();
printf(“Enter two integer values:-”);
scanf(“%d%d”,&v1,&v2);
res = v1+ v2;
printf(“Result = %d”,res);
}
Output:
Enter two integer values:-10
20
Result = 30
/*area of square*/
#include<stdio.h>
main()
{
int s, area;
clrscr();
printf(“Enter side of a square:-”);
scanf(“%d”,&s);
area = s * s;
printf(“Area of Square = %d”,area);
}
Output:
Enter side of a square:-6
Area of Square = 36
10. /*area of rectangle*/
#include<stdio.h>
main()
{
int length, breadth, area;
clrscr();
printf(“Enter length of rectangle:-”);
scanf(“%d%d”,&length);
printf(“Enter breadth of rectangle:-”);
scanf(“%d”,&breadth);
area = length * breadth;
printf(“Area of Rectangle = %d”,area);
}
/*area of triangle*/
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float s,area;
clrscr();
printf(“Enter sides of triangle:-”);
scanf(“%d%d%d”,&a,&b,&c);
s = (a+b+c)/2.0;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf(“Area of Triangle = %f”,area);
}
Output:
Enter length of rectangle :-10
Enter breadth of rectangle :- 20
Area of Rectangle = 200
Output:
Enter sides of a triangle:-6
7
8
Area of Triangle = 20.333162
11. /*simple interest*/
#include<stdio.h>
main()
{
float p,r,t,si;
clrscr();
printf(“Enter P, R and T:-”);
scanf(“%f%f%f”,&p,&r,&t);
si = (p*r*t)/100;
printf(“Simple Interest = %f”,si);
}
/*compound interest*/
#include<stdio.h>
main()
{
float p,r,t,n,a,ci;
clrscr();
printf(“Enter P,R,Tand N:-”);
scanf(“%f%f%f%f”,&p,&r,&t,&n);
r = r/100;
a = p * pow((1+(r/n),(t*n));
printf(“Amount = %f”,a);
printf(“Compound Interest = %f”,ci);
}
Output:
Enter P, R and T:-10000
10.5
5
Simple Interest = 5250.000000
Output:
Enter P, R, T and N:-1000
5
5
2
Amount = 1280.084595
Simple Interest = 280.084595
12. /*fahrenheit to celsius conversion*/
#include<stdio.h>
main()
{
float fh,cl;
clrscr();
printf(“Enter temperature in F:-”);
scanf(“%f”,&fh);
c = (f-32)*5/9.0;
printf(“Celsius Equivalent = %f”,cl);
}
/*celsius to fahrenheit conversion*/
#include<stdio.h>
main()
{
float fh,cl;
clrscr();
printf(“Enter temperature in C:-”);
scanf(“%f”,&cl);
fh = (1.8*c + 32);
printf(“Fahrenheit Equivalent = %f”,fh);
}
Output:
Enter temperature in F:-212
Celsius Equivalent = 100.000000
Output:
Enter temperature in C:-100
Fahrenheit Equivalent = 212.000000
13. /*sum of ‘n’ natural numbers*/
#include<stdio.h>
main()
{
int n, sum;
clrscr();
printf(“Enter ‘n’:-”);
scanf(“%d”,&n);
sum = n*(n+1)/2;
printf(“Sum of ‘n’ natural term = %d”,sum);
}
Output:
Enter ‘n’:-11
Sum of ‘n’ natural term = 66
Till now what we have
programmed were simple
formulae based questions.
Now, we will move towards the
logical based questions.
Means there will be no more
fixed patterns for any
solutions, procedure may
differ from programmer to
programmer as per their
own logical reasoning's.
14. Task To Do – LOGIC BASED
• WAP to find the greater among two numbers.
• WAP to check whether the number is odd or even.
• WAP to check whether a given year is leap or not.
• WAP to add the first and last digit of any four digit
number.
• WAP to interchange two integer values.
15. /*greater among two numbers*/
#include<stdio.h>
main()
{
int v1,v2;
clrscr();
printf(“Enter two numbers:-”);
scanf(“%d%d”,&v1,&v2);
printf(“Greater value is :- ”);
printf((v1>v2 ? “V1” : “V2”));
}
/*check for number is odd or even*/
#include<stdio.h>
main()
{
int no;
clrscr();
printf(“Enter any number:-”);
scanf(“%d”,&no);
printf((no%2==0 ? “Even” : “Odd”));
}
Output:
Enter two values:-10 20
Greater value is:- V2
Enter two values:-25 15
Greater value is:- V1
Output:
Enter any number:-16
Even
Enter any number:-15
Odd
16. /*check for leap year*/
#include<stdio.h>
main()
{
int yr;
clrscr();
printf(“Enter the value of an year:-”);
scanf(“%d”,&yr);
printf( (y%100!=0 && y%4==0) ||
(y%400==0) ? “Leap Year” : “Not a
Leap Year”));
}
zzzzzzzzzzzzzzzzzzzzzz
/*sum of first and last digit of any four
digit number*/
#include<stdio.h>
main()
{
int no,fd,ld,sum;
clrscr();
printf(“Enter any four digit number:-”);
scanf(“%d”,&no);
fd = no/1000;
ld = no%10;
sum = fd + ld;
Printf(“Sum = %d”, sum);
}
6.8
Output:
Enter the value of an year:-2004
Leap Year
Enter the value of an year:-2000
Leap Year
Enter the value of an year:-1900
Not a Leap Year
Output:
Enter any four digit number:-4578
Sum = 12
17. /*swapping of two integer values*/
#include<stdio.h>
main()
{
int v1,v2,temp;
clrscr();
printf(“Enter two values for v1 and v2:-”);
scanf(“%d%d”,&v1,&v2);
printf(“Values before swapping: V1 = %d,V2
= %d”, v1,v2);
temp = v1;
v1 = v2;
v2 = temp;
printf(“Values after swapping: V1 = %d,V2
= %d”, v1,v2);
}
Output:
Enter two values for v1 and v2:-10 20
Values before swapping: V1 = 10, V2 = 20
Values after swapping: V1 = 20, V2 = 10
This logic using third variable
temp can be simplified without
using it.
Logic 1: using +,-
v1=v1+v2;
v2=v1-v2;
v1=v1-v2;
Logic 2: using *,/
v1=v1*v2;
v2=v1/v2;
v1=v1/v2;
Logic 2: using ^
v1=v1^v2;
v2=v1^v2;
v1=v1^v2;
V1 V2
10 20
30 20
30 10
20 10
V1 V2
10 20
200 20
200 10
20 10
V1 V2
10 20
30 20
30 10
20 10
18. END OF MODULE TWO
Send your feedback/queries at
abhisheksinha786@gmail.com