Program, Flowchart, Algorithm (All)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Program, Flowchart, Algorithm

Equation Based Programs:


To find the area of a rectangle:
Program Flowchart Algorithm

#include<stdio.h>
main()
{
float h, w;
printf("Enter height and width:");
scanf("%f%f",&h,&w);
area=h*w;
printf("Area is %f",area);
}

To find the area of a circle:


Program Flowchart Algorithm

#include<stdio.h>
main()
{
float r, area;
printf("Enter Radius:");
scanf("%f",&r);
area=3.1416*r*r;
printf("Area is %f",area);
}

To find the volume of a sphere:


Program Flowchart Algorithm

#include<stdio.h>
main()
{
float r, volume;
printf("Enter Radius:");
scanf("%f",&r);
volume=(4/3)*3.1416*r*r*r;
printf("Volume is %f",volume);
}
To find the area of a triangle (When height and base are given):
Program Flowchart Algorithm

#include<stdio.h>
main()
{
float h, b, area;
printf("Enter Height and Base:");
scanf("%f%f",&h,&b);
area=0.5*h*b;
printf("Area is %f",area);
}

To find the area of a triangle (When lengths of three sides are given):
Program Flowchart Algorithm

#include<stdio.h>
#include<math.h>
main()
{
float a, b, c, s, area;
printf("Enter lengths:");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("%f",area);
}

To Convert Temperature (Fahrenheit to Centigrade / Centigrade to Fahreheit):


Program Flowchart Algorithm

#include<stdio.h>
main()
{
float c, f;
printf("Enter Centigrade Temp:");
scanf("%f",&c);
f=(c*9)/5+32;
printf("Temp in Fahrenheit = %f",f);
}

Fahrenheit to Centigrade:
[ c = 5*(f-32)/9 ]
Conditional Control Statements:
To find if a number is Positive or Negative:
Program Flowchart Algorithm

#include<stdio.h>
main()
{
int num;
printf("Enter the number:");
scanf("%d",&num);
if(num>0)
printf("Positive");
else
printf("Negative");
}

To find weather a number is Odd or Even:


Program Flowchart Algorithm

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

To find large number among two numbers:


Program Flowchart Algorithm

#include<stdio.h>
main()
{
int n1, n2;
printf("Enter two numbers:");
scanf("%d%d",&n1,&n2);
if(n1>n2)
printf("%d is large",n1);
else
printf("%d is large",n2)
}
Large number among three or more numbers:
Program Flowchart Algorithm

#include<stdio.h>
main()
{
int n1, n2, n3;
printf("Enter three numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>n2 && n1>n3)
printf("%d is large",n1);
else if(n2>n3)
printf("%d is large",n2);
else
printf("%d is large",n3)
}

To find a year is leap year or not:


Program Flowchart Algorithm

#include<stdio.h>
main()
{
int year;
printf("Enter year:");
scanf("%d",&year);
if(year%400==0)
printf("Leap Year");
else if(year%4==0 && year%100!=0)
printf("Leap year");
else
printf("Not leap year");
}

To find a letter is vowel or consonant:


Program Flowchart Algorithm

#include<stdio.h>
main()
{
char letter;
scanf("%c",&letter);
if(letter == 'a' || letter == 'e' || letter ==
'i' || letter == 'o' || letter == 'u'
|| letter == 'A' || letter == 'E' || letter
== 'I' || letter == 'O' || letter == 'U')
printf("Vowel");
else
printf("Consonant"); }
Loop Control Statements:
To print the following numbers: 3, 7, 11, ………………., n
Program (Using for Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int i, n;
printf("Enter last number:");
scanf("%d",&n);
for(i=3; i<=n; i=i+4)
printf("%d\n",i);
}

Program (Using do-while Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int i=3, n;
printf("Enter last number:");
scanf("%d",&n);
do
{
printf("%d\n",i);
i=i+4;
}
while(i<=n);
}
To find the sum of the given series: 33+73+113+……………..+n3
Program (Using for Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int i, n, s=0;
printf("Enter last number:");
scanf("%d",&n);
for(i=3; i<=n; i=i+4)
s=s+i*i*i;
printf("Sum is %d",s);
}
Program (Using do-while Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int i=3, n, s=0;
printf("Enter last number:");
scanf("%d",&n);
do
{
s=s+i*i*i;
i=i+4;
}
while(i<=n);
printf("Sum is %d",s);
}

C program to find n factorial: 1*2*3*……….*n


Program (Using while Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int i=1, n, f=1;
printf("Enter last number:");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i=i+1;
}
printf("Factorial of %d is %d",n,f);
}

Program (Using if-goto Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int i=1, n, f=1;
printf("Enter last number:");
scanf("%d",&n);
again:
{
f=f*i;
i=i+1;
}
if(i<=n) goto again;
printf("Factorial of %d is %d",n,f);
}
C program to find LCM of two integer numbers:
Program (Using if-goto Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int a, b, x;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
if(a>b)
x=a;
else
x=b;
again:
if(x%a==0 && x%b==0)
printf("LCM is %d",x);
else
{
x=x+1;
goto again;
}
}
C program to find GCD of two integer numbers:
Program (Using if-goto Loop) Flowchart Algorithm

#include<stdio.h>
main()
{
int a, b, x;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
if(a<b)
x=a;
else
x=b;
again:
if(a%x==0 && b%x==0)
printf("GCD is %d",x);
else
{
x=x-1;
goto again;
}
}

You might also like