Schoolans.com

C Programming

C programming:

–> is a general-purpose, block structured, procedural computer programming language

–> Developed in 1972 A.D. by Dennis Ritchie at the Bell Telephone Laboratories.

–> C language is the mother language of all other programming language

–> All other languages are developed from the base of C language like C++, Java, C# etc

–> UNiX operating system is also developed by using the C language.

–> It is  3rd generation language(3GL)

–> Both High Level and Low Level Language (Mid-Level Language)

–> It uses compiler.

C ProgrammingQBASIC
i. Uses Compileri. Uses Interpreter
ii. Mid-level languageii. High Level Language
iii. Mother of all languagesiii. Basic Language
iv. Advanced programs/software’s can be developediv. Advanced programs cannot be developed
Differences Between C-Programming and QBASIC

a. Character Sets in C language

  • C language has 98 character sets.
CharactersNameCount
A-Z, a-zLetters52
0-9Digits10
, ; : ? ‘ ” ! @ $ # % ~ > < = & _ – + * ) ( ] [ {} / \ | .Special Characters31
Blank Space, horizontal tab carriage return, new line, from feedWhite spaces5
Total 98
Character sets in C language

b. Keywords :

Keywords are the set of words in C language meaning and actions of which remains fixed throughout the language. There are 32 keywords.

autobreakcasecharconstcontinuedefaultdo
doubleelseenumexternfloatforgotoif
intlongregisterreturnshortsignedsifzeofstatic
structswitchtypedefunionunsignedvoidvolatilewhile
Keywords in C Language

c. Data Types

 There are 4 basic data types

i. Primary or Fundamental Data types

ii. User Defined Data Type

iii. Derived Data Type

iv. Empty Data Type

i. Primary or Fundamental Data Types

            The primary data types are built in and with the help of that data type we can create all other data types. Primary or fundamental data Types are again divided into three types.

Data TypeMemory Occupies (Bytes)Scan FormatCode/Short form
integer2%dint
Floating Point4%ffloat
Double8%lfdouble
Signed Character1%cchar
Fundamental Data Types in C

Operators:

         Operators in C is similar as the operators in QBASIC. Operators are the special symbols which perform certain calculation to the operands. For eg; a + b ; here a and b are operands and  “+” is the operator.

a. Arithmetic Operator.

OperatorsMeaning
+Addition
Subtraction
*Multiplication
/Division
%  (same as MOD in QBASIC)Modulo Division
( )Brackets
Arithmetic Operator

b. Relational/Comparison Operator

OperatorsMeaning
Less than
< =is less than or equal to
Is greater than
> =Is greater than or equal to
= =is equal to
!=  (QBASIC < > is not equal to )is not equal to
Relational/Comparison Operator

c. Logical Operator:

QBASICC-OperatorsC-Meaning
AND&&  (ampersand)Logical And
OR||Logical Or
NOT|Logical Not
Logical Operator

d. Assignment Operator.

          = is the assignment operator.

A= 5;  we assign the value 5 to variable A .

Without Short Hand OperatorsWith Short Hand Operators/ Supported by C
a =  a + 1a+=1
a = a – 1a-=1
a = a * (n+1)a*=n+1
a = a / (n+1)a/=n+1
a = a % ba%=b
Assignment Operator

e. Increment & Decrement Operators:

Without  Increment OperatorWith Increment Operators
a=a+1a++, ++a
a= a-1a–, –a
Increment & Decrement Operator

f. Ternary Operator or Conditional Operator in C.

          We use ? as Ternary Operator.

Syntax:

          (Condition) ? for true body: for false body

eg;

a=10, b= 20, c= 30

(a>5) ? b = b +5, c = c-5

After execution;

b= 25

1. display

Syntax :

    printf (“Message/scan format”, list of variables);

    eg;

void main(){

          int a,b,c;

          c = a + b;

          Printf  (“The sum of  %d and %d is = %d”,a,b,c);

}

2. To take input from keyboard

Sytax:

   scanf  (“scan format”,&variable, &variable);

  eg;

 void main(){

          int a,b,c;

          printf(“Enter first number”);

          scanf (“%d”,&a);

          printf(“Enter the second number”);

          scanf (“%d”,&b);

          c = a + b;

          printf(“The sum of  %d and %d is = %d”,a,b,c);

}

3. Variable Declaration.

    eg;

      int a;

       float b;

         double  d, e, f=50.90;

Conditional Statement

QBASICC-Programming
1. If condition then       *** body of if ***        It is shown if  the condition is true    End If  1. If (condition) {              *** body of if ***              It is shown if  the condition is true   }         
2. If condition then              *** body of if ***               It is shown if  the condition is true     else         *** body of  else ***       It is shown if  the condition is false      end if2. If (condition) {       *** body of if ***         It is shown if  the condition is true } else {      *** body of  else ***       It is shown if  the condition is false   }
3. Multiple If/Compound If3. Multiple If/Compound If
If condition then     *** body of if ***      It is shown if  the condition of if  is true else if       *** body of if ***      It is shown if  the condition of  else if  is     true else if     *** body of if ***      It is shown if  the condition of  else if  is     true else       *** body of  else ***       It is shown if  the condition is false end if  If (condition) {   } else if (condition) {     } else if (condition) {   } else if (condition) {     } else {     }

Q1.

1-5= Child

6-12= Baby

13-19= Teenager

20-49= Adult

50-69= Old

70-84=Jestha Nagarik

85 Above= too old

ANs :

 void main(){

          int a;

          printf (“Enter your age”);

          scanf (“%d”,&a);

          if (a>0 && a<=5 ) {

              printf (“YOu are Baby”);

          }

          else if (a>5 && a<=12)

          {

                   printf (“you are Child”);

          }

          else if (a>12 && a<=19)

          {

                   printf (“you are Teenager”);

          }

          else if (a>19 && a<=49)

          {

                   printf (“you are Adult”);

          }

          else if (a>49 && a<=69)

          {

                   printf (“you old”);

          }

          else if (a>69 && a<=84)

          {

                   printf (“you are Jestha Nagarik”);

          }else if (a>84)

          {

                   printf (“you are Too old”);

          }

          else

          {

                   printf (“Try Again by Entering the valid age!!!”);

          }

}

Q2.

1-3= Pre-Primary level

4-5= Primary level

6-8= L.Secondary

9-10= Secondary level

11-12= H. Secondary Level

13-16= BAchelor level

17-18= Masters Level

19-20= Before pHD level

21-25= phD level

Try Again please input the valid class

Q3. WAP to find If the number is even or odd.

 void main(){

          int a;

printf (“Enter any number”);

          scanf (“%d”,&a);

    if (a % 2 ==0)

    {

          printf (“The number is even”);

          }else

          {

                   printf (“The number is odd”);

          }

}

Q4. WAP to find  if the number is +ve, -ve or zero.

Iterative Statements/Looping Statements.

          Iterative statements perform the same set of statements zero or more time depends on the condition are repetition of job. When we want to do the same type of job in a finite or infinite number of times then we take the help of iterative/looping statements.

          Iterative/Looping is the statement which executes its body till the condition is true and terminates if the condition is false. There are also three types of iterative/looping statement used in C-programming. They are:

i. for statementii. while statementiii. do-while statement
syntax:   for (initialization;condition,incr/dec) {      body of for; }    syntax : initial variable; while (condition) {     body of while;   counter; }syntax: do {     body of do-while;   counter; }while (condtion);
eg; void main( ){   int a;   for (a=1; a<=5; a++)      {          printf (“%d “, a);      } }eg; void main( ) { int a; a=1; while (a<=5) {      printf (“%d”, a);   a+=1;   } }eg; void main( ) { int a; a=1; do {      printf (“%d”, a);   a+=1;   }while (a<=5); }
Looping Statements

Questions :

1. WAP to print 1 3 5 7 9 up to 10th term.

2. WAP to print 100  99  98  97  96  95 .. up to 11th term.

3. WAP to print fibonic series  1  1  2  3  5  8 .. up to 12th  term

4. WAP to print hailstone series   6  3  10   5  16 .up to 12th  term.

5. WAP to find sum of first five natural numbers.

6. WAP to find sum of first 100 natural numbers.

Leave a Comment

Your email address will not be published. Required fields are marked *