Concepts of C [Module 2]

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

Transcript of Concepts of C [Module 2]

Page 1: Concepts of C [Module 2]

Introduction to ‘C’

Module 2.1

Compiled By :

Abhishek Sinha (MBA-IT, MCA)Director – Academics

Concept Institute of Technology, Varanasi.Website: www.conceptvns.org

Page 2: Concepts of C [Module 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.

Page 3: Concepts of C [Module 2]

Types of Standard I/O Statement

Formatted I/O Statement Unformatted I/O Statement

Input Output InputOutput

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).

Page 4: Concepts of C [Module 2]

Syntaxes and Examples

scanf() : Standard Formatted Input Statementsyntax : scanf(“<Format String>”,&<variable_name>)

or <Control String> or <Format Specifier>

example : for single value inputint num;scanf(“%d”,&num);

example : for multiple value inputint num1, num2;float num3;scanf(“%d%f%d”,&num1,&num3,&num2);

The use of scanf() statement provides interactivity and makes the program user friendly.

Page 5: Concepts of C [Module 2]

Syntaxes and Examples

printf() : Standard Formatted Output Statementsyntax : printf(“<Format String>”,<variable_name>)

or <Control String> or <Format Specifier>

example : for single value outputint num=10;printf(“%d”,num);

example : for multiple value outputint num1=10, num2=20;float num3=30.5;printf(“%d%f%d”,num1,num3,num2);

example : for message + value outputint num=10;printf(“The vale of num is = %d”,num);

Page 6: Concepts of C [Module 2]

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.

Page 7: Concepts of C [Module 2]

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 RAMputch(‘\n’); or putchar(‘\n’); SITA SITAprintf(“SITA”);

Page 8: Concepts of C [Module 2]

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.

Page 9: Concepts of C [Module 2]

/*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:-1020Result = 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:-6Area of Square = 36

Page 10: Concepts of C [Module 2]

/*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 :-10Enter breadth of rectangle :- 20Area of Rectangle = 200

Output:

Enter sides of a triangle:-678Area of Triangle = 20.333162

Page 11: Concepts of C [Module 2]

/*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:-1000010.55Simple Interest = 5250.000000

Output:Enter P, R, T and N:-1000552Amount = 1280.084595Simple Interest = 280.084595

Page 12: Concepts of C [Module 2]

/*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:-212Celsius Equivalent = 100.000000

Output:

Enter temperature in C:-100Fahrenheit Equivalent = 212.000000

Page 13: Concepts of C [Module 2]

/*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’:-11Sum 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.

Page 14: Concepts of C [Module 2]

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.

Page 15: Concepts of C [Module 2]

/*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 20Greater value is:- V2Enter two values:-25 15Greater value is:- V1

Output:

Enter any number:-16EvenEnter any number:-15Odd

Page 16: Concepts of C [Module 2]

/*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:-2004Leap YearEnter the value of an year:-2000Leap YearEnter the value of an year:-1900Not a Leap Year

Output:

Enter any four digit number:-4578Sum = 12

Page 17: Concepts of C [Module 2]

/*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 20Values before swapping: V1 = 10, V2 = 20Values 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

Page 18: Concepts of C [Module 2]

END OF MODULE TWO

Send your feedback/queries [email protected]