Header Ads

Examples of C Programming Language

C Programs

1. Hello World

#include<stdio.h>
//tells compiler to include std input output header file

int main()
{
  printf("Hello World");
 
  return 0;

}


2. Print Integer

#include<stdio.h>
int main()
{
  int a;
  printf("Enter an integer\n");
  scanf("%d",&a);
  printf("Integer that you have entered is %d\n",a);
  return 0;
}


3. Addition of two number

#include<stdio.h>
int main()
{
  int a,b,sum;
  printf("Enter two integers to add\n");
  scanf("%d%d",&a,&b);
  sum = a+b;
  printf("Sum of two number is = %d\n",sum);
  return 0;
}

4. Area of triangle

#include<stdio.h>
int main()                                            
{
  int height,base;
  float ans;
  printf("Enter the height and base of triangle");
  scanf("%d%d",&height,&base);
  ans = (1/2)*height*base;
  printf("Area of triangle is %f",ans);
}


5. Odd or Even

#include<stdio.h>
int main()
{
  int n;
  printf("Enter the integer\n");
  scanf("%d",&n);
  if(n%2==0)
  printf("Even");
  else
  printf("Odd\n");
  return 0;
}


6.  Add subtract multiply divide

#include<stdio.h>
int main()
{
  int a,b,add,sub,mult;
  float div;
  printf("Enter two integers\n");
  scanf("%d%d",&a,&b);
 
  add = a+b;
  sub =a-b;
  mult =a*b;
  div =a/b;
 
  printf("sum is %d",add);
  printf("difference is %d",sub);
  printf("multiplication is %d",mult);
  printf("Division is = %2f\n ",div);

  return 0;
}


7.  Add n Number


#include<stdio.h>
int main()
{
  int n,sum=0,c,value;
  printf("Enter the number of integers you want to add\n");
  scanf("%d",&n);
 
  printf("Enter %d integers \n",n);
 
  for(c =1;c<=n;c++)
  {
   scanf("%d",&value);
   sum = sum+value;
  }

  printf("Sum of entered integers = %d\n",sum);
  return 0;
}


8.  Add digits


#include<stdio.h>
int main()
{
  int n,sum = 0,remainder;

  printf("Enter an intger\n");
  sacnf("%d",&n);
 
  while(n!=0)
  {
   remainder = n%10;
   sum = sum+remainder;
   n =n\10;
  }

  printf("Sum of digits of entered number = %d\n",sum);

  return 0;
}







9.  calculate percentage

#include < stdio.h >

void main()
{
int s1, s2, s3, s4, s5, sum, total = 500;
float per;
printf("\nEnter marks of 5 subjects : ");
scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d", sum);

per = (sum * 100)/500;
/* percentage formula*/
printf("\nPercentage : %f", per);
}

10.  Calculate Gross salary

#include < stdio.h >

void main()
{
int gross_salary, basic, da, ta;

printf("Enter basic salary : ");
scanf("%d", &basic);

da = (10 * basic)/100;
ta = (12 * basic)/100;

gross_salary = basic + da + ta;
printf("\nGross salary : %d",gross_salary);
}


11.  Simple interest

#include < stdio.h >

void main()
{
int amount, rate, time, ans;

printf("\nEnter Principal Amount : ");
scanf("%d", &amount);

printf("\nEnter Rate of Interest : ");
scanf("%d", &rate);

printf("\nEnter Period of Time : ");
scanf("%d", &time);

ans = (amount * rate * time)/100;
/Simple interest formula/
printf("\nSimple Interest : %d",ans);
}


12.   Area of Circle

#include < stdio.h >
#include < math.h >
#define PI 3.142

void main()
{
float radius, area;

printf("Enter the radius of a circle \n");
scanf("%f", &radius);

area = PI * pow(radius, 2);

printf("Area of a circle = %5.2f\n", area);
}


13.  Area of Rectangle

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


int main()
{
      int length,              breadth, area;

printf("\nEnter the Length of Rectangle : ");
scanf("%d", &length);

printf("\nEnter the Breadth of Rectangle : ");
scanf("%d", &breadth);

area = length * breadth;
printf("\nArea of Rectangle : %d", area);

return (0);
}



14.   Area of Square

#include < stdio.h >

int main()
{   
 int side, area;

printf("\nEnter the Length of Side : ");
scanf("%d", &side);

area = side * side;
printf("\nArea of Square : %d", area);

return (0);
}



15.  Volume of Cube

#include < stdio.h >

void main()
{
            float a;
float surface_area,volume;

printf("Enter size of any side of a cube : ");
scanf("%f",&a);

surface_area = 6 * (a * a);
volume = a * a * a;

printf("Surface area of cube is: %.3f",surface_area);
printf("\nVolume of cube is : %.3f",volume);
}



16.   Volume of Cylinder

#include < stdio.h >

void main()
{
            float vol,pie=3.14;
float r,h;

printf("ENTER THE VALUE OF RADIOUS :- ");
scanf("%f",&r);
printf("ENTER THE VALUE OF HEIGHT :- ");
scanf("%f",&h);

vol = pie * r * r * h;
printf("VOLUME OF CYLINDER IS :- %3.2f ",vol);
}



 17.  Volume of Sphere

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


void main()
{
float radius,pie,volume;
pie=3.1416;

printf("Enter the radius:");
if(scanf("%f",&radius)==1)
{
volume=(4/3)*pie*pow(radius,3);
printf("The volume is :%6.2f",volume);
}
else
{
printf("error ,enter correct value");
}






18.  Check vowel

#include < stdio.h >

int main()
{
  char ch;

  printf("Input a character\n");
  scanf("%c", &ch);

  switch(ch)
  {
  case 'a':
  case 'A':
  case 'e':
  case 'E':
  case 'i':
  case 'I':
  case 'o':
  case 'O':
  case 'u':
  case 'U':
  printf("%c is a vowel.\n", ch);
  break;
  /*if ch matches any case then it prints & breaks the execution */
  default:
  printf("%c is not a vowel.\n", ch);
 /*if the ch is not from the cases then prints ch is not a vowel */
  }
  return 0;
}


19.  Leap year

#include < stdio.h >

int main()
{
  int year;

  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);

  if ( year%400 == 0)
  printf("%d is a leap year.\n", year);
  else if ( year%100 == 0)
  printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 )
  printf("%d is a leap year.\n", year);
  else
  printf("%d is not a leap year.\n", year);

  return 0;
}


20.  HCF and LCM

#include < stdio.h >

long gcd(long, long);
int main() {
  long x, y, hcf, lcm;

  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);

  hcf = gcd(x, y);
  lcm = (x*y)/hcf;

  printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);

  return 0;
}

/*if 1st no is 0 then 2nd no is gcd
make 2nd no 0 by subtracting smallest from largest and return 1st no as gcd*/
long gcd(long x, long y) {
  if (x == 0) {
  return y;
  }

  while (y != 0) {
  if (x > y) {
    x = x - y;
  }
  else {
    y = y - x;
  }
}

return x;
}




10 comments:

  1. This is my first post on this new blog.
    I think it would definitely help everyone to understand c programs.

    thank you

    ReplyDelete
  2. Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your feed to stay informed of any updates. 稼げる

    ReplyDelete
  3. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! c program for lcm of two numbers

    ReplyDelete
  4. If technology in the classroom is effective; why do many students dislike it so much? The effectiveness of technology use in the classroom has become a controversial issue. While many teachers and students feel that it's best to use technology because it enhances teaching many others feel that it causes too many challenges and that it is a waste of time. If technology is as effective in the classroom as many teachers believe it to be; why do some students dislike it so much? Programming Help

    ReplyDelete
  5. A thoughtful insight and ideas I will use on my blog. You have obviously spent a lot of time on this. Well done! سایت شرط بندی ریچ بت

    ReplyDelete
  6. I wish to get across my admiration for your kindness supporting those people who must have help with this one field. Your personal commitment to getting the message all through had become really insightful and has constantly made folks just like me to attain their desired goals. Your new useful tutorial signifies a lot a person like me and extremely more to my colleagues. Regards; from each one of us. devops course

    ReplyDelete
  7. A thoughtful insight and ideas I will use on my blog. You have obviously spent a lot of time on this. Well done! かに本舗

    ReplyDelete
  8. What an amazing review it is! Can I get some more of you thoughts on the topic? buy shrooms

    ReplyDelete
  9. Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us. best programming chair

    ReplyDelete

Popular Posts

Theme images by 5ugarless. Powered by Blogger.