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

Programs

programs

Uploaded by

Ramya Sivaraman
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 views7 pages

Programs

programs

Uploaded by

Ramya Sivaraman
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

Find the array elements and largest number in c using the pointer

#include <stdio.h>

// Function to find the largest number in an array


int findLargest(int *arr, int size) {
int largest = *arr; // Assume the first element is the largest

// Iterate through the array using pointers


for (int i = 1; i < size; i++) {
if (*(arr + i) > largest) {
largest = *(arr + i);
}
}

return largest;
}

int main() {
int size;

// Get the size of the array from the user


printf("Enter the size of the array: ");
scanf("%d", &size);

// Declare an array of the given size


int arr[size];

// Get array elements from the user


printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Display array elements


printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

// Find the largest number in the array


int largest = findLargest(arr, size);

// Display the largest number


printf("\nLargest number in the array: %d\n", largest);

return 0;
}

Fibonacci series using function concept

#include<stdio.h>
int fibo_num (int i)
{
// if the num i is equal to 0, return 0;
if ( i == 0)
{
return 0;
}
if ( i == 1)
{
return 1;
}
return fibo_num (i - 1) + fibonacci (i -2);
}
int main ()
{
int i;
// use for loop to get the first 10 fibonacci series
for ( i = 0; i < 10; i++)
{
printf (" %d \t ", fibo_num (i));
}
return 0;
}

Factorial using function

#include<stdio.h>

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

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

fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
Add two numbers using a pointer

#include <stdio.h>

int main() {
int num1 = 10;
int num2 = 20;
int *ptr1 = &num1;
int *ptr2 = &num2;
int sum;

sum = *ptr1 + *ptr2;

printf("Sum of %d and %d is: %d\n", *ptr1, *ptr2, sum);

return 0;
}
Swapping two numbers using pointers

// C program to swap two numbers using pointers


#include <stdio.h>

int main() {

int a, b, temp;
int *ptr1, *ptr2;

printf("Enter the value of a and b: ");


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

printf("\nBefore swapping a = %d and b = %d", a, b);

// Assign the memory address of a and b to *ptr1 and *ptr2


ptr1 = &a;
ptr2 = &b;

// Swap the values a and b


temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;

printf("\nAfter swapping a = %d and b = %d", a, b);

return 0;

}
Program to count the number of words in the string

#include <stdio.h>

#define MAX_SIZE 100 // Maximum string size

int main()

char str[MAX_SIZE];

int i, words;

/* Input string from user */

printf("Enter any string: ");

gets(str);

i = 0;

words = 1;

/* Runs a loop till end of string */

while(str[i] != '\0')

{
/* If the current character(str[i]) is white space */

if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')

words++;

i++;

printf("Total number of words = %d", words);

return 0;

You might also like