C language basic to Advance
رفتن به کانال در Telegram
380
مشترکین
+724 ساعت
+437 روز
+17030 روز
آرشیو پست ها
/* Recursion in C involves a function calling itself directly or indirectly. Here's an example of a recursive function in C: */
#include <stdio.h>
void countdown(int n) {
if (n > 0) {
printf("%d ", n); // Print the current number
countdown(n - 1); // Call the function itself with a smaller number
}
}
int main() {
int num = 5;
countdown(num);
return 0;
}
// @C_code5
#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;
}
}
}
}
int main() {
int numbers[] = {6, 2, 10, 12, 89, 34, 99, 31, 5};
int n = sizeof(numbers) / sizeof(numbers[0]);
printf("Original list: \n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
bubbleSort(numbers, n);
printf("\n\nSorted list (ascending order): \n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
return 0;
} // @C_code5
print("Hi What's you think about this running Features say me on @bca_mca_btech group")
#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;
}
}
}
}
int main() {
int numbers[] = {6, 2, 10, 12, 89, 34, 99, 31, 5};
int n = sizeof(numbers) / sizeof(numbers[0]);
printf("Original list: \n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
bubbleSort(numbers, n);
printf("\n\nSorted list (ascending order): \n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
return 0;
} // @C_code5
// Series answer
#include <stdio.h>
#include <math.h>
int main() {
float ans = 1.0, temp = 1.0, x = 2.0;
int iterations = 3;
for (int i = 1; i < iterations; i++) {
temp = temp * x / i;
ans += temp;
}
printf("Result from series expansion: %f\n", ans);
float expResult = exp(x);
printf("Result from inbuilt function (exp()): %f\n", expResult);
if (fabs(ans - expResult) < 0.0001) {
printf("The results match.\n");
} else {
printf("The results do not match.\n");
}
return 0;
} // @C_code5
// Secant method for root finding
#include <stdio.h>
#include <math.h>
#define f(x) x*x*x -4*x +1
#define e 0.001
int main(){
float x0 = 0.1, x1=0.2, x2,f0, f1, f2;
do {
f0 = f(x0);
f1 = f(x1);
x2 = (x0 * f1- x1 * f0)/(f1 - f0);
f2 = f(x2);
x0 = x1;
x1 = x2;
f0 = f1;
f1 = f2;
printf("Root value = %f", x2);
printf("\nRoot function value = %f\n\n", f2);
} while(fabs(f2)>e);
return 0;
} // @C_code5
// bisection method for root finding
#include <stdio.h>
#include <math.h>
#define f(x) (x*x*x - 4*x + 1)
#define epsilon 0.01
int main() {
float x0 = 0.1, x1 = 0.2, x2, f0, f1, f2;
do {
f0 = f(x0);
f1 = f(x1);
x2 = (x0 + x1) / 2;
f2 = f(x2);
if (f0 * f2 < 0)
x1 = x2;
else
x0 = x2;
printf("Root: %f\n", x2);
printf("Func: %f\n\n", f2);
} while (fabs(f2) > epsilon && fabs(x1 - x0) > epsilon);
return 0;
} // @C_code5
// Regula falsi method for root finding
#include <stdio.h>
#include <math.h>
#define f(x) x*x*x -4*x +1
#define e 0.001
int main(){
float x0 = 0.1, x1=0.2, x2,f0, f1, f2;
do {
f0 = f(x0);
f1 = f(x1);
x2 = (x0 * f1- x1 * f0)/(f1 - f0);
f2 = f(x2);
if(f0 * f2 < 0){
x1 = x2;
f1 = f2;
} else {
x0 = x2;
f0 = f2;
}
printf("\n\nRoot value = %f", x2);
printf("\nRoot function value = %f", f2);
} while(fabs(f2)>e);
return 0;
} // @C_code5
// Eulars method
#include <stdio.h>
#include <math.h>
#define f(x, y) -x * y
int main(){
float x = 0, y = 1, h = 0.05, xn = 0.25, k;
while(x<xn) {
k = h * f(x, y);
y += k;
x += h;
printf("for x %.3f, ", x);
printf("Value of y is %.3f \n\n", y);
}
return 0;
} // @C_code5
Upper part is server code
And after that i run client program
See output 👆👆
server code: https://t.me/C_Code5/86
client code: https://t.me/C_Code5/87
For running this code on your computer
Copy server code make file
server.c and run it
Like this copy client code and paste it in client.c file and run it after running server code
(You can run this file using new terminal)
Note: always run server code first
@C_code5
Discussion: @bca_mca_btech
// Udp client program which send string and accept string (reversedString)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX_BUFFER_SIZE 1024
#define SERVER_IP "127.0.0.1"
#define PORT 8888
int main() {
int sock;
struct sockaddr_in server_addr;
char buffer[MAX_BUFFER_SIZE];
char message[] = "Hello, server!";
// Create UDP socket
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
// Configure server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_IP, &(server_addr.sin_addr)) <= 0) {
perror("Invalid address");
exit(EXIT_FAILURE);
}
// Send message to server
sendto(sock, (const char*)message, strlen(message), 0, (const struct sockaddr*)&server_addr, sizeof(server_addr));
printf("Message sent to server: %s\n", message);
int len, n;
// Receive reversed string from server
len = sizeof(server_addr);
n = recvfrom(sock, (char*)buffer, MAX_BUFFER_SIZE, MSG_WAITALL, (struct sockaddr*)&server_addr, &len);
buffer[n] = '\0';
printf("Reversed string from server: %s\n", buffer);
close(sock);
return 0;
} // @C_code5
// Udp server program which reverse string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX_BUFFER_SIZE 1024
#define PORT 8888
void reverseString(char* str) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
char temp = str[left];
str[left] = str[right];
str[right] = temp;
left++;
right--;
}
}
int main() {
int sock;
struct sockaddr_in server_addr, client_addr;
char buffer[MAX_BUFFER_SIZE];
// Create UDP socket
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
memset(&client_addr, 0, sizeof(client_addr));
// Configure server address
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind the socket to the specified address and port
if (bind(sock, (const struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Binding failed");
exit(EXIT_FAILURE);
}
printf("UDP Server Running on port %d...\n", PORT);
int len, n;
// Receive message from client
len = sizeof(client_addr);
n = recvfrom(sock, (char*)buffer, MAX_BUFFER_SIZE, MSG_WAITALL, (struct sockaddr*)&client_addr, &len);
buffer[n] = '\0';
printf("Received message from client: %s\n", buffer);
// Reverse the string
reverseString(buffer);
// Send the reversed string back to the client
sendto(sock, (const char*)buffer, strlen(buffer), 0, (const struct sockaddr*)&client_addr, len);
printf("Reversed string sent to client: %s\n", buffer);
close(sock);
return 0;
} // @C_code5
#include<stdio.h>
int main() {
int a,b,s,n,k=2;
printf("Number🫴");
scanf("%d",&n);
for(a=1; a<=n; a++) {
for(s=a*k; s<=20; s++) {
printf(" ");
}
for(b=1; b<=a*k; b++) {
if(b==k)
printf("%d ",a);
else if(b==a*k)
printf("%d",a);
else
printf(" ");
}
printf("\n");
}
for(a=n-1; a>=1; a--) {
for(s=22; s>=a*k; s--) {
printf(" ");
}
for(b=a*k; b>=1; b--) {
if(b==a*k)
printf("%d ",a);
else if(b==k)
printf("%d",a);
else printf(" ");
}
printf("\n");
}
return 0;
} // @C_code5
#include<stdio.h>
// Gauss seidel method for showing 2 iterations values
int main() {
float n[12];
printf("Gauss seidel method for showing 2 iterations value\n\n");
printf("Enter 3×4 matrix numbers:\n");
for(int i=0; i<12; i++)
scanf("%f", &n[i]);
printf("\nYour Equations are: \n");
for(int i=0;i<3;i++)
printf("%.0fa + %.0fb + %.0fc = %.0f\n", n[i], n[i+1], n[i+2], n[i+3]);
printf("\nFor iteration 1:\n");
printf("let a = b = c = 0\n");
float a = 0;
float b = 0;
float c = 0;
a = (n[3] - n[1]*b - n[2]*c)/n[0];
b = (n[7] - n[4]*a - n[6]*c)/n[5];
c = (n[11] - n[8]*a - n[9]*b)/n[10];
printf("\nlteration 1 values: a = %.4f, b = %.4f, c = %.4f \n\n", a, b, c);
// Now Iteration 2
a = (n[3] - n[1]*b - n[2]*c)/n[0];
b = (n[7] - n[4]*a - n[6]*c)/n[5];
c = (n[11] - n[8]*a - n[9]*b)/n[10];
printf("\nlteration 2 values: a = %.4f, b = %.4f, c = %.4f \n\n", a, b, c);
return 0;
} // @C_Code5
#include<stdio.h>
// Gauss jacobi method for showing 2 iterations value
int main() {
float n[12];
printf("Gauss jacobi method for showing 2 iterations value\n\n");
printf("Enter 3×4 matrix numbers:\n");
for(int i=0; i<12; i++)
scanf("%f", &n[i]);
printf("\nYour Equations are: \n");
for(int i=0;i<3;i++)
printf("%.0fa + %.0fb + %.0fc = %.0f\n", n[i], n[i+1], n[i+2], n[i+3]);
printf("\nlteration 1: \n");
printf("let a = b = c = 0\n");
float a = 0;
float b = 0;
float c = 0;
float aa = (n[3] - n[1]*b - n[2]*c)/n[0];
float bb = (n[7] - n[4]*a - n[6]*c)/n[5];
float cc = (n[11] - n[8]*a - n[9]*b)/n[10];
a = aa, b = bb, c = cc;
printf("\nlteration 1 values: a = %.4f, b = %.4f, c = %.4f \n\n", a, b, c);
// Iteration 2:
aa = (n[3] - n[1]*b - n[2]*c)/n[0];
bb = (n[7] - n[4]*a - n[6]*c)/n[5];
cc = (n[11] - n[8]*a - n[9]*b)/n[10];
a = aa, b = bb, c = cc;
printf("\nlteration 2 values: a = %.4f, b = %.4f, c = %.4f \n\n", a, b, c);
return 0;
} // @C_code5
#include <stdio.h>
// File Handling in c
int main(void) {
// ▬▬▬▬▬▬▬▬▬▬▬▬
// for write hello world in file
FILE *fp = fopen("file.txt", "w");
fprintf(fp, "Hello, world!");
fclose(fp);
// ▬▬▬▬▬▬▬▬▬▬▬▬
// for read file
fp = fopen("file.txt", "r");
char chars[256];
fgets(chars, 256, fp);
fclose(fp);
// ▬▬▬▬▬▬▬▬▬▬▬▬
// now printing data of file
printf("File contents: %s", chars);
// ▬▬▬▬▬▬▬▬▬▬▬▬
return 0;
} // @C_code5
how to learn any programming?
if(teacher available){
learn from them
if(doubt){
use yt etc
}
try{
Practice code
}
catch (many errors in Practice){
use yt etc for solution
}
} else {
use online platforms
if(understood){
try{
Create project
if(questions || doubts)
ask from gpt or in tg
}
catch (project not created){
Practice more
}
}
}
if (project created){
do something big
}
// how to learn anything 👆
@ignou_study_channel
for windows full path of myheader.h can be as given example in c file👇
#include "C:\\Desktop\\myheader.h"
For Linux 👇
#include "/home/sid/desktop/myheader.h"
🤡 Note that this is just example you must use your own file location
myheader.h file
int add(int x, int y) {
return x + y;
}
File1 👆👆👆 file2 👇👇👇👇
main.c file
#include <stdio.h>
// full path of file
#include "/home/sid/desktop/myheader.h"
int main() {
int result = add(2, 3);
printf("Result: %d", result);
return 0;
}
// thats how you can create your codes in separate file
