0% found this document useful (0 votes)
6 views

C Programming File 4

If if g o. Khi

Uploaded by

jfjrcjvfgf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Programming File 4

If if g o. Khi

Uploaded by

jfjrcjvfgf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INDEX

PARTICULARS OF THE EXPERIMENTS PERFORMED


Date Of Signature
SL NO.
Name Of Experiment Page No. Experiment
1. WAP Simple program of
hello world. 03
2. WAP that takes user
input and then displays
output based on that
input. 04

3. Draw a flowchart of
while loop and do while
loop using Raptor and
Drakon Tool. 05
4. Write a programme for
addition, subtraction,
multiplication and
division 6-7
5. Write a program to check
whether the no. is odd or
even. 08
6. Write a simple
programme of calculator
by using switch
statement . 09
7. WAP to calculate the sum
of the first n natural
numbers. 10
8. Print a pascal triangle
patter of start. 11

pg. 1
9. Write a program to
calculate a factorial of
the number. 12
10. Write a recursive
function to find the
greatest common divisor
(GCD) of two numbers. 13
11. Write a program to
calculate the sum of
elements in a 1D array. 14
12. Write a program to
reverse a string. 15
13. Implement these sorting
algorithms on a 1D array. 16
14. Implement a binary
search function on a
sorted array. 17
15. Write a program to swap
two variables using
pointers. 18

pg. 2
Program No.1
WAP Simple program of hello world.

#include <stdio.h>
int main(){
printf("Hello World");
return 0;
}

OUT PUT
Hello World

pg. 3
Program No. 2
WAP that takes user input and then displays output based on that input.
include <stdio.h>
int main(){
int a;
printf("Enter integer value\n");
scanf("%d",&a);
printf("Your Value %d",a);
return 0;
}
OUT PUT
Enter integer value
5
Your Value 5

pg. 4
Program No. 3
Draw a flowchart of while loop and do while loop using Raptor and Drakon
Tool.

While loop Flowchart Do While Loop Flowchart

Start Start

Process
Condition

Condition
Process

End End

pg. 5
Program No. 4
Write a programme for addition, subtraction, multiplication and
division.
#include <stdio.h>
int main(){
float num1, num2;
char operation;
printf("Enter the first number: ");
scanf("%f", &num1);
printf("Enter the second number: ");
scanf("%f", &num2);
printf("Choose an operation (+, -, *, /): ");
scanf(" %c", &operation);
switch (operation) {
case '+':
printf("Result of addition: %.2f\n", num1 + num2);
break;
case '-':
printf("Result of subtraction: %.2f\n", num1 - num2);
break;
case '*':
printf("Result of multiplication: %.2f\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("Result of division: %.2f\n", num1 / num2);
} else {

pg. 6
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operation.\n");
}
return 0;
}

OUT PUT
Enter the first number: 56
Enter the second number: 65
Choose an operation (+, -, *, /): +
Result of addition: 121.00

pg. 7
Program No. 5
Write a program to check whether the no. is odd or even.

#include<stdio.h>
int main(){
int a;
printf("Enter Any One Number : ");
scanf("%d",&a);
if(a%2==0){
printf("Ths is Even Number");
}
else{
printf("This is Odd Number");
}
return 0;
}

OUT PUT
Enter Any One Number : 5
This is Odd Number

pg. 8
Program No. 6
Write a simple program of calculator by using switch statement
#include<stdio.h>
int main(){
int a,b;
printf("Enter any two number\n");
scanf("%d %d",&a, &b);
char symbol;
printf("Enter any symbol(+,-,*,/) for the calculate numbers ");
scanf(" %c", &symbol);
switch(symbol)
{
case '+':
printf("Sum of a and b %d :",a+b);
break;
case '-':
printf("Subtraction of a and b %d :",a-b);
break;
case '*':
printf("Multiplication of a and b %d:",a*b);
break;
case '/':
printf("Division of a and b %f :",a/b);
break;
default :
printf("Your value not found");
break;
}
return 0
}
OUT PUT
Enter any two number
67
90
Enter any symbol(+,-,*,/) for the calculate numbers +
Sum of a and b 157 :

pg. 9
Program No.7
Write a program to Calculate the sum of the first n natural numbers.
#include<stdio.h>

int main(){
int n,sum;

printf("Enter value of n 4");


scanf("%d",&n);

sum=(n*(n+1))/2;
printf(" The Sum first natural number: %d",sum);

return 0;
}

OUT PUT
Enter value of n 23
The Sum first natural number: 276

pg. 10
Program No.8
Print a pascal triangle pattern of start
#include <stdio.h>
void printSpaces(int num) {
for (int i = 0; i < num; i++) {
printf(" ");
}
}
void printPascalTriangle(int rows) {
for (int i = 0; i < rows; i++) {
printSpaces(rows - i - 1);
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}

int main() {
int n;
printf("Enter the number of rows for the Pascal's triangle: ");
scanf("%d", &n);
printPascalTriangle(n);
return 0;
}

OUT PUT

Enter the number of rows for the Pascal's triangle: 6


*
**
***
****
*****
******

pg. 11
Program No. 9
Write a program to calculate a factorial of the number
#include<stdio.h>
int facto(int num){
if(num<=1){
return 1;
}
return num*facto(num-1);
}
int main(){
int n;
printf("Enter the number for factorial ");
scanf("%d",&n);
if(n<0){
printf("Negative Numbers are not Defined");
}
else{
printf("The factorial of %d is %d",n,facto(n));
}
return 0;
}

OUT PUT
Enter the number for factorial 7
The factorial of 7 is 5040

pg. 12
Program N0. 10
Write a recursive function to find the greatest common divisor (GCD) of two
numbers.
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two numbers to find their GCD: ");
scanf("%d %d", &num1, &num2);
int result = gcd(num1, num2);
printf("GCD of %d and %d is %d\n", num1, num2, result);

return 0;
}

OUT PUT
Enter two numbers to find their GCD: 45
25
GCD of 45 and 25 is 5

pg. 13
Program No. 11
Write a program to calculate the sum of elements in a 1D array
#include<stdio.h>
int main(){
int n,sum=0;
printf("Enter the number of element ");
scanf("%d",&n);
int ele[n];
for(int i=0;i<n;i++){
printf("Enter %d Element ",i);
scanf("%d",&ele[i]);
}
for(int i=0;i<n;i++){
sum+=ele[i];
}
printf("sum of numbers is %d",sum);
return 0;
}

OUT PUT

Enter the number of element 5


Enter 0 Element 78
Enter 1 Element 76
Enter 2 Element 45
Enter 3 Element 23
Enter 4 Element 12
sum of numbers is 234

pg. 14
Program No. 12
Write a program to reverse a string

#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
// Swap characters from both ends
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}

OUTPUT

Enter a string: RAM


Reversed string: MAR

pg. 15
Program No. 13
Implement these sorting algorithms on a 1D array

#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array using Bubble Sort: \n");
printArray(arr, n);
return 0;
}

OUT PUT

Sorted array using Bubble Sort:


11 12 22 25 34 64 90

pg. 16
Program No. 14
Implement a binary search function on a sorted array

#include <stdio.h>
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
void printResult(int index) {
if (index != -1)
printf("Element found at index %d\n", index);
else
printf("Element not found in the array\n");
}
int main() {
int arr[] = {11, 22, 25, 34, 64, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 34;

int result = binarySearch(arr, n, target);


printResult(result);

return 0;
}

OUTPUT

Element found at index 3

pg. 17
Program No. 15
Write a program to swap two variables using pointers

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}

OUTPUT

Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5

pg. 18

You might also like