Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

Some Basic Examples of C Programming: Part 1

// print "hello world"

#include <stdio.h>
#include <conio.h>

main ()

{
     printf("Hello World");
     getch ();
     }
----------------------------------------------------------

// conversion between ferhenheit and celcius scale

#include <stdio.h>
#include <conio.h>
main ()
{float c,f;
printf("Enter the value of f=");
scanf("%f",&f);
c=((f-32)/1.8);
printf("The value of c is %f",c);
getch ();
}
-----------------------------------------------------------

// calculation of the area of a circle

#include <stdio.h>
#include <conio.h>
main ()
{float A,R,Z;
     Z=3.1416;
     printf("Enter the value of R");
     scanf("%f",&R);
     A=(Z*R*R);
     printf("The value of area is %f",A);
     getch ();
     }
----------------------------------------------------------

// application of if-else to get the solutions of an quadratic equation

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     float a,b,c,d,root1,root2;
     printf("Enter the Values of a,b and c\n");
     printf("Enter the Value of a\n\n");
     scanf("%f",&a);
     printf("\nEnter the Value of b\n\n");
     scanf("%f",&b);
     printf("\nEnter the Value of c\n\n");
     scanf("%f",&c);
     //scanf("%f%f%f",&a,&b,&c);
     d=((b*b)-(4*a*c));
     if(d<0)
     {printf("The Roots are Imaginary");}
     else
     {
         root1=(-b+sqrt(d))/(2*a);
         root2=(-b-sqrt(d))/(2*a);
         printf("\nroot1=%f \n\nroot2=%f",root1,root2);
         }
         getch ();
         }
----------------------------------------------------------

// comparison of a number

#include <stdio.h>
#include <conio.h>
main ()
{
int no;

printf("\n Enter Number :");
scanf("%d",&no);
if(no>0)
{
printf("\n\n Number is greater than 0 !");
}
else
{
if(no==0)
{
printf("\n\n It is 0 !");
}
else
{
printf("Number is less than 0 !");
}
}
getch();
}
-----------------------------------------------------------

// program to show the grade of a subject

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     float x;
     printf("Enter the Value of x\n");
     scanf("%f",&x);
     if(x>=80 && x<=100)
     {printf("A+");}
     else if(x>=75 && x<80)
     {printf("A-");}
      else if(x>=70 && x<75)
     {printf("A");}
      else if(x>=65 && x<70)
     {printf("B+");}
      else if(x>=60 && x<65)
     {printf("B");}
      else if(x>=55 && x<60)
     {printf("B-");}
      else if(x>=50 && x<55)
     {printf("C+");}
      else if(x>=45 && x<50)
     {printf("C");}
      else if(x>=40 && x<45)
     {printf("D");}
     else if(x<40)
     {printf("F");}
     else if (x>100)
     {printf("Please Enter the Value within 0 to 100");}
     getch ();}
-----------------------------------------------------------

// application of switch in determining values from several equations

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int flag;
     float x,y;
     printf("Enter flag");
     scanf("%d",&flag);
     printf("Enter the value of x");
     scanf("%f",&x);
   
     switch(flag)
     {
                 case -1:
                      y=(x*x);
                      printf("The value of y is %f",y);
                      break;
                   
                 case 0:
                      y=sqrt(x);
                      printf("The value of y is %f",y);
                      break;
                   
                 case 1:
                      y=x;
                      printf("The value of y is %f",y);
                      break;
                   
                 case 2:
               
                 case 3:
                      y=2*(x-1);
                      printf("The value of y is %f",y);
                      break;
                   
                 default:
                         y=0;
                         printf("The value of y is %f",y);}
                       
                         getch ();}
----------------------------------------------------------

// application of switch to choice colors

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

main ()
{
     char choice;
   
     switch(choice=toupper(getchar()))
     {
       case 'r':
            printf("RED");
            break;
         
       case 'w':
            printf("WHITE");
            break;
         
       case 'b':
            printf("BLUE");
            break;
         
       default:
               printf("Error");
               }
               getch ();
               } 
------------------------------------------------------------

// application of switch as well as if-else

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
int menu, numb1, numb2, total;

printf("enter in two numbers -->");
scanf("%d %d", &numb1, &numb2);
printf("enter in choice\n");
printf("1=addition\n");
printf("2=subtraction\n");
scanf("%d",&menu );
switch(menu)
        {
case 1:
                 total = numb1 + numb2;
                 break;
case 2:
                 total = numb1 - numb2;
                 break;

         default:
                     printf("Invalid option selected\n");
}
if(menu == 1)
{printf("%d",total);}
else if(menu==2)
        {printf("%d",total);}


            getch ();
}
---------------------------------------------------------------

//program which will display the consecutive digits from 0 to 9 with one digit on each line using for statement

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int digit;
     for (digit=0; digit<=9; ++digit)
     {printf("%d\n",digit);}
     getch ();
     }
-----------------------------------------------------------------

//program which will display the consecutive digits from 0 to 9 with one digit on each line using while statement

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int digit=0;
     while (digit<=9)
     {printf("%d\n",digit);
     ++digit;}
     getch ();}
-----------------------------------------------------------------

//program which will display the consecutive digits from 0 to 9 with one digit on each line using do-while statement

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int digit=0;
     do {printf("%d\n",digit++);}
     while (digit<=9);
     getch ();}
------------------------------------------------------------------

//calculation of the sum of squares of all the integers between 1 and 10

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int n;
     float sum;
     sum=0;
     n=1;
     while(n<=10)
     {
                 sum=sum+(n*n);
                 n=n+1;
                 }
                 printf("sum=%f\n",sum);
                 getch ();
                 }
--------------------------------------------------------------------

// averaging a list of numbers

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int n,count;
     float x,average,sum=0;
     printf("How many numbers?");
     scanf("%d",&n);
     for (count=1; count<=n; ++count)
     {printf("x=");
     scanf("%f",&x);
     sum+=x;}
     average=sum/n;
     printf("\nThe average is %f\n",average);
     getch ();}
----------------------------------------------------------------------

//repeated averaging of a list of numbers

#include <stdio.h>
#include <conio.h>
#include <math.h>

main ()
{
     int n,count,loops,loopcount;
     float x,average,sum;
     printf("How many lists?");
     scanf("%d",&loops);
   
     for(loopcount=1; loopcount<=loops; ++loopcount)
     {
     sum=0;
     printf("\nList number %d\nHow many numbers?",loopcount);
     scanf("%d",&n);
   
     for(count=1; count<=n; ++count)
     {
     printf("x=");
     scanf("%f",&x);
     sum+=x;}
     average=sum/n;
     printf("\nThe average is %f\n",average);
     }
     getch ();
     }

No comments:

Post a Comment