Code to print multiple lines in C :
• Output :
#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>Output :
int main(void)
{
int number;
printf("Enter a number:");
scanf("%d",&number);
printf("\n");
printf("Number you entered : %d\n",number);
return 0;
}
Enter a number: 16
Number you entered : 16Code to calculate circumference and area of circle by taking user input :
#include <stdio.h>Output :
#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;
}
Enter radius:10
Circumference of the circle whose radius is 10.000000 is 62.831800 & its area is 314.159000Code to convert miles and yards to kilometers :
#include <stdio.h>Output :
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;
}
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>Output :
#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)
Enter any number between 0 and 1: 0.5
sin(0.500000 rad) is 0.479426Short circuit evaluation using if-else flow of control :
1)
2)
#include <stdio.h>Output :
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;
}
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>Output :
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;
}
$ ./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>Output :
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;
}
$ ./count.out <count.c
Blanks = 189,
Digits = 8,
Letters = 237,
Others = 145
Total chars = 579Program to levy speeding ticket using ternary operator :
#include <stdio.h>Output :
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;
}
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>Output :
#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;
}
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 :
30
#include <stdio.h>Output :
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);
}
30
👍2
Program to define a function with return statement :
30
#include <stdio.h>Output :
int myfunc(int x,int y)
{
return x+y;
}
int main(void)
{ int x = 10,y = 20;
printf("%d",myfunc(x,y));
}
30
Function to print square and cubes of a given range of numbers :
#include <stdio.h>Output :
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;
}
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 1000Program to print Fibonacci series till 10th term using iteration :
#include <stdio.h>Output :
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;
}
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 = 55Program to print fibonacci series using recursion :
#include <stdio.h>Output :
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;
}
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 = 55Program to print factorial upto n using recursion :
#include <stdio.h>Output :
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;
}
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! = 3628800Program to initialize pointer and print its adress in hex and decimal format :
#include <stdio.h>Output :
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;
}
Adress of pointer is 0x7fffe569a6f4 or 140737042294516 and is pointed to 10Program to swap value of 2 variables using pointers :
#include <stdio.h>Output :
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);
}
Before swap:
10 = 0x7fe7e83c9c or 549351603356
20 = 0x7fe7e83c98 or 549351603352
After swap:
20 = 0x7fe7e83c9c or 549351603356
10 = 0x7fe7e83c98 or 549351603352Program to take input of number of subject and grades from user in a array and calculate percentage:
#include <stdio.h>Output :
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;
}
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>Output :
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;
}
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