C
June 21, 2021•1,154 words
05-12
Syllabus
05-19
C program structure:
- documentations (documentation section)
- Preprocessor (link section)
- Global declaration (defination section)
main () function
➢ local declaration
➢ program statements & expressionsUser defined functions
05-26
Flowchart
graphical representation that explains the sequence of operations to be performed in order to solve a problem under consideration
Flowchart Symbols
Algorithm & Flowchart to find sum of two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
06-01
Variables and datatypes
06-02
basic C syntax
06-03
scanf and printf
06-08
program to find sum of two numbers
#include <stdio.h>
int main()
{
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum of the entered numbers: %d", sum);
}
program to find simple interest
#include <stdio.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);
SI = (principle * time * rate) / 100;
printf("Simple Interest = %f", SI);
}
06-09
to find simple interest
Step 1: Start
Step 2: Read Principal Amount, Rate and Time
Step 3: Calculate Interest using formula SI= ((amount*rate*time)/100)
Step 4: Print Simple Interest
Step 5: Stop
to find area of circle
Step 1: Start
Step 2: input radius
Step 3: let pi = 3.14
Step 5: circumference = 2 * pi * radius
Step 6: print circumference
Step 7: stop
#include < stdio.h >
int main()
{
float pi=3.14;
float radius, area;
printf("Enter radius of circle: ");
scanf("%f", & radius);
area = pi * radius * radius;
printf("Area of circle : %0.4f\n", area);
}
06-10
find largest among two numbers
Step 1 : Start
Start 2 : Input a, b
Start 3 : if a > b then Output a else Output b
Start 7 : Stop
#include <stdio.h>
int main() {
int a, b;
printf("Enter Two different numbers: ");
scanf("%d %d", &a, &b);
if(a > b)
{
printf("%d is Largest\n", a);
}
else
{
printf("%d is Largest\n", b);
}
}
06-16
find weather given number is odd or even
Step 1 : Start
Step 2 : Define variable number
Start 3 : Read number
Start 4 : if (number%2 == 0), Write " even number" else Write " odd number"
Start 5 : Stop
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("even number");
else
printf("odd number");
}
06-18
find largest among 3 numbers
Step 1 : Start
Start 2 : Input A, B, C
Start 3 : if A > B goto step 4, otherwise goto step 5
Start 4 : if A > C goto step 6, otherwise goto step 8
Start 5 : if B > C goto step 7, otherwise goto step 8
Start 6 : Output "a is the largest", goto step 9
Start 7 : Output "b is the largest", goto step 9
Start 8 : Output " c is the largest", goto step 9
Start 9 : Stop
#include <stdio.h>
int main()
{
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
if (B >= A && B >= C)
printf("%d is the largest number.", B);
if (C >= A && C >= B)
printf("%d is the largest number.", C);
}
find largest among 2 numbers if different and print same if both are same
Step 1: Start
Step 1: Define variables a and b
Step 1: Take input a and b
Step 1: If a==b, print both are equal
Step 1: If a>b, print a is largest else print b is largest
Step 1: End
#include <stdio.h>
int main()
{
int a, b;
printf("Please Enter Two different values\n");
scanf("%d %d", &a, &b);
if(a==b){
printf("Both are Equal\n");
}
else if(a> b)
{
printf("%d is Largest\n", a);
}
else
{
printf("%d is Largest\n", b);
}
}
06-23
print numbers from 1-10
Step 1: Start
Step 2: Initialize variable n as integer number i = 0
Step 3: i = i + 1
Step 4: print the value of n
Step 5: Go to step 3 until i < 10
Step 6: Stop
#include <stdio.h>
void main()
{
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
}
print numbers from 100-1
Step 1: Start
Step 2: Define integer n = 100
Step 3: print n
Step 4: Decrease n by one i.e n=n- 1
Step 5: If n>=1, go to Step 3
Step 6: End
#include<stdio.h>
main()
{
int n=100;
while(n>=1)
{
printf("%d\n",n);
n--;
}
}
06-24
find sum of 1-100
Step 1: Start
step 2: set n=1, sum =0
step 3: Add n to sum
step 4: Increase n by one i.e n=n+ 1
step 5: If n<=100, go to Step 2
Step 6: Print sum
Step 7: End
#include<stdio.h>
int main(){
int n=1;
int sum=0;
while(n<=100){
sum=sum+n;
n=n+1;
}
printf("Sum from 1 to 100 is %d",sum);
}
find sum of even numbers from 1 to n
Step 1: Start
Step 2: Read the limit of numbers, n
Step 3: Assign i=1
Step 4: Assign sum=0
Step 5: Repeat steps 6,7&8 until i=n reaches
Step 6: If i%2==0 goto step 7 otherwise go to Step 8
Step 7: Compute sum=sum+i
Step 8: Compute i=i+1
Step 9: Print sum of even numbers, sum
Step 10: Stop
#include<stdio.h>
int i, n, sum;
main(){
printf("Enter natural number upto which sum of even number is to be calculate: ");
scanf("%d",&n);
i=1;
sum=0;
while(i<=n){
if(i%2==0){
sum=sum+i;
}
i=i+1;
}
printf("Sum of even numbers from 1 to %d is %d",n,sum);
}
06-25
to find the sum of n natural number
#include<stdio.h>
int i, n, sum;
main(){
printf("Enter natuural number to calculate sum: ");
scanf("%d",&n);
for(i=0; i<=n; i++){
sum=sum+i;
}
printf("The sum of %d natural number is %d.",n,sum);
}