Introduction to C-Language Programming

Introduction to C-Language Programming

Structured programming

The programming that follows a top down approach, on which developers separate the overall program structure into different sub section, is called structured programming.

 Advantages of structured programming

·        Reduces complexity and easy to code.

·        It takes less time to program and easy to debug.

·        Reuse of modules and flow of control is clear.

  C language

C Language is a high level structured programming language which is used to develop system software. Dennis Ritchie at Bell telephone laboratory developed C-language in early 1970s. C is called middle level language because it combines elements of high level language with some features of assembler.

Data types used in C

Data type are means to identify the type data and associated operation of handling it.

Data types used in C language

a) char       b) int    c) float       d) double         e) void


 Features of C language



Limitations of C language

·        There is no runtime checking. It has poor errors detection system.

·        It does not support object oriented programming.

·        C language has only 32 Keywords

·        There is no strict type checking int data type to float variables. 

 

 Application of C Language

a)  Operating System
b)  Language Compilers/Interface
c)  Assemblers
d)  Text Editors
e)  DBMS 

f)  Utilities etc.


C Token

C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in a C program are known as C tokens

C tokens are of six types.

a)  Keywords (eg: int, while),

b)  Identifiers (eg: main, total),

c)  Constants (eg: 10, 20),

d)  Strings (eg: “total”, “hello”),

e)  Special symbols (eg: (), {}), 

f)  Operators (eg: +, /,-,*)


2. Write a C program to calculate the average of three numbers.
#include <stdio.h>
#include <conio.h>
int main()
{
    int a, b, c;
    float d;
 
    printf("Enter first number: ");
    scanf("%d", &a);
 
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("Enter third number: ");
    scanf("%d", &c);
    d = (a + b + c) / 3.0;
    printf("Average of three numbers = %.2f", d);
 
    return 0;
}
3. Write a C program to calculate the volume of a cylinder.
#include <stdio.h>
#include <conio.h>
int main()
{
    float r, h, v;
    printf("Enter radius and height: ");
    scanf("%f %f", &r, &h);
    v = (22.0 / 7) * r * r * h;
    printf("Volume of cylinder = %.2f", v);
    return 0;
}
4. Write a C program to calculate distance travelled by a body.
#include <stdio.h>
#include <conio.h>
int main()
{
    float u, a, s;
    int t;
    printf("Enter initial velocity: ");
    scanf("%f", &u);
    printf("Enter acceleration: ");
    scanf("%f", &a);
    printf("Enter time: ");
    scanf("%d", &t);
    s = (u * t) + (a * t * t) / 2;
    printf("Distance travelled by body = %.2f", s);
    return 0;
}

5. Write a C program to get radius of circle and then print its area.

#include <stdio.h>
#include <conio.h>
int main() {
    float r, a;
    printf("Enter radius: ");
    scanf("%f", &r);
    a = (22.0 / 7) * r * r;
    printf("Area of circle = %.2f", a);
    return 0;
}
6. Write a C program to calculate and print the volume of a box.
#include <stdio.h>
#include <conio.h>
int main()
{
    int l, b, h, v;
    printf("Enter length: ");
    scanf("%d", &l);
    printf("Enter breadth: ");
    scanf("%d", &b);
    printf("Enter height: ");
    scanf("%d", &h);
    v = l * b * h;
    printf("Volume of box = %d", v);
    return 0;

}

7. Write a C program to get radius of a circle and then print its circumference.
#include <stdio.h>
#include <conio.h>
int main() 
{
    float r, c;
    printf("Enter radius: ");
    scanf("%f", &r);
    c = 2 * (22.0 / 7) * r;
    printf("Circumference of circle = %.2f", c);
    return 0;

}

8. Write a C program to calculate the area of four walls.
#include <stdio.h>
#include <conio.h>
int main() 
{
    int l, b, h, a;
    printf("Enter length: ");
    scanf("%d", &l);
    printf("Enter breadth: ");
    scanf("%d", &b);
    printf("Enter height: ");
    scanf("%d", &h);
    a = 2 * h * (l + b);
    printf("Area of four walls = %d", a);
    return 0;

}

9. Write a C program to find the total surface area of a box.
#include <stdio.h>
#include <conio.h>
int main() {
    int l, b, h, a;
    printf("Enter length: ");
    scanf("%d", &l);
    printf("Enter breadth: ");
    scanf("%d", &b);
    printf("Enter height: ");
    scanf("%d", &h);
    a = 2 * ((l * b) + (b * h) + (h * l));
    printf("Total surface area of box = %d", a);
    return 0;

}

10. Write a C program to calculate and print the simple interest.
#include <stdio.h>
#include <conio.h>
int main()
 {
    float p, t, r, i;
    printf("Enter principal: ");
    scanf("%f", &p);
    printf("Enter time: ");
    scanf("%f", &t);
    printf("Enter rate: ");
    scanf("%f", &r);
    i = (p * t * r) / 100;
    printf("Simple Interest = %.2f", i);
    return 0;
}
 11. Write a C program to get temperature in Celsius from the user and then print it in Fahrenheit.
#include <stdio.h>
#include <conio.h>
int main() {
    float c, f;
    printf("Enter temperature in Celsius: ");
    scanf("%f", &c);
    f = ((c * 9) / 5) + 32;
    printf("%.2f Celsius = %.2f Fahrenheit", c, f);
     return 0;

}

 12. Write a C program to find the area of a triangle.

#include <stdio.h>
#include <conio.h>
int main() {
    int b, h;
    float a;
    printf("Enter base: ");
    scanf("%d", &b);
    printf("Enter height: ");
    scanf("%d", &h);
    a = (b * h) / 2.0;
    printf("Area of triangle = %.2f", a);

    return 0;


}
13. Write a C program to get temperature in Fahrenheit and convert it into Celsius.
#include <stdio.h>
#include <conio.h>
int main()
{
    float c, f;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &f);
    c = (f - 32) * 5 / 9;
    printf("%.2f Fahrenheit = %.2f Celsius", f, c);
    return 0;

}

Control Statement in C

Control statement defines the flow of control within the program. Below are the statements:

  1. Simple if statement: It evaluates the content only if expression is true.
Syntax
if (Condition)  
Statement;  
  Or,  
 if(Condition)  
Statement;  
.......  
}

Example:

1. WAP to enter the marks of English and print “Pass” if it is 40 or more.

# include<stdio.h>  

int main ( )

{

 float e;

 printf (“Enter English Marks \n”);

 scanf (“%f”, &e);

 if (e >=  40)

{

 Printf (“Pass”);

}

 return 0;

}

2.      if-else statement: It verifies the given condition and executes only one out of two blocks of statements based on the condition result.

Syntax
if(Condition)  
Statement;  
else  
Statement;  
  Or,  
  if(Condition)  
True block of statement;  
else  
False block of statement;  
} 

Example:

1. WAP to enter the marks of English and print “Pass” if it is 40 or more, otherwise “Fail”.

# include<stdio.h>
int main ( )
{
 float e;
 printf (“Enter English Marks \n”);
 scanf (“%f”, &e);
 if (e >=  40)
 {
  printf (“Pass”);
 }
 else
 {
   printf (“Pass”);
 }
 return 0;

}


  1. if-else-if statement: It is also known as if-else-if ladder. This statement is used when there are more than two possible actions based on different conditions.
Syntax
if(Condition1)  
  Statement1;  
else if(Condition2)  
  Statement2;  
.........  
.........  
else if(Condition N)  
  Statement N;  
else  
  Default_Statement;  

Example 1: WAP to enter a number and print “Positive” if it is greater than 0, print “Negative” if it is less than 0, otherwise print “Zero”.

# include<stdio.h> 

int main ( )

{

int n;

printf("Enter number");

scanf("%d",&n);

if (n > 0 )

{

 printf (“Positive”);

}

else if (n < 0 )

{

  printf (“Negative”);

}

else

{

 printf (“Zero”);

}

return 0;

}

 

 

  1. Switch statement: It is a multi-way decision maker that tests the value of an expression against a list of integers or character constants. When a match is found, the statements associated with that constant are executed.
  Syntax
switch(expression)  
  case constant1:  
  statement1;  
  break;  
  case constant2:  
  statement2;  
  break;  
  case constant3:  
  statement3;  
  break;  
  Default:  
  statement;  

#include <stdio.h>

int main()

{

    int day;

    printf("Enter a number (1-7) for a day of the week: ");

    scanf("%d", &day);

 

    switch (day) {

        case 1:

            printf("Monday\n");

            break;

        case 2:

            printf("Tuesday\n");

            break;

        case 3:

            printf("Wednesday\n");

            break;

        case 4:

            printf("Thursday\n");

            break;

        case 5:

            printf("Friday\n");

            break;

        case 6:

            printf("Saturday\n");

            break;

        case 7:

            printf("Sunday\n");

            break;

        default:

            printf("Invalid input! Please enter a number between 1 and 7.\n");

    }

     return 0;

}


a) 5, 10, 15, ………………….50.
#include<stdio.h>  
int main()
{
    int i,s;
    for (i=1;i<=10;i++)
    {
        s=5*i;
        printf("%d\n",s);
    }  
    return 0;

}

b) WAP to generate fibonacci series. [ 0,1,1,2,3,5,8 …..10th term]
#include<stdio.h> 
int main()
{
    int i,a=0,b=1,c;
    printf(“%d%d””,a,b);
    for (i=1;i<=10;i++)
    {
       c=a+b;
       printf(“%d”,c);
       a=b;
       b=c;        
    }
    return 0;

}

c) WAP to generate 1 2 4 7 11 ...............10th term
#include<stdio.h>  
int main()
{
    int i,a=1,g=1;
    for (i=1;i<=10;i++)
    {
       printf(“%d”,a);
       a=a+g;
       g=g+1; 
    }
    return 0;

}

d) WAP to generate 1 2 5 10 17 ...............10th term
#include<stdio.h> 
int main()
{
    int i,a=1,g=1;
    for (i=1;i<=10;i++)
    {
       printf(“%d”,a);
       a=a+g;
       g=g+2;
    }
    return 0;
}
e) WAP to generate  3 12 27 48 ................10th term
#include<stdio.h>  
int main()
{
    int a,i;
    for (i=1;i<=10;i++)
    {
       a=3*I*I;       
       printf(“%d”,a);
    }
    return 0;

3 تعليقات

إرسال تعليق

Post a Comment

أحدث أقدم