C_CPP_Codes
114 subscribers
1 photo
About me :- alokosx.me

Books,notes and more at :- cp_resources.t.me

Discussion :- c_cpp_code.t.me
Download Telegram
Code to calculate circumference and area of circle by taking user input :
#include <stdio.h>
#define PI 3.14159
int main(void)
{
double radius;
printf("Enter radius:");
scanf("%lf",&radius);
printf("\n");
printf("Circumference of the circle whose radius is %lf is %lf & its area is %lf\n",radius,2*PI*radius,PI*radius*radius);
return 0;
}
Output :

Enter radius:10

Circumference of the circle whose radius is 10.000000 is 62.831800 & its area is 314.159000
Code to convert miles and yards to kilometers :
#include <stdio.h>

int main()
{
int miles,yards;
double kilometers;
printf("Enter miles,yards:");
scanf("%d%d",&miles,&yards);
kilometers = 1.609*(miles+yards/1760.0);
printf("\n%d miles & %d yards equals %lf kilometers.\n",miles>
return 0;
}
Output :

Enter miles,yards:10 250

10 miles & 250 yards equals 16.318551 kilometers.
Code to print sin of a number(in radian) between 0 and 1 using math.h:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a;
printf("Enter any number between 0 and 1:\n");
scanf("%d",&a);
sin_a = sin(a);
printf("Sin of %lf is %lf",a,sin_a)
Output :

Enter any number between 0 and 1: 0.5

sin(0.500000 rad) is 0.479426
Short circuit evaluation using if-else flow of control :
#include <stdio.h>
int main(void)
{
int outside,weather;
printf("\nEnter if outside 1(for yes) or 0(for no):");
scanf("%d",&outside);
printf("Enter if its raining(1) or not(0):");
scanf("%d",&weather);
if(outside && weather)
printf("\nPlease use an umbrella.\n");
else
printf("\nDress normally.\n");
return 0;
}
Output :
1)
Enter if outside 1(for yes) or 0(for no):1
Enter if its raining(1) or not(0):0

Dress normally.


2)
Enter if outside 1(for yes) or 0(for no):1
Enter if its raining(1) or not(0):1

Please use an umbrella.
Program to count number of blanks,digits,letters and other characters in a file using while loop :
#include <stdio.h>

int main(void)
{
int blanks = 0, digits = 0, letters = 0, others = 0;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
++blanks;
else if (c >= '0' && c <= '9')
++digits;
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
++letters;
else
++others;
}

printf("blanks = %d,\n digits = %d,\n letters = %d,\n others = %d.\n\n", blanks, digits, letters, others);

return 0;
}
Output :

$ ./count.out < count.c
$ blanks = 184,
digits = 7,
letters = 197,
others = 135.
Program to count number of blanks,digits,letters,other characters and total characters in a file using for loop :
#include <stdio.h>

int main(void)
{
    int blanks = 0, digits = 0, letters = 0, others = 0,totalchars = 0;
    int c;

    for(;(c = getchar())!=EOF;totalchars++)
    {
        if (c == ' ')
            ++blanks;
        else if (c >= '0' && c <= '9')
            ++digits;
        else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            ++letters;
        else
            ++others;
    }

    printf(" Blanks = %d,\n Digits = %d,\n Letters = %d,\n Others = %d\n Total chars = %d\n\n", blanks, digits, letters, others, totalchars);

    return 0;
}
Output :

$ ./count.out <count.c

Blanks = 189,
Digits = 8,
Letters = 237,
Others = 145
Total chars = 579
Program to levy speeding ticket using ternary operator :
#include <stdio.h>
int main(void)
{
int speed;
printf("\nEnter speed as integer:");
scanf("%d",&speed); speed = (speed<=65)?(65):(speed<=70)?(70):(90);
switch(speed)
{
case 65: printf("\nNo speeding ticket.\n");break;
case 70: printf("\nSpeeding ticket.\n");break;
case 90: printf("\nExpensive speeding ticket.\n");break;
default: printf("\nIncorrect speed.\n");break;
}
return 0;
}
Output :

1)
Enter speed as integer:99

Expensive speeding ticket.

2)
Enter speed as integer:16

No speeding ticket.
Program to print values of sin and cos between 0 and 1 using for loop :
#include <stdio.h>
#include <math.h> /* has sin(),abs(),and fabs() */
int main(void)
{
double interval;
int i; double sine,cosine;
for(i=0;i<=10;i++) // taking values between 0 and 1 as range
{
interval = i/10.0;
sine = sin(interval);
cosine = cos(interval);
/* here in above lines we called fabs function as interval is
in float */
printf("sin(%0.3lf) = %0.3lf | cos(%0.3lf) = %0.3lf\n",in>


}
return 0;
}
Output :

sin(0.000) = 0.000 | cos(0.000) = 1.000
sin(0.100) = 0.100 | cos(0.100) = 0.995
sin(0.200) = 0.199 | cos(0.200) = 0.980
sin(0.300) = 0.296 | cos(0.300) = 0.955
sin(0.400) = 0.389 | cos(0.400) = 0.921
sin(0.500) = 0.479 | cos(0.500) = 0.878
sin(0.600) = 0.565 | cos(0.600) = 0.825
sin(0.700) = 0.644 | cos(0.700) = 0.765
sin(0.800) = 0.717 | cos(0.800) = 0.697
sin(0.900) = 0.783 | cos(0.900) = 0.622
sin(1.000) = 0.841 | cos(1.000) = 0.540
👍1
Program to create a void function in C to add 2 int :
#include <stdio.h>
void myfunc(int x,int y){
int sum;
sum = x+y;
printf("%d",sum);
}

int main(void)
{ int x = 10,y = 20;
  myfunc(x,y);
}
Output :

30
👍2
Program to define a function with return statement :
#include <stdio.h>
int myfunc(int x,int y)
{
return x+y;
}

int main(void)
{ int x = 10,y = 20;
  printf("%d",myfunc(x,y));
}
Output :
30
Function to print square and cubes of a given range of numbers :
#include <stdio.h>
int square(int);
int cube(int);
int main(void)
{
int end = 0,i,j;
printf("I want squares and cubes from 1 to:");
scanf("%d",&end);
printf("\nNumber\tSquare\tCube\n");
for(i=1;i<=end;i++)
{
printf("\n %d \t %d \t %d",i,square(i),cube(i));
}
printf("\n\n");
return 0;
};

int square(int x)
{
return x*x;
}

int cube(int x)
{
return x*x*x;
}
Output :

I want squares and cubes from 1 to:10

Number Square Cube

1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
Program to print Fibonacci series till 10th term using iteration :
#include <stdio.h>
long fibonacci(int n)
{
long f2 = 0,f1 = 1,f_old;
int i;
for(i =0;i<n;i++)
{
  f_old = f2;
  f2 = f2+f1;
  f1 = f_old;
}
return f2;
}

int main(void)
{
int n;
for(n=1;n<=10;n++)
{
  printf("%d term = %ld\n",n,fibonacci(n));
}
return 0;
}
Output :

1 term = 1
2 term = 1
3 term = 2
4 term = 3
5 term = 5
6 term = 8
7 term = 13
8 term = 21
9 term = 34
10 term = 55
Program to print fibonacci series using recursion :
#include <stdio.h>
long recursive_fibonacci(int n)
{
if(n<=1)
  return n;
else
  return(recursive_fibonacci(n-1)+ recursive_fibonacci(n-2));
}

int main(void)
{
int n;
for(n=1;n<=10;n++)
{
printf("%d term = %ld\n",n,recursive_fibonacci(n));
}
return 0;
}
Output :

1 term = 1
2 term = 1
3 term = 2
4 term = 3
5 term = 5
6 term = 8
7 term = 13
8 term = 21
9 term = 34
10 term = 55
Program to print factorial upto n using recursion :
#include <stdio.h>
long int factorial(int n)
{
if(n == 0 | n == 1)
return 1;
else
return(factorial(n-1)*n);
}

int main(void)
{
int how_many = 0,i;
printf("I want table of factorial upto :");
scanf("%d",&how_many);

for(i=0;i<=how_many;i++)
printf("\n%d!\t=\t%ld\n",i,factorial(i));
return 0;
}
Output :

I want table of factorial upto :10

0! = 1

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

7! = 5040

8! = 40320

9! = 362880

10! = 3628800
Program to initialize pointer and print its adress in hex and decimal format :
#include <stdio.h>
int main(void)
{
int a = 10,*p = &a;
printf("Adress of pointer is %p or %lu  and is pointed to %d",p,p,*p);
return 0;
}
Output :

Adress of pointer is 0x7fffe569a6f4 or 140737042294516  and is pointed to 10
Program to swap value of 2 variables using pointers :
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int main(void)
{
int a = 10,b = 20;
printf("Before swap:\n%d = \t%p or %lu \n%d = \t%p or %lu\n\n",a,&a,&a,b,&b,&b);
swap(&a,&b);
printf("After swap:\n%d = \t%p or %lu\n%d = \t%p or %lu\n",a,&a,&a,b,&b,&b);
}
Output :

Before swap:
10 = 0x7fe7e83c9c or 549351603356
20 = 0x7fe7e83c98 or 549351603352

After swap:
20 = 0x7fe7e83c9c or 549351603356
10 = 0x7fe7e83c98 or 549351603352
Program to take input of number of subject and grades from user in a array and calculate percentage:
#include <stdio.h>
int main(void)
{
int SIZE;
printf("Enter number of subjects:\n");
scanf("%d",&SIZE);
int grades[SIZE];
double sum = 0.0;
int i;
printf("\nEnter your grades:\n");
for(i = 0; i<SIZE; i++)
  scanf("%d",&grades[i]);
printf("\nYour grades are:\n");
for(i = 0; i<SIZE; i++)
  printf("%d\t",grades[i]);
printf("\n\n");
                                                                       for(i = 0; i<SIZE; i++)
sum += grades[i];
printf("Your percentage is %.2f %\n\n",sum/SIZE);
return 0;
}
Output :

Enter number of subjects:
5

Enter your grades:
98 96 94 92 90

Your grades are:
98      96      94      92      90

Yout percentage is 94.00 %
Program to bubble sort a array of numbers :
#include <stdio.h>
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

void print_array(int how_many,int data[],char *str)
{
int i;
printf("%s",str);

for(i = 0;i < how_many;i++)
printf("%d\t",data[i]);
}

void bubble(int data[],int how_many)
{
int i,j;
int go_on;
for(i=0 ; i<how_many ;i++)
{
print_array(how_many,data,"\nInside bubble\n");
printf("\nEnter a number to continue : ");
scanf("%d",&go_on);
for(j=how_many-1;j>i;j--)
if(data[j-1] > data[j])
swap(&data[j-1],&data[j]);
}
}

int main(void)
{
int SIZE;
printf("Enter no. of numbers you want to sort:");
scanf("%d",&SIZE);
int num[SIZE];
printf("Enter %d digits which you want to sort:\n",SIZE);
for(int i = 0; i<SIZE ; i++)
scanf("%d",&num[i]);
print_array(SIZE,num,"Numbers you entered:\n");
printf("\n\n");
bubble(num,SIZE);
print_array(SIZE,num,"\n\nSorted numbers:\n");
printf("\n\n");
return 0;
}
Output :

Enter no. of numbers you want to sort:5
Enter 5 digits which you want to sort:
27 91 63 29 15
Numbers you entered:
27 91 63 29 15


Inside bubble
27 91 63 29 15

Enter a number to continue : 0

Inside bubble
15 27 91 63 29

Enter a number to continue : 2

Inside bubble
15 27 29 91 63

Enter a number to continue : 9

Inside bubble
15 27 29 63 91

Enter a number to continue : 2

Inside bubble
15 27 29 63 91

Enter a number to continue : 4


Sorted numbers:
15 27 29 63 91
1
Program to merge sort 2 arrays :
#include <stdio.h>
void print_array(int how_many,int data[],char *str)
{
int i;

printf("%s",str);

for(i=0;i<how_many;i++)
printf("%d\t",data[i]);
}

void merge(int a[],int b[],int c[],int how_many)
{
int i=0,j=0,k=0;

while(i<how_many && j<how_many)
if(a[i]<b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
while(i<how_many)
c[k++] = a[i++];
while(j<how_many)
c[k++] = b[j++];
}

int main(void)
{
const int SIZE = 5;
int a[SIZE] = {45,57,69,81,93};
int b[SIZE] = {44,56,68,80,92};
int c[2*SIZE];
print_array(SIZE,a,"My grades are:\n");
printf("\n\n");
print_array(SIZE,b,"More grades:\n");
merge(a,b,c,SIZE);
printf("\n\n");
print_array(2*SIZE,c,"My sorted grades are:\n");
printf("\n\n");
}
Output :

My grades are:
45 57 69 81 93

More grades:
44 56 68 80 92

My sorted grades are:
44 45 56 57 68 69 80 81 92 93
Program to sort a array given by user using merge sort algorithm :
#include <stdio.h>
void print_array(int how_many,int data[],char *str)
{
int i;

printf("%s",str);

for(i=0;i<how_many;i++)
printf("%d\t",data[i]);
}

void merge(int a[],int b[],int c[],int how_many)
/* here a and b are of same size */
{
int i=0,j=0,k=0;

while(i<how_many && j<how_many)
if(a[i]<b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
while(i<how_many)
c[k++] = a[i++];
while(j<how_many)
c[k++] = b[j++];
}

void mergesort(int key[],int how_many)
/* how_many is in power of 2*/
{
int j,k;
int w[how_many];

for(k=1;k<how_many;k*=2)
{
for(j=0;j<how_many;j+=2*k)
merge(key+j,key+j+k,w+j,k);
for(j=0;j<how_many;j++)
key[j] = w[j];
}
}

int main(void)
{
int size,i;
printf("Enter size of array:");
scanf("%d",&size);
int a[size];
printf("Enter %d numbers:\n",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
print_array(size,a,"Numbers you entered:\n\n");
printf("\n\n");
mergesort(a,size);
print_array(size,a,"Sorted numbers:\n\n");
printf("\n\n");
}
Output :

Enter size of array:8
Enter 8 numbers:
25 72 19 71 10 72 92 93
Numbers you entered:

25 72 19 71 10 72 92 93

Sorted numbers:

10 19 25 71 72 72 92 93
Program to define enum and print today and next day :
#include <stdio.h>
enum day{sun,mon,tue,wed,thr,fri,sat};
typedef enum day day;

void print_day(day d)
{
switch(d)
{
case sun:printf("Sunday\n");break;
case mon:printf("Monday\n");break;
case tue:printf("Tuesday\n");break;
case wed:printf("Wednesday\n");break;
case thr:printf("Thrusday\n");break;
case fri:printf("Friday\n");break;
case sat:printf("Saturday\n");break;
default :printf("This is not a valid day\n");break;
}
}

day next_day(day d)
{
return((d+1)%7);
}
int main(void)
{
day today = sun;
print_day(today);
print_day(next_day(today));
print_day(10);
return 0;
}
Ouput :

Sunday
Monday
This is not a valid day