You are on page 1of 23

Subject Programming Fundamental.

Book:- C – Language or Turbo C


Author :- Robert Lafore.
1) Structure Programming . C
2) Objected Oriented Programming . C++
3) Advance Objected Oriented Programming . Java C++

Structure Of C Programming
C-programming start with a keyword main().Keyword mean language own words. main()
contains a block latter of codes / statement or instruction. Each Statement terminate
with semi-colon (;).
Data
Symbol where data can be performed.
i. Integer data:- A number with no decimal point, e.g
2,3,4,5,6………..;
ii. Float data :- Number with decimal point, e.g
2.2,3.4,9.9,……….:
iii. Character data:- Character data consist of single character . e.g
‘a’, ‘f’, ‘3’, ‘+’, ‘_’…………;
iv. Stream data:- It is combination of character. e.g
MALIK AsGHaR, Abc,123,………….;

Output Statement
It is use to display output statement on screen and it is denoted by printf()

1) Display string :-
Using format specifiers we can print string constant as well as numbers.
General Syntax:-
main()
{
printf(“String”);
}
p1. Display your name .
main()
{
printf("Malik Asghar");
}
p2. Display :-
Welcome
To
Gomal University.
main()
{
printf("Welcome to Gomal University");
2) Display Integer:-
%d specifiers is used for show input or output of integer data.
General Syntax:-
main()
{
printf(“%d”, integer value);
}
p3. Display integer 5.
main()
{
printf(“%d”, 5);
}
p4. Display two integer 4 and 8 .
main()
{
printf("%d%d",4,8);
}
3) Display Float :-
%f specifeir use for display float number .
General syntax :
main()
{
Printf(“%f”,float number );
}
p5. Display 3.4
main()
{
printf("%f",4.4);
}
p6. Display following output :
4.5 , 3.2
2,7
1
main()
{
printf("%f,%f\n%d %d\n%d",4.5,3.2,2,7,1);
}
4) Display character data :
%c is specifier is used to display character data.
General syntax:
main()
{
Printf(“%c”,’char value’);
}
p7. Display a character :-
main()
{
printf("%c",'*');
}
p8. Display following output :
***
**
*
main()
{
printf("%c%c%c\n%c%c\n%c",'*','*','*','*','*','*');
}
p9. Display following output:
Square of 3 = 9.
main()
{
printf("Square of %d %c %d",3,'=',9);
}
p10. Display following output:
A=3;
B=4.5;
main()
{
printf("A %c %d\nB %c %f",'=',3,'=',4.5);
}

Variable
A variable is a set space in computer memory set aside for a certain kind of data and given
name for easily reference . variable are used so that the same space in memory can bolt
different value at different times .
Rules for variable name :
 First latter must be alphabetic.
 No space is allowed.
 No special char (+,-,=,…….) can be used with variable name expect underscore (_).
 No reserved word can be uses a variable name . i.e, for, main, printf, scanf etc.

Types Of Variable :-
1) Integer variable :-
Here we can store an integer in variable .
General syntax:
int variable name,
variable ‘int’ is a reserved wors and variable name is a user define word.
int variable has two bytes.
Its range is -32768 to 32767.
General syntax:
Long int V1.
It has four bytes .
Its rage is -2147483648 to 2147483647.
p11. Store 150 in a variable and display its value .
main()
{
int a;
a=150;
printf("%d",a);
}
p12. Display Square of 4 = 16.
main()
{
int a,b;
a=4;
b=16;
printf("Square of %d = %d",a,b);
}
2) Float variable :-
It is use to store decimal point numbers.
General synatax :-
float variable ;
it has four bytes in memory.
Maximam number = 1038
Minimum number = 10-38
General synatax :-
it has eight bytes in memory.
Maximam number = 10308
Minimum number = 10-308
p13. Display following output “a=4.5 and b=3.2”.
main()
{
float a,b;
a= 4.5;
b= 3.2;
printf("a = %f and b = %f",a,b);
}
3) Character variable :
It can hold single bytes.
Only single character can be show .
p14. Display ; “ ch1 = + and ch2 =S.
main()
{
char ch1,ch2;
ch1='+';
ch2='S';
printf("ch1 = %c and ch2 = %c",ch1,ch2);
}

INPUT FUNCTIONS
scanf() function is a input function through which we can enter data with the help of
keyboard .
General syntax:-
Scanf(“specifier”,&var);
p15. Read and display an integers:-
main()
{int n;
printf("Please Enter an Integer:");
scanf("%d",&n);
printf("n = %d",n);
}
p16. Read and display an float number:-
main()
{
int s;
printf("Please Enter an float:");
scanf("%f",&s);
printf("s = %f",s);
}
p17. Read and display character data:-
main()
{
int z;
printf("Please Enter an char :");
scanf("%c",&z);
printf("z = %c",z);
}
p18. Display following output:
Square of 4 = 16.
main()
{
int n,sq;
printf("Enter Number");
scanf("%d",&n);
printf("Enter Square of %d :",n);
scanf("%d",&sq);
printf("Square of %d = %d",n,sq);
}
Operator
Operators are words or symbol that cause program to do something to variable there
are many different types of operators.
A. Arithmetic Operator .
B. Arithmetic Assignment Operator .
C. Increment /Decrement Operator.
D. Relational Operator
E. Logical Operator.
F. Ternary Operator.

A) Arithmetic Operator :-
Arithmetic operator we use the multiplication ( * ) to multiply two number together . C
use the four arithmetic operator that are common in most programming language and
one, the reminder operator which is not common .
i. + Operator.
ii. - Operator.
iii. * Operator.
iv. | Operator.
v. % (mod) Remainder Operator.

i. Addition Operator:-
it is used to add two are more operands. e.g
c=a+b;
it means Add ‘a’ with ‘b’ and assign value in c.
ii. Subtraction Operator:-
iii. it is used for subtract two are more operands. e.g
c=a-b;
it means Subtarct ‘b’ from ‘a’ and store result in c.
iv. Multiplication operator:-
it is used to add two are more operands. e.g
c=a+b;
it means Add ‘a’ with ‘b’ and assign value in c.
v. Division operator:-
it is used for division. There are two types of division.
float divisionand Integer division.
float division:-
e.g float a=9;
float b=2;
c=a|b; the result of ‘a and b’ is ‘4.5’ in this type of division is called float division.
integer division:-
int a=9;
int b=2;
c=a|b; the result of ‘a and b’ is ‘4’ in integer division.
Or
int a=2;
int b=9;
c=a|b; the result of ‘a and b’ is ‘0’ in integer division.
vi. Mod or Reminder operator :-
It is used to find the reiminder e.g
int a= 9;
int b=2;
c=a%b; in this situation the result of ‘a and b’ is ‘1’.
p19. Display sum of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a+b;
printf("%d + %d = %d",a,b,c);
}
p20. Display sum of three given numbers:-
main()
{
int a,b,c,d;
printf("Enter Three Numbers:");
scanf("%d %d %d",&a,&b,&c);d=a+b+c;
printf("%d+%d+%d = %d",a,b,c,d);
}
p21. Display subtraction of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a-b;
printf("%d-%d = %d",a,b,c);
}
p22. Display product of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a*b;
printf("%d*%d = %d",a,b,c);
}
p23. Display division of two numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a/b;
printf("%d/%d = %d",a,b,c);
}
p24. Display reminder of two given numbers:-
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);
c=a%b;
printf("%d mod %d = %d",a,b,c);
}
p25. Display square of given numbers:-
main()
{
int n,sq;
printf("Enter Number:");
scanf("%d",&n);
sq=n*n;
printf("%d * %d = %d",n,n,sq);
}
p26. Display cube of given numbers:-
main()
{
int n,sq;
printf("Enter Number:");
scanf("%d",&n);
p27. sq=n*n*n;
printf("%d * %d * %d= %d",n,n,n,sq);
}
p28. Covert given celisuc to forhrenhit :-
p29. Calculate following equation:-
c = a2+b2 where a =3 and b =4.
main()
{
int a,b,c;
printf("Enter Two Number:");
scanf("%d %d",&a,&b);c=a*a+b*b;
printf("c=%d * %d + %d * %d = %d",a,a,b,b,c);
}
B. Arithmetic Assignment Operators:-
i. + operators:-
Add and assignment operator its add right operand to left operands .
c +=a is equivalent to c = c+a. e.g
suppose : a=a+2;
alternate : a+=2;
ii. - Assignment operator:-
Subtract and assignment operator , subtract right operands fro the left operands and assign result to left
operands . c-=a; is equivalent to c=c-a; e.g
suppose : a=a-2;
alternate : a-=2;
iii. * Assignment operators :-
Multiply and assignment operator , its multiplies right operands with left operand
and assign the result in left operands . c*=a; is equivalent to c=c*a;. e.g
suppose : a=a*2;
alternate : a*=2;
iv. | Assignment operators :-
Divide and assignment operator, it divides left operand with the right operands and
assign result to left operands c|=a; is equivalent to c = c|a. e.g
suppose : a=a|b;
alternate : a|=b;
v. % (mod) Assignment operator :-
Modulus And assignment operators , it takes modulus using two operands and
assign the result to left operands. c%=a; is equivalent to c%=a; . e.g
suppose : a=a%b;
alternate : a%=b;.

p30. Display percentage of given number :-


main()
{
float tm,obm,per;
printf("Enter Obtained Marks:");
scanf("%f",&obm);
printf("Enter Total Marks:");
scanf("%f",&tm);
per=(obm / tm)*100;
printf("%f/%f*100=%f",obm,tm,per);
printf("\nYour Percentage is %f ",per);
}
C. Increment / Decrement Operator :-
a) Increment Operator:-
Increment operator increase value of variable by 1 .
It is denoted by ++ .
There are two types of Increment operator .
There are two types of Increment operator .
Prefix and postfix
Prefix:
Prefix place ++ before variable .e.g
++a;
a =4;
b=++a; Result is 5.
Postfix:
Prefix place ++ after variable .e.g
a++;
a =4;
b=++a; Result : a=5 and b=4.
p31. Display output of the following program:-
main()
{
int a,b;
a=4;
b=++a;
printf("%d , %d",a,b);
}
p32. Display outputnof the following program:-
main()
{
int a,b;
a=5;
b=a++;
printf("%d , %d",a,b);
getch();
}
b) Decrement operator:-
Increment operator decrease value of variable by 1 .
It is denoted by -- .
There are two types of decrement operator .
There are two types of decrement operator .
Prefix and postfix
Prefix:
place (--)operator before variable name .e.g
Ex1 : a= 4;
--a; result is 3
Ex2:a =4;
b=--a; Result b=3 and a =4.
Postfix:
place (--) after variable name .e.g
Ex1 : a= 4;
a--; result is 3
Ex2:a =4;
b--=a; Result b=4 and a =3.
p33. What could be the output of the following programs:-
main()
{
int a,b;
a=7;
--a;
a--;
b=--a;
printf("a=%d , b=%d",a,b);
b=a--;
printf("\na=%d , b=%d",a,b);
}
Out put :
a=4,b=4
a=3,b=4.
D. Relational operator:-
They show relationship b/t two variable or operands .
They become makes condition because redult of those statement return true
(1) or false (0).
If the condition is true result will be 1.
And otherwise condition is false result will be 0.
Relation operator are following bellow:
Relational Opretors
Human Language Mathematics C-language
1 Less then. < <
2 Greater then. > >
3 Less or Equal <=
4 Greater or Equal. >=
5 Equal. = ==
6 Not Equal. <> !=
p34. What could be the output of the following program:-
main()
{
int a,b,c;
a=3;
b=4;
c=a<b;
printf("%d\n",c);
c=a>=b;
printf("%d\n",c);
a=4;
b=4;
c=a<=b;
printf("%d\n",c);
c=a==b;
printf("%d\n",c);
}
main()
{ int a,b,c;
a=3;
b=4;
c=a<b;
printf("%d\n",c);
c=a>=b;
printf("%d\n",c);
a=4;
b=4;
c=a<=b;
printf("%d\n",c);
c=a==b;
printf("%d\n",c);
}
Result is :
1 -- True.
0 -- False.
1 -- True.
1 -- True.
E. Logical Operator:-
Logical operator combine two or more then two condition .
Its result is also true or false.
Types of Logical operators :-
There are three types of logical operator :-
( AND , OR ,NOT )

AND:-
It is denoted by &&.
The following diagram cleared that “ Result will be true (1) when all
conditions become true“.

Condition1 Operator Condiyion2 Result


0 && 0 0
1 && 0 0
0 && 1 0
1 && 1 1
OR :-
It is denoted by || .
The following diagram showa that “ Result will be true when any one
condition become true “.
Condition1 Operator Condiyion2 Result
0 || 0 0
1 || 0 1
0 || 1 1
1 || 1 1

NOT:-
It is denoted by ! .
It operator convert true to false and false to true .
Ex : a =3; and b =4;
c = !(a<b); Result: c =0;
Here the value of ‘a’ is less then the value of ‘b’ that’s result is true ,, but use
to the NOT operator that’s convert result to true to false and false to true.
Therefore the result of c is false (0).

p35. What could be the result output of the following program:


main()
{
int a,b,c;
a=3;
b=4;
c=a<b&&a==3; ------- Using AND operator.
printf("%d\n",c);
c=a!=3|| a==b; ------ Using OR operator.
printf("%d\n",c);
c=!(a==b); ------ Using NOT operator.
printf("%d\n",c);
}
Output :
1
0
1

p36. Display next and pervious number of given number:-


main()
{
int n,a,b;
printf("ENTER A NUMBER\n");
scanf("%d",&n);
a=n+1;
b=n-1;
printf("number = %d \nNext=%d \nprevious =%d",n,a,b);
}
F. Ternary operator or Conditional operator:-
Gernal syntax:
condition ? expression1 :expression2.

p37. What could be the output of the following program:-


main()
{
int n;
n=2;
n>4?printf("YES"):printf("NO");
}
Output : NO.
p38. Display “Yes” if given number is positive otherwise “No” .
(using ternary operator)
main()
{
int n;
scanf("%d",&n);
n>0?printf("YES"):printf("NO");
}
p39. Display “Yes” if given number in range of 10 to 20 otherwise “No” .
(using ternary operator)
main()
{
int n;
scanf("%d",&n);
n>10&&n<20?printf("YES"):printf("NO");
}
p40. Display “Yes” if given number not in range of 10 to 20 otherwise “No” .
(using ternary operator)
main()
{
int n;
scanf("%d",&n);
n<10||n>20?printf("YES"):printf("NO");
}

p41. Display “Yes” if given number is Even otherwise “No” .


(using ternary operator)
main()
{
int n;
scanf("%d",&n);
n%2==0?printf("YES"):printf("NO");
}
p42. Display Largest number from two given numbers:-
(using ternary operator)
main()
{
int x,y;
scanf("%d %d",&x,&y);
x>y?printf("%d",x):printf("%d",y);
}

getch () And getche()


getch() getche()
It is input function for a character. It is input function for a character.
No need to prees ‘Enter’ key at insertion No need to prees ‘Enter’ key at insertion
time .. time ..
Character will not be display at insertion Here ‘e’ means echo display. i.e,
time. character will be display at insertion
time.

p43. Use getch():-


main()
{
char ch;
ch=getch();
printf("\n%c",ch);
}
p44. Display “Yes” if given character is ‘y’ otherwise “No” .
(using ternary operator)
main()
{
char ch;
printf("Enter a char");
ch=getch();
ch=='y'?printf("\nYes"):printf("\nNo");
}
p45. Display ‘Yes’ if given is character is vowel :-
(using ternary operator)
main()
{
char ch;
printf("Enter a Character");
ch=getche();
ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'?printf("\nYES"):printf("\nNO")
;
}

Decision Statement
In C-language there are three major descision statement.
a. if – statement.
b. if – else statement.
c. Switch statement.
a. if – statement:-
General syntax:-
if(condition)
statement; Block or body of ‘ if ’.
when condition is true then the compiler will show execute “ block -of- of ”,
otherwise compiler will ignore “ block - of – if ”.
If “ block - of – if ” has more then one then braces are necessary .
General syntax:-
if (condition)
{
Statement 1; “ block - of – if ”
Statement 2;
}
Flow chart:-
if

Condition
Block of if Next statement
after block

p46. Display ‘Yes’ if given number is positive:-


main()
{
int n;
scanf("%d",&n);
if(n>0)
printf("\nYes");
printf("\nGood Bye");
}
p47. Display ‘Yes’ if given number is in range 10 to 20.
main()
{
int n;
scanf("%d",&n);
if(n>10&&n<20)
printf("\nYes");
printf("\nGood Bye");
}
p48. Display ‘Yes’ if given number is not in range 10 to 20.
main()
{
int n;
scanf("%d",&n);
if(n<10||n>20)
printf("\nYes");
printf("\nGood Bye");
}
p49. Display if given number is ‘ even’:-
main()
{
int n;
scanf("%d",&n);
if(2%n==0)
printf("\nYES");
printf("\nGood Bye");
}

000000000000000000000000000000000000000000000000000000000000000000

You might also like