C++ Codes - Basic to Advanced π»
Open in Telegram
π‘ Daily C++ Codes with Output & Logic Any queries, message π @sai7981 π€ Get instant code explanations: @cpp_codes_bot π Join now: @cpp_code_snippets #cpp #dsa #coding #programming
Show more3 188
Subscribers
No data24 hours
+357 days
+12030 days
Posts Archive
// Program to find power of any number using recursion
#include<stdio.h>
int power(int m,int n)
{
if(n==0)
{
return 1;
}
if(n%2 == 0)
{
return power(m*m,n/2);
}
else
{
return m*power(m*m,(n-1)/2);
}
}
int main()
{
int base,exp,res;
printf("Enter the Base and Exponent:");
scanf("%d%d",&base,&exp);
res=power(base,exp);
printf("%d ^ %d =%d",base,exp,res);
return 0;
}
// Program to find factorial of a number using recursion
#include<stdio.h>
int fact (int n)
{
if(n==0)
{
return 1;
}
return fact(n-1)*n;
}
int main()
{
int res,num;
printf("Enter any number:");
scanf("%d",&num);
res=fact(num);
printf("The factorial of the number %d is %d",num,res);
return 0;
}
// Program to calculate sum of n natural numbers using recursion
#include<stdio.h>
int nSum(int n)
{
if (n==0)
{
return 0;
}
return nSum(n-1)+n;
}
int main()
{
int n,res;
printf("Enter the value for n:");
scanf("%d",&n);
res=nSum(n);
printf("Sum of %d natural numbers : %d",n,res);
return 0;
}
// Program to return pointer to a structure from the function
#include<stdio.h>
#include<stdlib.h>
struct Point
{
int x;
int y;
};
struct Point* assign(int a ,int b)
{
struct Point *ptr;
ptr=(struct Point *)malloc(sizeof(struct Point));
ptr->x=a;
ptr->y=b;
return ptr;
}
void print(struct Point *print)
{
printf("x:%d y:%d",print->x,print->y);
}
int main()
{
struct Point *p;
int x,y;
printf("Enter the values of x and y:");
scanf("%d%d",&x,&y);
p=assign(x,y);
print(p);
free(p);
return 0;
}
// Program to return structure variable from the function
#include<stdio.h>
struct Point
{
int x;
int y;
};
struct Point modify(struct Point s)
{
s.x=s.x+10;
s.y=s.y+20;
return s;
}
void print(struct Point a)
{
printf("x:%d y:%d\n",a.x,a.y);
}
int main()
{
struct Point p1,p2;
printf("Enter the values for x and y:");
scanf("%d%d",&p1.x,&p1.y);
printf("Values of x and y before modification:\n");
print(p1);
p2=modify(p1);
printf("Values of x and y after modification:\n");
print(p2);
return 0;
}
// Program to pass pointer to structure as an argument to the function
#include<stdio.h>
struct Point
{
int x;
int y;
};
void print(struct Point *ptr)
{
printf("x:%d y:%d",ptr->x,ptr->y);
}
#include<stdio.h>
int main()
{
struct Point s,*ptrs;
printf("Enter the values of x and y:");
scanf("%d%d",&s.x,&s.y);
ptrs=&s;
print(ptrs);
return 0;
}
// Program to pass pointer to structure as an argument to the function
#include<stdio.h>
struct Point
{
int x;
int y;
};
void print(struct Point *ptr)
{
printf("x:%d y:%d",ptr->x,ptr->y);
}
#include<stdio.h>
int main()
{
struct Point s;
printf("Enter the values of x and y:");
scanf("%d%d",&s.x,&s.y);
print(&s);
return 0;
}
// Program to pass structure variable as argument to a function
#include<stdio.h>
struct Point
{
int x;
int y;
};
void print(struct Point s)
{
printf("x:%d y:%d",s.x,s.y);
}
int main()
{
struct Point p;
printf("Enter the values of x and y:");
scanf("%d%d",&p.x,&p.y);
print(p);
return 0;
}
// Program to pass structure members as arguments to the function and modify values using call by reference
#include<stdio.h>
struct Point
{
int x;
int y;
};
void modifyPoint(int *ptrx, int *ptry)
{
*ptrx=*ptrx+5;
*ptry=*ptry-5;
}
void print(int x,int y)
{
printf("The co-ordinates of point are x:%d , y=%d",x,y);
}
int main()
{
struct Point myPoint={5,15};
modifyPoint(&myPoint.x,&myPoint.y);
print(myPoint.x,myPoint.y);
return 0;
}
// Program to pass structure members as arguments to the function
#include<stdio.h>
struct Point
{
int x;
int y;
};
void print(int x,int y)
{
printf("The co-ordinates of point are x:%d , y=%d",x,y);
}
int main()
{
struct Point myPoint={10,20};
print(myPoint.x,myPoint.y);
return 0;
}
// program to store data dynamically using malloc and extend the existing data using realloc and print it .
#include<stdio.h>
#include<stdlib.h>
int main() {
int *p, n, i, k;
// Input the initial number of elements to be stored
printf("Enter the number of elements to be stored: ");
scanf("%d", &n);
// Allocate memory dynamically using malloc
p = (int*)malloc(n, sizeof(int));
// Check if memory allocation was successful
if (p == NULL) {
printf("Cannot allocate memory. Exiting...\n");
exit(1);
}
// Input initial elements from the user
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", p + i);
}
// Display the entered initial elements
printf("The entered elements are: ");
for (i = 0; i < n; i++) {
printf("%4d", *(p + i));
}
// Input the number of elements to extend
printf("\nEnter the number of elements you want to extend: ");
scanf("%d", &k);
// Extend the allocated memory using realloc
p = (int*)realloc(p, (n + k) * sizeof(int));
// Check if memory reallocation was successful
if (p == NULL) {
printf("Cannot allocate memory. Exiting...\n");
exit(1);
}
// Input the additional elements from the user
printf("Enter the next %d elements: ", k);
for (i = 0; i < k; i++) {
scanf("%d", (p + n + i));
}
// Display all entered elements after extending
printf("The entered elements after extending are: ");
for (i = 0; i < n + k; i++) {
printf("%4d", *(p + i));
}
// Free dynamically allocated memory
free(p);
return 0;
}
//Program to store data dynamically using calloc() and print it/
#include<stdio.h>
#include<stdlib.h>
int main() {
int *p, n, i;
// Input the number of elements to be stored
printf("Enter the number of elements to be stored: ");
scanf("%d", &n);
// Allocate memory dynamically using calloc
p = (int*)calloc(n, sizeof(int));
// Check if memory allocation was successful
if (p == NULL) {
printf("Memory allocation failed. Exiting...\n");
exit(1);
}
// Input elements from the user
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", p + i);
}
// Display the entered elements
printf("The entered elements are: ");
for (i = 0; i < n; i++) {
printf("%4d", *(p + i));
}
// Free dynamically allocated memory
free(p);
return 0;
}
//Program for storing data dynamically and print it.
#include<stdio.h>
#include<stdlib.h>
int main() {
int *p, n, i;
// Input the number of elements to be stored
printf("Enter the number of elements to be stored: ");
scanf("%d", &n);
// Allocate memory dynamically
p = (int*)malloc(n * sizeof(int));
// Check if memory allocation was successful
if (p == NULL) {
printf("Memory allocation failed. Exiting...\n");
exit(1);
}
// Input elements from the user
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", p + i);
}
// Display the entered elements
printf("The entered elements are: ");
for (i = 0; i < n; i++) {
printf("%4d", *(p + i));
}
// Free dynamically allocated memory
free(p);
return 0;
}
Static Memory Allocation- The process of allocating memory at compile time is called as Static memory allocation.
Dynamic Memory Allocation - The process of allocating memory at run time is called as Dynamic memory allocation.
// Program to understand declaration ,initialization and accessing a value in a variable using pointer
#include <stdio.h>
int main() {
// Pointer Declaration
int *ptr;
int num = 10;
ptr = # // 'ptr' now holds the memory address of the variable 'num' (Initializing)
// Accessing the value using a pointer
printf("Value of num: %d\n", num);
printf("Value of num using pointer: %d\n", *ptr);
// Modifying the value using a pointer
*ptr = 20; // Modifying the value indirectly through the pointer 'ptr'
printf("Updated value of num: %d\n", num);
return 0;
}
Pointer Definition:
A pointer is a variable that stores the memory address of another variable.
