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
Channel created
Code to print single line in c :
#include <stdio.h>
int main(void)
{
 printf("Hello World!\n");
}
Output :

Hello World!
Code to print multiple lines in C :

#include <stdio.h>
int main(void)
{
printf("Some say the world will end in fire,\n");
printf("Some say in ice.\n");
printf("From what I’ve tasted of desire\n");
printf("I hold with those who favor fire.\n");
printf("But if it had to perish twice,\n");
printf("I think I know enough of hate\n");
printf("To say that for destruction ice is also great\n");
printf("And would suffice.\n");
}

• Output :

Some say the world will end in fire,
Some say in ice.
From what I’ve tasted of desire
I hold with those who favor fire.
But if it had to perish twice,
I think I know enough of hate
To say that for destruction ice is also great
And would suffice.
Code to take integer input in C :
#include <stdio.h>
int main(void)
{
int number;
printf("Enter a number:");
scanf("%d",&number);
printf("\n");
printf("Number you entered : %d\n",number);
return 0;
}
Output :

Enter a number: 16

Number you entered : 16
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.