0% found this document useful (0 votes)
24 views23 pages

C Programs With Algorithms

The document contains a series of C programming examples, each accompanied by algorithms and code implementations. It covers basic operations such as printing text, arithmetic calculations, conditional statements, and using switch cases for various applications. Each program includes user input, processing, and output, demonstrating fundamental programming concepts.

Uploaded by

gotoj73289
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)
24 views23 pages

C Programs With Algorithms

The document contains a series of C programming examples, each accompanied by algorithms and code implementations. It covers basic operations such as printing text, arithmetic calculations, conditional statements, and using switch cases for various applications. Each program includes user input, processing, and output, demonstrating fundamental programming concepts.

Uploaded by

gotoj73289
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

C Programs with Algorithms (1–14)

Program 1: Print Your Name

Algorithm:

text

Step 1: START

Step 2: Print "Your Name" on the screen

Step 3: STOP

Code:

#include <stdio.h>

int main()

printf("John");

return 0;

Output:

text

John

Program 2: Print "HELLO ALL" Vertically

Algorithm:

text

Step 1: START

Step 2: Print 'H' and go to next line

Step 3: Print 'E' and go to next line

Step 4: Print 'L' and go to next line


Step 5: Print 'L' and go to next line

Step 6: Print 'O' and go to next line

Step 7: Print ' ' (blank line) and go to next line

Step 8: Print 'A' and go to next line

Step 9: Print 'L' and go to next line

Step 10: Print 'L' and go to next line

Step 11: STOP

Code:

#include <stdio.h>

int main()

printf("H\n");

printf("E\n");

printf("L\n");

printf("L\n");

printf("O\n");

printf("\n");

printf("A\n");

printf("L\n");

printf("L\n");

return 0;

Output:

text

E
L

Program 3: Print Sum of Two Numbers

Algorithm:

text

Step 1: START

Step 2: Declare variables a, b, sum

Step 3: Read the value of a from user

Step 4: Read the value of b from user

Step 5: Calculate sum = a + b

Step 6: Print the value of sum

Step 7: STOP

Code:

#include <stdio.h>

int main()

int a, b, sum;

printf("Enter first number: ");

scanf("%d", &a);
printf("Enter second number: ");

scanf("%d", &b);

sum = a + b;

printf("Sum = %d", sum);

return 0;

Output:

text

Enter first number: 10

Enter second number: 20

Sum = 30

Program 4: Swap Two Numbers

Algorithm:

text

Step 1: START

Step 2: Declare variables a, b, temp

Step 3: Read the value of a from user

Step 4: Read the value of b from user

Step 5: Print values of a and b before swapping

Step 6: Set temp = a

Step 7: Set a = b

Step 8: Set b = temp

Step 9: Print values of a and b after swapping

Step 10: STOP


Code:

#include <stdio.h>

int main()

int a, b, temp;

printf("Enter first number: ");

scanf("%d", &a);

printf("Enter second number: ");

scanf("%d", &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

temp = a;

a = b;

b = temp;

printf("After swapping: a = %d, b = %d", a, b);

return 0;

Output:

text

Enter first number: 5

Enter second number: 10

Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Program 5: Find the Average of Three Numbers

Algorithm:

text

Step 1: START

Step 2: Declare variables a, b, c and avg (float)

Step 3: Read the value of a from user

Step 4: Read the value of b from user

Step 5: Read the value of c from user

Step 6: Calculate avg = (a + b + c) / 3.0

Step 7: Print the value of avg

Step 8: STOP

Code:

#include <stdio.h>

int main()

float a, b, c, avg;

printf("Enter first number: ");

scanf("%f", &a);

printf("Enter second number: ");

scanf("%f", &b);

printf("Enter third number: ");


scanf("%f", &c);

avg = (a + b + c) / 3.0;

printf("Average = %.2f", avg);

return 0;

Output:

text

Enter first number: 10

Enter second number: 20

Enter third number: 30

Average = 20.00

Program 6: Print the Area of a Circle

Algorithm:

text

Step 1: START

Step 2: Declare variable radius and area (float)

Step 3: Set pi = 3.14

Step 4: Read the value of radius from user

Step 5: Calculate area = pi * radius * radius

Step 6: Print the value of area

Step 7: STOP

Code:

#include <stdio.h>
int main()

float radius, area;

float pi = 3.14;

printf("Enter radius of circle: ");

scanf("%f", &radius);

area = pi * radius * radius;

printf("Area of circle = %.2f", area);

return 0;

Output:

text

Enter radius of circle: 7

Area of circle = 153.86

Program 7: Print the Perimeter and Area of a Rectangle

Algorithm:

text

Step 1: START

Step 2: Declare variables length, width, area, perimeter (float)

Step 3: Read the value of length from user

Step 4: Read the value of width from user

Step 5: Calculate area = length * width

Step 6: Calculate perimeter = 2 * (length + width)

Step 7: Print the value of area


Step 8: Print the value of perimeter

Step 9: STOP

Code:

#include <stdio.h>

int main()

float length, width, area, perimeter;

printf("Enter length of rectangle: ");

scanf("%f", &length);

printf("Enter width of rectangle: ");

scanf("%f", &width);

area = length * width;

perimeter = 2 * (length + width);

printf("Area = %.2f\n", area);

printf("Perimeter = %.2f", perimeter);

return 0;

Output:

text

Enter length of rectangle: 10

Enter width of rectangle: 5

Area = 50.00
Perimeter = 30.00

Program 8: Display the Largest of Two Numbers

Algorithm:

text

Step 1: START

Step 2: Declare variables a, b

Step 3: Read the value of a from user

Step 4: Read the value of b from user

Step 5: IF a > b THEN

Print "a is largest"

Step 6: ELSE IF b > a THEN

Print "b is largest"

Step 7: ELSE

Print "Both are equal"

Step 8: STOP

Code:

#include <stdio.h>

int main()

int a, b;

printf("Enter first number: ");

scanf("%d", &a);

printf("Enter second number: ");


scanf("%d", &b);

if (a > b)

printf("%d is largest", a);

else if (b > a)

printf("%d is largest", b);

else

printf("Both are equal");

return 0;

Output:

text

Enter first number: 25

Enter second number: 10

25 is largest

Program 9: Check Whether a Number is Odd or Even

Algorithm:

text

Step 1: START

Step 2: Declare variable num


Step 3: Read the value of num from user

Step 4: IF num % 2 == 0 THEN

Print "num is Even"

Step 5: ELSE

Print "num is Odd"

Step 6: STOP

Code:

#include <stdio.h>

int main()

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("%d is Even", num);

else

printf("%d is Odd", num);

return 0;

}
Output:

text

Enter a number: 7

7 is Odd

Program 10: Check Uppercase, Lowercase, Digit, or Special Character

Algorithm:

text

Step 1: START

Step 2: Declare variable ch (char)

Step 3: Read the character ch from user

Step 4: IF ch >= 'A' AND ch <= 'Z' THEN

Print "Uppercase letter"

Step 5: ELSE IF ch >= 'a' AND ch <= 'z' THEN

Print "Lowercase letter"

Step 6: ELSE IF ch >= '0' AND ch <= '9' THEN

Print "Digit"

Step 7: ELSE

Print "Special character"

Step 8: STOP

Code:

#include <stdio.h>

int main()

char ch;
printf("Enter a character: ");

scanf("%c", &ch);

if (ch >= 'A' && ch <= 'Z')

printf("%c is an Uppercase letter", ch);

else if (ch >= 'a' && ch <= 'z')

printf("%c is a Lowercase letter", ch);

else if (ch >= '0' && ch <= '9')

printf("%c is a Digit", ch);

else

printf("%c is a Special character", ch);

return 0;

Output:

text

Enter a character: A

A is an Uppercase letter

Program 11: Find the Largest of 3 Numbers


Algorithm:

text

Step 1: START

Step 2: Declare variables a, b, c

Step 3: Read the values of a, b, c from user

Step 4: IF a >= b AND a >= c THEN

Print "a is largest"

Step 5: ELSE IF b >= a AND b >= c THEN

Print "b is largest"

Step 6: ELSE

Print "c is largest"

Step 7: STOP

Code:

#include <stdio.h>

int main()

int a, b, c;

printf("Enter first number: ");

scanf("%d", &a);

printf("Enter second number: ");

scanf("%d", &b);

printf("Enter third number: ");

scanf("%d", &c);
if (a >= b && a >= c)

printf("%d is the largest", a);

else if (b >= a && b >= c)

printf("%d is the largest", b);

else

printf("%d is the largest", c);

return 0;

Output:

text

Enter first number: 10

Enter second number: 30

Enter third number: 20

30 is the largest

Program 12: Print Day Name by Day Number

Algorithm:

text

Step 1: START

Step 2: Declare variable day


Step 3: Read the value of day from user

Step 4: Use switch(day):

Case 1: Print "Monday"

Case 2: Print "Tuesday"

Case 3: Print "Wednesday"

Case 4: Print "Thursday"

Case 5: Print "Friday"

Case 6: Print "Saturday"

Case 7: Print "Sunday"

Default: Print "Invalid day number"

Step 5: STOP

Code:

#include <stdio.h>

int main()

int day;

printf("Enter day number (1-7): ");

scanf("%d", &day);

switch (day)

case 1:

printf("Monday");

break;

case 2:
printf("Tuesday");

break;

case 3:

printf("Wednesday");

break;

case 4:

printf("Thursday");

break;

case 5:

printf("Friday");

break;

case 6:

printf("Saturday");

break;

case 7:

printf("Sunday");

break;

default:

printf("Invalid day number");

return 0;

Output:

text

Enter day number (1-7): 3

Wednesday
Program 13: Simple Calculator Using Switch

Algorithm:

text

Step 1: START

Step 2: Declare variables a, b (float) and op (char)

Step 3: Read values of a, op, and b from user

Step 4: Use switch(op):

Case '+': Print a + b

Case '-': Print a - b

Case '*': Print a * b

Case '/': IF b != 0 THEN Print a / b

ELSE Print "Division by zero error"

Default: Print "Invalid operator"

Step 5: STOP

Code:

#include <stdio.h>

int main()

float a, b, result;

char op;

printf("Enter first number: ");

scanf("%f", &a);

printf("Enter operator (+, -, *, /): ");

scanf(" %c", &op);


printf("Enter second number: ");

scanf("%f", &b);

switch (op)

case '+':

result = a + b;

printf("Result = %.2f", result);

break;

case '-':

result = a - b;

printf("Result = %.2f", result);

break;

case '*':

result = a * b;

printf("Result = %.2f", result);

break;

case '/':

if (b != 0)

result = a / b;

printf("Result = %.2f", result);

else

printf("Error! Division by zero");

}
break;

default:

printf("Invalid operator");

return 0;

Output:

text

Enter first number: 10

Enter operator (+, -, *, /): +

Enter second number: 5

Result = 15.00

Program 14: Display Grade Using Switch Statement

Algorithm:

text

Step 1: START

Step 2: Declare variable marks (int)

Step 3: Read the value of marks from user

Step 4: IF marks < 0 OR marks > 100 THEN

Print "Invalid marks" and go to Step 6

Step 5: Use switch(marks / 10):

Case 10:

Case 9: Print "Grade A+"

Case 8: Print "Grade A"

Case 7: Print "Grade B"

Case 6: Print "Grade C"


Case 5: Print "Grade D"

Case 4: Print "Grade E"

Default: Print "Grade F"

Step 6: STOP

Code:

#include <stdio.h>

int main()

int marks;

printf("Enter marks (0-100): ");

scanf("%d", &marks);

if (marks < 0 || marks > 100)

printf("Invalid marks");

else

switch (marks / 10)

case 10:

case 9:

printf("Grade A+");

break;

case 8:
printf("Grade A");

break;

case 7:

printf("Grade B");

break;

case 6:

printf("Grade C");

break;

case 5:

printf("Grade D");

break;

case 4:

printf("Grade E");

break;

default:

printf("Grade F");

return 0;

Output:

text

Enter marks (0-100): 85

Grade A

You might also like