0% found this document useful (0 votes)
37 views20 pages

All Prgrams FPL

The document contains a series of algorithms and C programming examples covering various topics such as calculating the area of a circle, swapping numbers, finding the largest number among three, checking even or odd numbers, and working with operators. It includes detailed code snippets for increment/decrement, logical, relational, assignment, arithmetic operators, as well as functions for addition, checking even/odd, factorial, and power calculations. Additionally, it provides examples of array manipulation, string comparison, and matrix operations.

Uploaded by

yashsathe1357
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)
37 views20 pages

All Prgrams FPL

The document contains a series of algorithms and C programming examples covering various topics such as calculating the area of a circle, swapping numbers, finding the largest number among three, checking even or odd numbers, and working with operators. It includes detailed code snippets for increment/decrement, logical, relational, assignment, arithmetic operators, as well as functions for addition, checking even/odd, factorial, and power calculations. Additionally, it provides examples of array manipulation, string comparison, and matrix operations.

Uploaded by

yashsathe1357
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/ 20

Unit-1

Algorithm 1: Calculate area of circle


Step 1:BEGIN
Step 2: Accept the R
Step3: Find the square of R and store it in
A = R*R
Step4: Multiply R with 3.14 and store the result in A
Step5 :Display the value of A
Step6: END

Flowchart

Algorithm 2: Swap two numbers


Step 1 : Start
Step 2 : READ num1, num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : PRINT num1, num2
Step 7 : Stop

Flowchart:
Algorithm 3: Find largest number amongst three numbers
Step 1:BEGIN
Step2: Read values for A, B, C.
Step3: if A>B
Check if A>C if true then display A is Largest no.
Else
display C is Largest no.
Else
Check if B>C display B is Largest no.
Else
display C is Largest no.
Step4: END
Algorithm 4: Check whether a given number is even or odd.
Step 1 : Start
Step 2 : READ num
Step 3 : if num %2==0:
Display number is even number
Else
Display number is odd number

Step 4: Stop
Unit II: Operators and Expressions Programs

Q1. Working Of Increment and Decrement Operator.


Program:

#include <stdio.h>
int main() {
int a = 10;
printf("%d\n",a++); // a = a + 1 => a = 11
printf("%d\n",a--); // a = a - 1 => a = 10

int count = 5;
printf("%d\n", ++count); // prints 6 (prefix increment)
printf("%d\n", count--); // prints 6, then count becomes 5 (postfix decrement)

return 0;
}
Output:
10
11
6
6

Q2. Working of Logical Operator.


Program:
#include <stdio.h>
int main() {
int a = 1, b = 0;
printf("a && b: %d\n", a && b);
printf("a || b: %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}

Output:
a && b: 0
a || b: 1
!a: 0
Q3. Working of relational Operator.
Program:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a == b: %d\n", a == b); // 0 (false)
printf("a != b: %d\n", a != b); // 1 (true)
printf("a > b: %d\n", a > b); // 1 (true)
printf("a < b: %d\n", a < b); // 0 (false)
printf("a >= b: %d\n", a >= b); // 1 (true)
printf("a <= b: %d\n", a <= b); // 0 (false)
return 0;
}
Output:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0

Q4. Working of Assignment Operator.


Program:
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n)", a |= b);
printf("a >>= b: %d\n", a >> b);
printf("a <<= b: %d\n", a << b);

return 0;
}
Output:

a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a >>= b: 0
a <<= b: 160

Q5. Working Of Arithmetic Operator.


Program:

#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}

Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Unit 3
1.program to check whether the given number is Positive, negative or
zero.

#include<stdio.h>
void main()
{
int num;

printf("enter the number");


scanf("%d",&num);
if(num>0)
{
printf("the entered number is Positive");
}
else if(n <0)
printf("the entered number is negative");
else
printf("the entered number is zero");
}
2. Program to find the greater of two numbers
#include<stdio.h>

void main()
{
int num1,num2 ;

printf("enter the two numbers");


scanf("%d %d",&num1,&num2);
if(num1>num2)
{
printf("the number %d is big",num1);
}
else
{
printf("the number %d is big",num2);
}
}
3. C program to find the largest of three numbers
#include <stdio.h>

int main()
{
int a, b, c;
printf("Enter three numbers: \n ");
scanf("%d%d%d", &a,&b,&c);

if (a > b && a > c)


printf("Biggest number is %d", a);
if (b > a && b > c)
printf("Biggest number is %d", b);
if (c > a && c > b)
printf("Biggest number is %d", c);
return 0;
}

4. program to print day of the week

#include<stdio.h>
void main()
{
int day;

printf("enter the day number from 1 to7");


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("wrong input");
}
}
5. program to find the sum of digits of the given number (While loop)
#include<stdio.h>
void main()
{
int n,num,rem,sum=0;
printf("Enter the number");
scanf("%d",&num);
n=num;
while(num>0)
{
rem=num%10;
sum =sum+rem;
num=num/10;
}
printf("the sum of the digits of %d is =%d",n,sum);
}

6. Print numbers from 1 to 10 (Example of For loop)


#include <stdio.h>
int main()
{
int i;
for (i = 1; i < 11; ++i)

{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10

7. C Program to Check Whether a Number is Even or Odd

#include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);
// true if num is perfectly divisible by 2

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

Output:
Enter an integer: -7

-7 is odd.

8. C Program to Find Factorial of a Number

#include <stdio.h>

int main() {

int n, i, fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %d", n, fact);

}
return 0;
}

Output

Enter an integer: 5

Factorial of 5 = 120
Unit- 4
1. Write a program to find the largest element in an array of integers.

#include <stdio.h>
int main()
{
int arr[] = { 10, 324, 45, 90, 9807 };
int i;
int max = arr[0];

int n = sizeof(arr) / sizeof(arr[0]);


for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];

printf("%d", max );
return 0;
}

Output:
9807

2. Write a program to compare two strings using strcmp().

#include <stdio.h>
#include <string.h>
int main(){
char s1[5] = "Geeks";
char s2[5] = "Geeks";

printf("%d", strcmp(s1, s1));

return 0;
}

Output:
0

3. Write a program to multiply two matrices.


#include <stdio.h>

#define N 2 // Size of square matrices

int main() {
int matrix1[N][N] = {{1, 2}, {4, 5}};
int matrix2[N][N] = {{9, 8}, {6, 5}};
int result[N][N];
int i, j, k;

// Multiply the two matrices


for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
int sum = 0;
for (k = 0; k < N; k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
result[i][j] = sum;
}
}

// Print the resulting matrix


printf("Result Matrix:\n");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

Output
Result Matrix:
21 18
66 57

4. Write a program to addition two matrices.


#include <stdio.h>
#include <stdlib.h>
int main() {
int A[4][3] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4} };
int B[4][3] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4} };
int C[N][M];

printf("Result matrix is:\n");


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
C[i][j] = A[i][j] + B[i][j];
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}

Output:
Result matrix is:
222
444
666
888
5. C Program to Check for Palindrome String

#include <stdio.h>

#include <string.h>

int main() {

char string1[20];

int i, length;

int flag = 0;

printf("Enter a string: ");

scanf("%s", string1);

length = strlen(string1);

// Compare characters from the start and end of the string

// and stop if a mismatch is found or the middle of the string is reached.

for (i = 0; i < length / 2; i++) {

if (string1[i] != string1[length - i - 1]) {

flag = 1;

break;

if (flag) {

printf("%s is not a palindrome\n", string1);

} else {

printf("%s is a palindrome\n", string1);

return 0;

Output:

Enter a string: wow

Wow is a palindrome
Unit-5
1. Simple Function to Add Two Number

#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
int num1, num2, result;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

// Function call
result = add(num1, num2);

printf("The sum is: %d\n", result);

return 0;
}

// Function definition
int add(int a, int b) {
return a + b;
}

Output:

Enter two numbers: 5 10


The sum is: 15

2. Function to Check if a Number is Even or Odd

#include <stdio.h>

// Function declaration
void checkEvenOdd(int num);

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

// Function call
checkEvenOdd(number);
return 0;
}

// Function definition
void checkEvenOdd(int num) {
if (num % 2 == 0)
printf("%d is Even\n", num);
else
printf("%d is Odd\n", num);
}

Output:

Enter a number: 7
7 is Odd

3. Recursive Function for Factorial

#include <stdio.h>

// Function declaration
int factorial(int n);

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

// Function call
printf("Factorial of %d is: %d\n", num, factorial(num));

return 0;
}

// Recursive function definition


int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

Output:

mathematica
Enter a number: 5
Factorial of 5 is: 120
4. Function to Swap Two Numbers Using Call by Reference

#include <stdio.h>

// Function declaration
void swap(int *x, int *y);

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

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

// Function call
swap(&a, &b);

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

return 0;
}

// Function definition
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Output:

Enter two numbers: 3 7


Before swapping: a = 3, b = 7
After swapping: a = 7, b = 3

5. Function to Calculate the Power of a Number


#include <stdio.h>

// Function declaration
int power(int base, int exp);

int main() {
int base, exp;

printf("Enter base and exponent: ");


scanf("%d %d", &base, &exp);

// Function call
printf("%d to the power of %d is: %d\n", base, exp, power(base, exp));

return 0;
}

// Recursive function definition


int power(int base, int exp) {
if (exp == 0)
return 1;
else
return base * power(base, exp - 1);
}

Output:

Enter base and exponent: 2 3


2 to the power of 3 is: 8

6. C Program: Multiplication Table Using Recursion


#include <stdio.h>

// Function declaration
void printTable(int num, int multiplier);

int main() {
int number;

printf("Enter a number to print its multiplication table: ");


scanf("%d", &number);

// Start recursion with multiplier = 1


printTable(number, 1);

return 0;
}

// Recursive function definition


void printTable(int num, int multiplier) {
if (multiplier > 10) // Base condition
return;

printf("%d x %d = %d\n", num, multiplier, num * multiplier);

// Recursive call with incremented multiplier


printTable(num, multiplier + 1);
}

Enter a number to print its multiplication table: 5


5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

You might also like