0% found this document useful (0 votes)
2 views11 pages

C Programs for Basic Algorithms and Patterns

The document contains multiple C programming tasks, each with an algorithm and code implementation. Tasks include swapping variables, checking odd/even numbers, determining leap years, solving quadratic equations, summing digits of a 3-digit number, counting digits, performing arithmetic operations, reversing numbers, and displaying patterns. Each task is accompanied by a structured algorithm and C code for execution.

Uploaded by

nikhilpatil9146
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)
2 views11 pages

C Programs for Basic Algorithms and Patterns

The document contains multiple C programming tasks, each with an algorithm and code implementation. Tasks include swapping variables, checking odd/even numbers, determining leap years, solving quadratic equations, summing digits of a 3-digit number, counting digits, performing arithmetic operations, reversing numbers, and displaying patterns. Each task is accompanied by a structured algorithm and C code for execution.

Uploaded by

nikhilpatil9146
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

QUESTION: Write a program to swap two variables with or without third variables. Also make algo and flow.

Algorithm:
1.​ START
2.​ INPUT a,b
3.​ PRINT a,b (before swap w variable)
4.​ temp = a
5.​ b=a
6.​ a=b
7.​ PRINT a,b (after swap w variable)
8.​ PRINT a,b (before swap w/o variable)
9.​ a = a + b
10.​b = a - b
11.​a = a - b
12.​STOP

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

void main()
{
int a, b, temp;
printf("Enter two numbers");
scanf("%d %d", &a, &b); //INPUT a,b
printf("Numbers before swapping with temp variable: %d, %d \n", a, b);
//Swap with variable
temp = b;
b = a;
a = temp;
printf("Numbers after swapping with temp variable: %d, %d \n", a, b);
printf("Numbers before swapping without temp variable: %d, %d \n", a, b);
//Swap without variable
a = a + b;
b = a - b;
a = a - b;
printf("Numbers after swapping without temp variable: %d, %d \n", a, b);
getch();
}
QUESTION: Write a program to check odd or even through (a) modulus op. and (b) conditional op.

Algorithm:
1.​ START
2.​ INPUT a
3.​ d = a%2
4.​ IF d=0, then, go to step 5. ELSE go to step 6.
5.​ PRINT “Even”
6.​ PRINT “Odd”
7.​ STOP
(a)
#include <stdio.h>
#include <conio.h>

void main()
{
int a;
printf("Enter number");
scanf("%d", &a);
if (a % 2 == 0) // Modulus operator
{
printf("Even");
}
else
{
printf("Odd");
}
getch();
}

(b)
#include <stdio.h>
#include <conio.h>

void main()
{
int a;
printf("Enter number");
scanf("%d", &a);
(a % 2 == 0) ? printf("Even") : printf("Odd"); // Ternary operator
return 0;
getch();
}
QUESTION: Design a develop a C program to read year as an input and find whether it is a leap year or
not, also consider the end of the centuries. Also make algo and flow

Algorithm:
1.​ START
2.​ INPUT year
3.​ IF (year%4)=0, then go to step 4. ELSE go to step 7.
4.​ IF (year%100)=0, then go to step 5. ELSE go to step 6.
5.​ IF (year%400)=0, then go to step 6. ELSE go to step 7.
6.​ PRINT “Leap Year”
7.​ PRINT “Not a Leap Year”
8.​ STOP

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

void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (year % 4 == 0)
{
if (year % 100 == 0) // Considering centuries
{
if (year % 400 == 0)
printf("Leap year.\n", year);
else
printf("Not a leap year.\n", year);
}
else
{
printf("Leap year.\n", year);
}
}
else
{
printf("Not a leap year.\n", year);
}
getch();
}
QUESTION: Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a
Quadratic equation (ax²+bx+c=0) as input and compute all possible roots. Implement a C program for
the developed flowchart/algorithm and execute the same to output the possible roots for a given set of
coefficients with appropriate messages.

Algorithm:
1.​ START
2.​ INPUT a,b,c
3.​ D=(b*b)-(4*a*c)
4.​ IF D>0, go to step 5. ELSE go to step 8.
5.​ x1 = (-b + sqrt(D))/2*a
6.​ x2 = (-b - sqrt(D))/2*a
7.​ PRINT “Roots are {x1} , {x2}”
8.​ PRINT “Imaginary roots”
9.​ STOP
#include <stdio.h>
#include <conio.h>
#include <math.h> // for sqrt()

void main()
{
float a, b, c, D, x1, x2;

printf("Enter coefficients a, b and c: ");


scanf("%f %f %f", &a, &b, &c); // Input

D = (b * b) - (4 * a * c); // Discriminant

if (D > 0)​ //Calculating Real Roots


{
x1 = (-b + sqrt(D)) / (2 * a);
x2 = (-b - sqrt(D)) / (2 * a);
printf("Roots are real. \n");
printf("x1 = %.2f and x2 = %.2f\n", x1, x2);
}

else
{
printf("Imaginary roots");
}

getch();
QUESTION: Write a C program to find the sum of individual digits of a 3-digit number

Algorithm:
1.​ START
2.​ INPUT a
3.​ dup = a
4.​ IF (a>99 && a<1000), go to step 5. ELSE go to step 11.
5.​ IF dup > 0, go to step 6. ELSE go to step 10.
6.​ dig=dup%10
7.​ sum = sum + dig
8.​ dup=dup/10
9.​ Go to step 5.
10.​PRINT “Sum = {sum}”
11.​PRINT “Not a 3-digit number”
12.​STOP

#include <stdio.h>
#include <conio.h>
void main()
{
int a, sum = 0, digit, dup;
printf("Enter number");
scanf("%d", &a);
dup = a;
if (a > 99 && a < 1000)
{

while (dup > 0)


{
digit = dup % 10;
sum = sum + digit;
dup = dup / 10;
}
}
else
{
printf("Not a 3-digit number ");
}
printf("Sum: %d", sum);
getch();
}
QUESTION: Write a program to count the number of digits in a given integer

Algorithm:
1.​ START
2.​ INPUT a
3.​ count=0
4.​ IF a>0, go to step 4, else go to step 8.
5.​ dig = a%10
6.​ count = count +1;
7.​ a=a/10
8.​ Go to step 3
9.​ PRINT count
10.​STOP

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

void main()
{
int a, dig, count = 0;
printf("Enter number");
scanf("%d", &a);
while (a > 0)
{
dig = a % 10;
count++;
a = a / 10;
}

printf("Number of digits: %d", count);


getch();
}
QUESTION: Write a menu driven program to perform simple arithmetic operations based on the user's
choice. The user will indicate the operation to be performed using the signs e.g. + for addition, etc.
Write an algorithm and draw flowchart for same

Algorithm:
1.​ START
2.​ INPUT a,b
3.​ INPUT choice
4.​ IF choice = '+' then PRINT “Addition: {a+b}”. Go to step 10.
5.​ ELSE IF choice = '-' then PRINT “Subtraction: {a+b}”. Go to step 10.
6.​ ELSE IF choice = '*' then PRINT “Multiplication: {a*b}”. Go to step 10.
7.​ ELSE IF choice ‘/’ then go to step 8.
8.​ IF b==0, then, PRINT “Invalid”, ELSE PRINT “Division: {a/b}”. Go to step 10.
9.​ ELSE, PRINT “Invalid Input”
10.​STOP

#include <stdio.h>
#include <conio.h>
void main()
{
int a, b;
char choice;
printf("Enter two numbers");
scanf("%d %d", &a, &b);
printf("Enter operator");
choice = getch();

switch (choice)
{
case '+':
printf("Addition: %d", a + b);
break;
case '-':
printf("Subtraction: %d", a - b);
break;
case '*':
printf("Multiplication: %d", a * b);
break;
case '/':
if (b == 0)
{
printf("Invalid");
}
else
{
printf("Division: %f", a / b);
}
break;
default:
printf("Invalid Input");
break;
}
getch();
}
QUESTION: Write a program to read a number of more than one digit, reverse the number and display the
sum of digits of numbers. Write an algorithm and draw flowchart for the same.

Algorithm:
1.​ START
2.​ INPUT a
3.​ dup = a
4.​ IF dup>0, go to step 5. ELSE go to step 10.
5.​ dig=dup%10
6.​ rev = rev*10+dig
7.​ sum = sum + digit
8.​ dup = dup/10
9.​ Go to step 4.
10.​PRINT “Reverse: {rev}”
11.​PRINT “Sum: {sum}”
12.​STOP

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

void main()
{
int a, rev = 0, sum=0, digit,dup;
printf("Enter number");
scanf("%d", &a);
dup=a;
while (dup > 0)
{
digit = dup % 10;
rev = rev * 10 + digit;
sum = sum + digit;
dup = dup / 10;
}

printf("Reverse: %d \n", rev);


printf("Sum: %d",sum);
getch();
}
QUESTION: Write programs to display each of the following patterns. Write algorithm and draw flowchart for
the same. A) 1 B) A 2 1 ABA 3 2 1 ABCBA 4 3 2 1 ABCDCBA 5 4 3 2 1 ABCDEDCBA

Algorithm:
(a)
1.​ START
2.​ i=1
3.​ IF i<6, go to step 4, ELSE go to step 1.
4.​ j=i
5.​ IF j>0, PRINT j.
6.​ j=j-1
7.​ Go to step 5
8.​ PRINT “\n”
9.​ i=i+1
10.​Go to step 3
11.​STOP

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

void main()
{
for (int i = 1; i < 6; i++)
{
for (int j = i; j > 0; j--)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}
(b)
1.​ START
2.​ i=1
3.​ IF i<6, go to step 4. ELSE go to 13.
4.​ j=65
5.​ IF j<65 + i, go to step 6. ELSE go to step 9.
6.​ PRINT “ { j } “
7.​ j=j+1
8.​ Go to step 5
9.​ k=63+i
10.​IF k>64, go to step 11. ELSE go to step 13.
11.​PRINT “ {k} “
12.​k=k-1
13.​i=i+1
14.​Go to step 3
15.​PRINT “\n”
16.​STOP

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

void main()
{
for (int i = 1; i < 6; i++)
{
for (int j = 65; j < 65 + i; j++)
{
printf("%c ", j);
}
for (int k = 63+i; k > 64; k--)
{
printf("%c ", (k));
}
printf("\n");
}
getch();
}

You might also like