0% found this document useful (0 votes)
16 views

C Programs

I m a beginner

Uploaded by

chandanmurthy3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

C Programs

I m a beginner

Uploaded by

chandanmurthy3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Programs Samples

1. Write the C program to find the area of circle

#include <stdio.h>
void main()
{
float r , area;
printf("Enter The Radius of Circle\n");
scanf("%f", &r);
area = 3.142* r * r;
printf("The area of the circle= %f", area);
return 0;
}

2. Write the C program to find the area of rectangle

#include <stdio.h>
void main()
{
float w, h, area;
printf("enter the height and width\n");
scanf("%f %f", &h, &w);
area = h * w;
printf("Area of the rectangle=%f", area);
return 0;

3. Write the C program to find the area of triangle

#include <stdio.h>
void main()
{
float b, h, area;
printf("enter the height and base \n");
scanf("%f %f", &h, &b);
area=(h*b)/2;
printf("Area of the triangle=%f", area);
return 0;
}

1
C Programs Samples

4. Write the C program to find the area of square

#include <stdio.h>
void main()
{
float side, area;
printf("enter the side\n");
scanf("%f", &side);
area=side*side;
printf("area of square = %f", area);
return 0;
}

5. Write a C program to calculate Simple Interest

#include <stdio.h>
void main()
{
float p, t, r, si;
printf("enter the value of Principal, Time and Rate of interest \n");
scanf("%f %f %f", &p, &t, &r);
si = (p*t*r)/100;
printf(" Simple Interest = %f", si);
return 0;
}

6. Write a C program to swap two numbers

#include <stdio.h>
void main()
{
int a, b, temp;
printf("Enter two number \n ");
scanf("%d %d", &a, &b);
temp =a;
a = b;
b = temp;
printf("After swapping a= %d b= %d", a, b);
return 0;
}
2
C Programs Samples

7. Write a C program to swap two numbers without using temporary


variable

#include <stdio.h>
void main()
{
int a, b;
printf("Enter two number \n ");
scanf("%d %d", &a, &b);
a=a + b;
b=a - b;
a=a - b;
printf("After swapping a= %d b= %d", a ,b);
return 0;
}

8. Write a c program to convert Fahrenheit to Celsius

#include <stdio.h>
void main()
{
float f, c;
printf("enter the value in celsius\n");
scanf("%f", &c);
celsius=(f - 32.0) * 5.0 / 9.0;
printf("value in celsius is %f",celsius);
return 0;
}

3
C Programs Samples

9. Write a c program to check whether a number is positive , negative or


zero

#include <stdio.h>
void main()
{
int n;
printf("Enter the number \n ");
scanf("%d", &n);
if (n > 0)
printf("%d is positive.", n);
else
if (n < 0)
printf("%d is negative.", n);
else
printf("%d is zero.", n);
return 0;
}

10. Write a c program to check whether a number is even or odd

#include <stdio.h>
void main()
{
int n;
printf("Enter the number \n ");
scanf("%d", &n);
if (n%2 == 0)
printf("%d is even.", n);
else
printf("%d is odd.", n);
return 0;
}

4
C Programs Samples

11. Write a c program to find largest of 3 numbers

#include <stdio.h>
void main()
{
int a, b, c;
printf("Enter three numbers \n ");
scanf("%d %d %d", &a, &b, &c);
if (a >b)
{
if(a>c)
printf("a is larger");
else
printf("c is larger");
}
else
{
if(b>c)
printf("b is larger");
else
printf("c is larger");
}
return 0;
}

12. Write a c program to check whether a number is prime or not

#include <stdio.h>
void main()
{
int n, i, m=0,flag=0;
printf("Enter the number to check prime \n");
scanf("%d", &n);
for(i=2;i<=n/2;i++)
{
if(n % i = =0)
{
flag=1;
break;
5
C Programs Samples

}
}
if(flag = = 0)
printf("Number is prime");
else
printf("Number is not prime");
return 0;
}

13. Write a c program to check for leap year

#include <stdio.h>
void main()
{
int year;
printf("Enter the year \n ");
scanf("%d", &year);
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;

14. Write a C Program to Check Vowel or Consonant

#include <stdio.h>
void main()
{
char ch;
printf("enter the character \n");
scanf("%c",&ch);
if (ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u'||ch == 'A'||ch ==
'E'||ch == 'I'||ch == 'O'||ch == 'U')
printf("Its a vowel");
else
printf("Its a consonant");
return 0;
}

6
C Programs Samples

15. Write a C program to print n natural numbers using for loop

#include <stdio.h>
void main()
{
int i, n;
printf("Enter the value of n \n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("%d \t", i);
}
return 0;
}

16. Write a C program to print n natural numbers using while loop

#include <stdio.h>
void main()
{
int i, n;
printf("Enter the value of n \n");
scanf("%d", &n);
i=1;
while(i <= n)
{
printf("%d \t", i);
i++;
}
return 0;
}

7
C Programs Samples

17. Write a C program to find sum of n natural numbers using for loop

#include <stdio.h>
void main()
{
int i, n, sum=0;
printf("Enter the value of n \n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum=sum + i;
}
printf("sum = %d", sum);
return 0;
}

18. Write a C program to find sum of n natural numbers using while loop

#include <stdio.h>
void main()
{
int i, n, sum=0;
printf("Enter the value of n \n");
scanf("%d", &n);
i=1;
while(i <= n)
{
sum = sum + i;
i++;
}
printf("sum = %d", sum);
return 0;
}

8
C Programs Samples

19. Write a c program to find whether a number is palindrome or not

#include <stdio.h>
void main()
{
int n, rev = 0, rem, org;
printf("Enter an integer \n ");
scanf("%d", &n);
org = n;
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n/10;
}
if (org = = rev)
printf("%d is a palindrome.", org);
else
printf("%d is not a palindrome.", org);
return 0;
}

You might also like