KEMBAR78
C Programming Complete Revesion | PDF | Theoretical Computer Science | Computer Science
0% found this document useful (0 votes)
6 views4 pages

C Programming Complete Revesion

The document provides comprehensive notes and programs on C programming, covering topics such as operators, types of errors, control statements, loops, recursion, and arrays. It includes example code for a calculator, algorithms for finding the largest of three numbers, and various recursive and iterative functions for printing numbers, calculating factorials, and generating Fibonacci series. Additionally, it demonstrates input/output operations and calculations on arrays.

Uploaded by

accn60061
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)
6 views4 pages

C Programming Complete Revesion

The document provides comprehensive notes and programs on C programming, covering topics such as operators, types of errors, control statements, loops, recursion, and arrays. It includes example code for a calculator, algorithms for finding the largest of three numbers, and various recursive and iterative functions for printing numbers, calculating factorials, and generating Fibonacci series. Additionally, it demonstrates input/output operations and calculations on arrays.

Uploaded by

accn60061
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
You are on page 1/ 4

Complete C Programming Notes & Programs

Basics in C
1. Operators in C: Arithmetic, Relational, Logical, Bitwise, Assignment, Increment/Decrement,
Conditional, Special.
2. Types of Errors in C:
- Syntax Errors
- Logical Errors
- Runtime Errors

Control Statements - Calculator using switch-case


#include <stdio.h>
int main() {
char op;
double a, b;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
switch(op) {
case '+': printf("%.2lf", a + b); break;
case '-': printf("%.2lf", a - b); break;
case '*': printf("%.2lf", a * b); break;
case '/': if(b != 0) printf("%.2lf", a / b);
else printf("Division by zero error"); break;
default: printf("Invalid operator");
}
return 0;
}

Algorithm, Pseudocode & Flowchart - Largest of 3 numbers


Algorithm:
1. Start
2. Input a, b, c
3. If a > b and a > c, then a is largest
4. Else if b > a and b > c, then b is largest
5. Else c is largest
6. Stop

Pseudocode:
BEGIN
READ a, b, c
IF a>b AND a>c THEN
PRINT a
ELSE IF b>a AND b>c THEN
PRINT b
ELSE
PRINT c
END

Loops & Recursion - Print 1 to n using parameter


#include <stdio.h>
void printNumbers(int i, int n) {
if (i > n) return;
printf("%d ", i);
printNumbers(i + 1, n);
}
int main() {
int n;
scanf("%d", &n);
printNumbers(1, n);
return 0;
}

Loops & Recursion - Print n to 1 using parameter


#include <stdio.h>
void printReverse(int i) {
if (i == 0) return;
printf("%d ", i);
printReverse(i - 1);
}
int main() {
int n;
scanf("%d", &n);
printReverse(n);
return 0;
}

Loops & Recursion - Sum of 1 to n using (sum, n)


#include <stdio.h>
void sumOfNumbers(int sum, int n) {
if (n == 0) {
printf("Sum = %d\n", sum);
return;
}
sumOfNumbers(sum + n, n - 1);
}
int main() {
int n;
scanf("%d", &n);
sumOfNumbers(0, n);
return 0;
}

Loops & Recursion - Factorial of a Number


#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n-1);
}
int main() {
int n;
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
return 0;
}

Fibonacci Series - Using Recursion


#include <stdio.h>
int fibo(int n) {
if (n == 1) return 1;
if (n == 2) return 1;
return fibo(n-1) + fibo(n-2);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("%d ", fibo(i));
}
return 0;
}

Fibonacci Series - Without Recursion (Loop)


#include <stdio.h>
void fibo(int n) {
int a = 1, b = 1, c;
if (n >= 1) printf("%d ", a);
if (n >= 2) printf("%d ", b);
for (int i = 3; i <= n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
int main() {
int n;
scanf("%d", &n);
fibo(n);
return 0;
}

Arrays - Input and Output of Elements


#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

Arrays - Sum of Elements


#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum = %d", sum);
return 0;
}

Arrays - Largest Element


#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
}
printf("Largest = %d", max);
return 0;
}

You might also like