SlideShare une entreprise Scribd logo
1  sur  18
Introduction to ‘C’
Module 2.1
Compiled By :
Abhishek Sinha (MBA-IT, MCA)
Director – Academics
Concept Institute of Technology, Varanasi.
Website: www.conceptvns.org
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.
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).
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.
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);
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.
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”);
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.
/*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
/*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
/*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
/*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
/*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.
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.
/*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
/*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
/*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
END OF MODULE TWO
Send your feedback/queries at
abhisheksinha786@gmail.com

Contenu connexe

Tendances

Tendances (20)

Data Input and Output
Data Input and OutputData Input and Output
Data Input and Output
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
7 functions
7  functions7  functions
7 functions
 

Similaire à Concepts of C [Module 2]

Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringSri Harsha Pamu
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143alish sha
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solutionyogini sharma
 

Similaire à Concepts of C [Module 2] (20)

CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
string , pointer
string , pointerstring , pointer
string , pointer
 
First c program
First c programFirst c program
First c program
 
Chap 2 input output dti2143
Chap 2  input output dti2143Chap 2  input output dti2143
Chap 2 input output dti2143
 
input
inputinput
input
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
C Programming
C ProgrammingC Programming
C Programming
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 

Dernier

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 

Dernier (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Concepts of C [Module 2]

  • 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