Programs in C | Examples



LIST OF PROGRAMS


1. Write a program to find the SimpleInterest.

Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

float principle, time, rate, SI;

printf ("Enter principle (amount): ");
scanf ("%f", &principle);

printf ("Enter time: ");
scanf ("%f", &time);

printf ("Enter rate: ");
scanf ("%f", &rate);

// Calculate simple interest
SI = (principle * time * rate) / 100;

// Print the resultant value of SI
printf ("Simple Interest = %f", SI);

return(0);

getch();
}

2. Write a program to find sum of two numbers.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int firstNo, secondNo, sum;
printf ("Enter two integers: ");

// Two integers entered by user is stored using scanf() function
scanf ("%d %d", &firstNo, &secondNo);

// sum of two numbers in stored in variable sumOfTwoNumbers
sum = firstNo + secondNo;

// Displays sum
printf ("%d + %d = %d", firstNo, secondNo ,sum);
return 0;
getch();
}

3. Write a program to find the circumference of a circle.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
float radius,cir;
printf ("Enter radius of circle :");

// Radius is entered by user is stored using scanf() function
scanf ("%f", &radius);

// circumference is stored in variable cir
cir = 2 * 3.14 * radius;

// Displays Circumference of Circle
printf ("\nCircumference of circle :%f", cir);
return 0;
getch ();
}

4. Write a program to convert temperature from degree Centigrade to Fahrenheit.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
float Centigrade, fahrenheit;

printf ("Enter Centigrade : ");
scanf ("%f", &Centigrade);

// calculate fahrenheit using the equation
fahrenheit = (1.8 * Centigrade) + 32;

// Print the resultant value of fahrenheit
printf ("Fahrenheit = %f", fahrenheit);
return(0);
getch();
}

5. Write a program to convert binary number to digital number.


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

// function prototype
int convert_B2D (long long);

int main ()
{
  long long n;
  printf ("Enter a Binary Number: ");
  scanf ("%lld", &n);
  printf ("Decimal of Binary %lld  is %d ", n, convert_B2D (n));
  return 0;
}

// function definition
int convert_B2D (long long n)
{
  int D = 0,R, i = 0;

  while (n != 0)
    {
      R = n % 10;
      n /= 10;
      D += R * pow (2, i);
      ++i;
    }

  return D;
}

6. Write a program to find number is even or odd.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
//clrscr();

printf ("Enter any number : ");
scanf ("%d", &num);

// Check weather number is even or odd
if (num % 2 == 0)
{
printf ("The given number is EVEN");
}
else
{
printf ("The given number is ODD");
}
return(0);
getch();
}

7. Write a program to show the use of conditional operator.


Do You Know ? Conditional Operators [ ? : ] It is ternary Operator Statement. Operator that works on 3 operands is called as tertiary operator. Syntax: expression 1 ? expression 2 : expression 3 where : expression1 is Condition expression2 is Statement Followed if Condition is True expression2 is Statement Followed if Condition is False
Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
//clrscr();
printf ("Enter any number : ");
scanf ("%d", &num);
// Check weather number is even or odd using Conditional Operator
(num%2==0) ? printf("Even") : printf("Odd");

return(0);
getch();
}

Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int num,flag;
//clrscr();
printf ("Enter any number : ");
scanf ("%d", &num);

// Check weather number is even or odd using Conditional Operator
flag=(num%2==0) ? 1 : 0;
if (flag == 1)
{
printf ("The given number is EVEN");
}
else
{
printf ("The given number is ODD");
}

return(0);

getch();
}

8. Write a program to reverse a given number.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int n , reverse=0;
//clrscr();

printf ("Enter the number : ");
scanf ("%d", &n);

// Logic for reverse number
while( n != 0)
{
reverse = n % 10;
n = n / 10;
printf ("Reverse Number is: %d", reverse);
}

return(0);
getch();
}

9. Write a program to find gross salary of employee.
Note : Gross salary=Basic salary + DA + HRA + Bonus
where : DA=20% of Basic salary , HRA=60% of DA , Bonus=20% of HRA.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int bs,da,hra,gs,bonus;
//clrscr();

printf ("Enter Basic Salary : ");
scanf ("%d", &bs);

da=(20*bs)/100;
hra=(60*da)/100;
bonus=(20*hra)/100;
gs=da+hra+bonus+bs;
// Print Gross salary
printf ("Reverse Number is: %d", gs);

return(0);
getch();
}

10. Write a program to find the greatest in three numbers.


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

int main()
{

int fn , sn , tn;
//clrscr();

printf ("\nEnter First Number : ");
scanf ("%d", &fn);

printf ("\nEnter Second Number : ");
scanf ("%d", &sn);

printf ("\nEnter Third Number : ");
scanf ("%d", &tn);

if (fn >= sn && fn >= tn)
{
printf(" Greatest Number is %d",fn);
}

if (sn >= fn && sn >= tn)
{
printf(" Greatest Number is %d",sn);
}

if (tn >= sn && tn >= sn)
{
printf(" Greatest Number is %d",tn);
}

return(0);
getch();

}

11. Write a program to swap two numbers without using third variables.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()

{

int x = 20 , y = 30;
//clrscr();

printf ("\nOld value of x=%d and y=%d", x , y);

// Code for swap withou using temp variable
x = x + y; // x now becomes 50
y = x - y; // y becomes 20
x = x - y; // x becomes 30

printf ("\nAfter Swapping New value of : x = %d and y = %d", x , y);

return(0);
getch();
}

12. Write a program to find year is leap year or not.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int year;
//clrscr();

printf ("\nEnter the year");
scanf ("%d", &year);

// Condition for leap year
if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf ("\nLeap year.");
}
else
{
printf ("\nNot a Leap year.");
}
return(0);
getch();

}

13. Write a program to calculate sum of five subjects marks and percentage.



Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int sub1,sub2,sub3,sub4,sub5,total=500,sum=0;
float per;
//clrscr();

printf ("Enter Marks of 5 Subjects:");
scanf ("%d%d%d%d%d", &sub1,&sub2,&sub3,&sub4,&sub5);

sum=sub1+sub2+sub3+sub4+sub5;
printf ("Sum of 5 Subjects is: %d\n",sum);

per=(sum*100)/total;
printf ("Percentage is: %f ",per);

return(0);
getch();
}

14. Write a program to generate table of any number. like (a*b=c).


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int n, i,sum=0;
//clrscr();

printf (" Enter any number :\n");
scanf ("%d", &n);

for(i=1; i<=10; ++i)
{
sum = sum + i;
printf("%d * %d = %d \n ", n, i, n*i );
}
return(0);
getch();
}

15. Write a program to display Monday to Saturday using switch statement.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

char ch;
//clrscr();

printf ("Enter m/M for Monday\nt/T for Tuesday\nw/W for Wednesday\nh/H for Thursday\nf/F for Friday\ns/S for Saturday\nu/U for Sunday");
scanf ("%c", &ch);

switch(ch)
{
case 'm':
case 'M':
printf ("Monday");
break;
case 't':
case 'T':
printf ("Tuesday");
break;
case 'w':
case 'W':
printf ("Wednesday");
break;
case 'h':
case 'H':
printf ("Thursday");
break;
case 'f':
case 'F':
printf ("Friday");
break;
case 's':
case 'S':
printf ("Saturday");
break;
case 'u':
case 'U':
printf ("Sunday");
break;
default :
printf ("Invalid Choice");
break;
}
return(0);
getch();
}

16. Write a program to display Arithmetic Operator using switch statement.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int a , b , op;
//clrscr();

printf (" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf ("Enter the value of a & b:");
scanf ("%d%d", &a,&b);

printf ("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf ("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:
printf ("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:
printf ("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4:
printf ("Division of Two Numbers is %d : ",a,b,a/b);
break;
default :
printf ("Invalid Choice");
break;
}
return(0);
getch();
}

17. Write a program to display first 10 natural number and their sum.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int j, sum = 0;
//clrscr();

printf (" The first 10 natural number is :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ", j);
}
printf("\nThe Sum of 10 natural number is : %d\n", sum);
return(0);
getch();
}

18. Write a program to print Fibonacci series up to 100.(By using For Loop)


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int i, n, a = 0, b = 1, nextTerm;
//clrscr();

printf (" Enter the number of terms :\n");
scanf ("%d", &n);

printf (" Fibonacci Series is :\n");
for (i = 1; i <= 10; i++)
{
printf("%d ", a);
nextTerm = a + b;
a=b;
b=nextTerm;
}
return(0);
getch();
}

By using While Loop
Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int i, n, a = 1, b = 1, nextTerm;
//clrscr();

printf (" Enter the number of terms :\n");
scanf ("%d", &n);

// Displays the first two terms which is always 0 and 1
printf (" Fibonacci Series is :%d ,%d\n",a,b);
nextTerm = a + b;
while(nextTerm <= n)
{
printf("%d ", nextTerm);
a=b;
b=nextTerm;
nextTerm = a + b;
}
return(0);
getch();
}

19. Write a program to display 10 element of array and display their sum and average.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int Arr[100], n, i, sum = 0;
//clrscr();

printf (" Enter the number of elements you want to insert :\n");
scanf ("%d", &n);

for (i = 0; i < n; i++)
{
printf("%d ", i+1);
scanf("%d", &Arr[i]);
sum += Arr[i];
}
printf("\nThe sum of the array is : %d", sum);
printf("\nThe average of the array is : %f", (float)sum / n);
return 0;
getch();
}

20. Write a program to find given number is prime or not.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i , flag=0;
//clrscr();

printf (" Enter any number :\n");
scanf ("%d", &n);

for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf ("%d is not a prime no :",n);
}
else
{
printf ("%d is a prime no :",n);
}
return(0);
getch();
}

21. Write a program to display sum of series 1+1/2+1/3+......1/n.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i ;
float sum=0.0;
//clrscr();

printf (" Enter any number :\n");
scanf ("%d", &n);

for(i=1;i<=n;i++)
{
if(i<n)
{
printf (" 1/%d + ",i);
sum=sum + 1/(float)i;
}
if(i==n)
{
printf (" 1/%d ",i);
sum=sum + 1/(float)i;
}
}
return(0);
getch();
}

22. Write a program to display sum of series 1+ 3+ 5.....+ n.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int n , i , sum=0;
clrscr();

printf (" Enter any number :\n");
scanf ("%d", &n);

for(i = 1; i<n; i = i + 2 )
{
printf ("%d + ",i);
sum = sum + 1;
}
printf ("%d ",n);
printf (" \nSum = %d ",sum + n);
return(0);
getch();
}

23. Write a program to find factorial of a number.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{

int n, i , fact=1;
//clrscr();

printf (" Enter any number :\n");
scanf ("%d", &n);

for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf (" Factorial of %d = %d\n",n,fact);
return(0);
getch();
}

24. Write a program to find maximum number of array.


Copy Code
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10], Size, i, max, Position;
//clrscr();

printf ("\nEnter the size of an array \n");
scanf ("%d",&Size);

printf ("\n Enter %d elements of an array: \n",Size);
for(i=0; i<Size; i++)
{
scanf ("%d",&a[i]);
}
max = a[0];
for(i=1; i<Size; i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf ("\nMaximum element in an Array = %d",max);

return(0);
getch();
}

25. Write a program to add two number using pointers.


Copy Code
Code

26. Write a program to display a matrix.


Copy Code
Code

27. Write a program to display Multiplication of two matrix.


Copy Code
Code

28. Write a program to find maximum number in array using pointer.


Copy Code
Code

29. Write a program to display input and output of string.


Copy Code
Code

30. Write a program to display square of number using function.


Copy Code
Code

31. Write a program to Swap two numbers using function.


Copy Code
Code

32. Write a program to display table of a number using function.


Copy Code
Code

33. Write a program to find factorial of a number using recursion.


Copy Code
Code

34. Write a program to find whether a string is palindrome or not.


Copy Code
Code

35. Write a program to count words, vowels, consonant, space and special character in statement.


Copy Code
Code

36. Program Title.


Copy Code
Code

No comments:

Post a Comment