SlideShare a Scribd company logo
1 of 13
1
2
3
4
5
Program to Find Roots of a Quadratic Equation
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart,imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a,&b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart,imagPart, realPart,imagPart);
}
6
return 0;
}
Fibonacci Series up to n terms
#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Armstrong Numbers Between Two Integers
#include <math.h>
#include <stdio.h>
int main() {
int low, high, i, temp1, temp2, rem, n = 0;
float result = 0.0;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d and %d are: ", low, high);
for (i = low + 1; i < high; ++i) {
temp2 = i;
temp1 = i;
// number of digits calculation
while (temp1 != 0) {
temp1 /= 10;
++n;
}
// result contains sum of nth power of its digits
while (temp2 != 0) {
rem = temp2 % 10;
result += pow(rem, n);
temp2 /= 10;
}
// check if i is equal to the sum of nth power of its digits
if ((int)result == i) {
7
printf("%d ", i);
}
// resetting the values
n = 0;
result = 0;
}
return 0;
}
Program to Check Prime Number
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Display Prime Numbers Between two Intervals
#include <stdio.h>
int main() {
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);
while (low < high) {
flag = 0;
8
// if low is a non-prime number, flag will be 1
for (i = 2; i <= low / 2; ++i) {
if (low % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}
Program to convert binary to decimal
#include <math.h>
#include <stdio.h>
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
#include <math.h>
#include <stdio.h>
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
9
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
Output
Enter a binary number: 110110111
110110111 in binary = 439
Program to convert decimal to binary
#include <math.h>
#include <stdio.h>
long long convert(int n);
int main() {
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %lld in binary", n, convert(n));
return 0;
}
long long convert(int n) {
long long bin = 0;
int rem, i = 1, step = 1;
while (n != 0) {
rem = n % 2;
printf("Step %d: %d/2, Remainder = %d, Quotient = %dn", step++, n, rem, n / 2);
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
Output
Enter a decimal number: 19
Step 1: 19/2, Remainder = 1, Quotient = 9
Step 2: 9/2, Remainder = 1, Quotient = 4
Step 3: 4/2, Remainder = 0, Quotient = 2
Step 4: 2/2, Remainder = 0, Quotient = 1
Step 5: 1/2, Remainder = 1, Quotient = 0
19 in decimal = 10011 in binary
For example we want to convert decimal number 525 in the octal.
Step 1: 525 / 8 Remainder : 5 , Quotient : 65
Step 2: 65 / 8 Remainder : 1 , Quotient : 8
10
Step 3: 8 / 8 Remainder : 0 , Quotient : 1
Step 4: 1 / 8 Remainder : 1 , Quotient : 0
So equivalent octal number is: 1015
That is (525)10 = (1015)8
Example 1: Program to Convert Decimal to Octal
#include <stdio.h>
#include <math.h>
int convertDecimalToOctal(int decimalNumber);
int main()
{
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));
return 0;
}
int convertDecimalToOctal(int decimalNumber)
{
int octalNumber = 0, i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Output
Enter a decimal number: 78
78 in decimal = 116 in octal
Example 2: Program to Convert Octal to Decimal
#include <stdio.h>
#include <math.h>
long long convertOctalToDecimal(int octalNumber);
int main()
{
int octalNumber;
11
printf("Enter an octal number: ");
scanf("%d", &octalNumber);
printf("%d in octal = %lld in decimal", octalNumber, convertOctalToDecimal(octalNumber));
return 0;
}
long long convertOctalToDecimal(int octalNumber)
{
int decimalNumber = 0, i = 0;
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;
return decimalNumber;
}
Output
Enter an octal number: 116
116 in octal = 78 in decimal
Hexadecimal number system: It is base 16 number system which uses the digits from 0 to 9 and A,
B, C, D, E, F.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Decimal to hexadecimal conversion method:
Following steps describe how to convert decimal to hexadecimal
Step 1: Divide the original decimal number by 16
Step 2: Divide the quotient by 16
Step 3: Repeat the step 2 until we get quotient equal to zero.
Equivalent binary number would be remainders of each step in the reverse order.
Decimal to hexadecimal conversion example:
12
For example we want to convert decimal number 900 in the hexadecimal.
Step 1: 900 / 16 Remainder : 4 , Quotient : 56
Step 2: 56 / 16 Remainder : 8 , Quotient : 3
Step 3: 3 / 16 Remainder : 3 , Quotient : 0
So equivalent hexadecimal number is: 384
That is (900)10 = (384)16
C Program
1. #include<stdio.h>
2. int main() {
3. long int decimalNumber,remainder,quotient;
4. int i=1,j,temp;
5. char hexadecimalNumber[100];
6. printf("Enter any decimal number: ");
7. scanf("%ld",&decimalNumber);
8. quotient = decimalNumber;
9. while(quotient!=0) {
10. temp = quotient % 16;
11. //To convert integer into character
12. if( temp < 10)
13. temp =temp + 48; else
14. temp = temp + 55;
15. hexadecimalNumber[i++]= temp;
16. quotient = quotient / 16;
17. }
18. printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumbe
r);
19. for (j = i -1 ;j> 0;j--)
20. printf("%c",hexadecimalNumber[j]);
21. return 0;
22.}
Output
1. Enter any decimal number: 45
2. Equivalent hexadecimal value of decimal number 45: 2D
Program to Check Palindrome
#include <stdio.h>
int main() {
13
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
// reversed integer is stored in reversedN
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
// palindrome if orignalN and reversedN are equal
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}
Power ofa Number Using the while Loop
#include <stdio.h>
int main() {
int base,exp;
long long result = 1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);
while (exp != 0) {
result *= base;
--exp;
}
printf("Answer = %lld", result);
return 0;
}
Output
Enter a base number: 3
Enter an exponent: 4
Answer = 81

More Related Content

What's hot (19)

Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Simple c program
Simple c programSimple c program
Simple c program
 
Ansi c
Ansi cAnsi c
Ansi c
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
C programming
C programmingC programming
C programming
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
C programms
C programmsC programms
C programms
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C lab programs
C lab programsC lab programs
C lab programs
 
C programs
C programsC programs
C programs
 

Similar to Program flowchart

Similar to Program flowchart (20)

7 functions
7  functions7  functions
7 functions
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C programm.pptx
C programm.pptxC programm.pptx
C programm.pptx
 
C lab programs
C lab programsC lab programs
C lab programs
 
Progr3
Progr3Progr3
Progr3
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
C important questions
C important questionsC important questions
C important questions
 
C
CC
C
 
C basics
C basicsC basics
C basics
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Hargun
HargunHargun
Hargun
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
C lab
C labC lab
C lab
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 

More from Sowri Rajan

More from Sowri Rajan (11)

Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Unitii classnotes
Unitii classnotesUnitii classnotes
Unitii classnotes
 
Uniti classnotes
Uniti classnotesUniti classnotes
Uniti classnotes
 

Recently uploaded

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

Program flowchart

  • 1. 1
  • 2. 2
  • 3. 3
  • 4. 4
  • 5. 5 Program to Find Roots of a Quadratic Equation #include <math.h> #include <stdio.h> int main() { double a, b, c, discriminant, root1, root2, realPart,imagPart; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a,&b, &c); discriminant = b * b - 4 * a * c; // condition for real and different roots if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); } // condition for real and equal roots else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); } // if roots are not real else { realPart = -b / (2 * a); imagPart = sqrt(-discriminant) / (2 * a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart,imagPart, realPart,imagPart); }
  • 6. 6 return 0; } Fibonacci Series up to n terms #include <stdio.h> int main() { int i, n, t1 = 0, t2 = 1, nextTerm; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for (i = 1; i <= n; ++i) { printf("%d, ", t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } return 0; } Armstrong Numbers Between Two Integers #include <math.h> #include <stdio.h> int main() { int low, high, i, temp1, temp2, rem, n = 0; float result = 0.0; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); printf("Armstrong numbers between %d and %d are: ", low, high); for (i = low + 1; i < high; ++i) { temp2 = i; temp1 = i; // number of digits calculation while (temp1 != 0) { temp1 /= 10; ++n; } // result contains sum of nth power of its digits while (temp2 != 0) { rem = temp2 % 10; result += pow(rem, n); temp2 /= 10; } // check if i is equal to the sum of nth power of its digits if ((int)result == i) {
  • 7. 7 printf("%d ", i); } // resetting the values n = 0; result = 0; } return 0; } Program to Check Prime Number #include <stdio.h> int main() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 2; i <= n / 2; ++i) { // condition for non-prime if (n % i == 0) { flag = 1; break; } } if (n == 1) { printf("1 is neither prime nor composite."); } else { if (flag == 0) printf("%d is a prime number.", n); else printf("%d is not a prime number.", n); } return 0; } Display Prime Numbers Between two Intervals #include <stdio.h> int main() { int low, high, i, flag; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); printf("Prime numbers between %d and %d are: ", low, high); while (low < high) { flag = 0;
  • 8. 8 // if low is a non-prime number, flag will be 1 for (i = 2; i <= low / 2; ++i) { if (low % i == 0) { flag = 1; break; } } if (flag == 0) printf("%d ", low); ++low; } return 0; } Program to convert binary to decimal #include <math.h> #include <stdio.h> int convert(long long n); int main() { long long n; printf("Enter a binary number: "); scanf("%lld", &n); printf("%lld in binary = %d in decimal", n, convert(n)); return 0; } int convert(long long n) { int dec = 0, i = 0, rem; while (n != 0) { rem = n % 10; n /= 10; dec += rem * pow(2, i); ++i; } return dec; } #include <math.h> #include <stdio.h> int convert(long long n); int main() { long long n; printf("Enter a binary number: "); scanf("%lld", &n); printf("%lld in binary = %d in decimal", n, convert(n)); return 0; } int convert(long long n) { int dec = 0, i = 0, rem;
  • 9. 9 while (n != 0) { rem = n % 10; n /= 10; dec += rem * pow(2, i); ++i; } return dec; } Output Enter a binary number: 110110111 110110111 in binary = 439 Program to convert decimal to binary #include <math.h> #include <stdio.h> long long convert(int n); int main() { int n; printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %lld in binary", n, convert(n)); return 0; } long long convert(int n) { long long bin = 0; int rem, i = 1, step = 1; while (n != 0) { rem = n % 2; printf("Step %d: %d/2, Remainder = %d, Quotient = %dn", step++, n, rem, n / 2); n /= 2; bin += rem * i; i *= 10; } return bin; } Output Enter a decimal number: 19 Step 1: 19/2, Remainder = 1, Quotient = 9 Step 2: 9/2, Remainder = 1, Quotient = 4 Step 3: 4/2, Remainder = 0, Quotient = 2 Step 4: 2/2, Remainder = 0, Quotient = 1 Step 5: 1/2, Remainder = 1, Quotient = 0 19 in decimal = 10011 in binary For example we want to convert decimal number 525 in the octal. Step 1: 525 / 8 Remainder : 5 , Quotient : 65 Step 2: 65 / 8 Remainder : 1 , Quotient : 8
  • 10. 10 Step 3: 8 / 8 Remainder : 0 , Quotient : 1 Step 4: 1 / 8 Remainder : 1 , Quotient : 0 So equivalent octal number is: 1015 That is (525)10 = (1015)8 Example 1: Program to Convert Decimal to Octal #include <stdio.h> #include <math.h> int convertDecimalToOctal(int decimalNumber); int main() { int decimalNumber; printf("Enter a decimal number: "); scanf("%d", &decimalNumber); printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber)); return 0; } int convertDecimalToOctal(int decimalNumber) { int octalNumber = 0, i = 1; while (decimalNumber != 0) { octalNumber += (decimalNumber % 8) * i; decimalNumber /= 8; i *= 10; } return octalNumber; } Output Enter a decimal number: 78 78 in decimal = 116 in octal Example 2: Program to Convert Octal to Decimal #include <stdio.h> #include <math.h> long long convertOctalToDecimal(int octalNumber); int main() { int octalNumber;
  • 11. 11 printf("Enter an octal number: "); scanf("%d", &octalNumber); printf("%d in octal = %lld in decimal", octalNumber, convertOctalToDecimal(octalNumber)); return 0; } long long convertOctalToDecimal(int octalNumber) { int decimalNumber = 0, i = 0; while(octalNumber != 0) { decimalNumber += (octalNumber%10) * pow(8,i); ++i; octalNumber/=10; } i = 1; return decimalNumber; } Output Enter an octal number: 116 116 in octal = 78 in decimal Hexadecimal number system: It is base 16 number system which uses the digits from 0 to 9 and A, B, C, D, E, F. Decimal number system: It is base 10 number system which uses the digits from 0 to 9 Decimal to hexadecimal conversion method: Following steps describe how to convert decimal to hexadecimal Step 1: Divide the original decimal number by 16 Step 2: Divide the quotient by 16 Step 3: Repeat the step 2 until we get quotient equal to zero. Equivalent binary number would be remainders of each step in the reverse order. Decimal to hexadecimal conversion example:
  • 12. 12 For example we want to convert decimal number 900 in the hexadecimal. Step 1: 900 / 16 Remainder : 4 , Quotient : 56 Step 2: 56 / 16 Remainder : 8 , Quotient : 3 Step 3: 3 / 16 Remainder : 3 , Quotient : 0 So equivalent hexadecimal number is: 384 That is (900)10 = (384)16 C Program 1. #include<stdio.h> 2. int main() { 3. long int decimalNumber,remainder,quotient; 4. int i=1,j,temp; 5. char hexadecimalNumber[100]; 6. printf("Enter any decimal number: "); 7. scanf("%ld",&decimalNumber); 8. quotient = decimalNumber; 9. while(quotient!=0) { 10. temp = quotient % 16; 11. //To convert integer into character 12. if( temp < 10) 13. temp =temp + 48; else 14. temp = temp + 55; 15. hexadecimalNumber[i++]= temp; 16. quotient = quotient / 16; 17. } 18. printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumbe r); 19. for (j = i -1 ;j> 0;j--) 20. printf("%c",hexadecimalNumber[j]); 21. return 0; 22.} Output 1. Enter any decimal number: 45 2. Equivalent hexadecimal value of decimal number 45: 2D Program to Check Palindrome #include <stdio.h> int main() {
  • 13. 13 int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if orignalN and reversedN are equal if (originalN == reversedN) printf("%d is a palindrome.", originalN); else printf("%d is not a palindrome.", originalN); return 0; } Power ofa Number Using the while Loop #include <stdio.h> int main() { int base,exp; long long result = 1; printf("Enter a base number: "); scanf("%d", &base); printf("Enter an exponent: "); scanf("%d", &exp); while (exp != 0) { result *= base; --exp; } printf("Answer = %lld", result); return 0; } Output Enter a base number: 3 Enter an exponent: 4 Answer = 81