CHAPTER-01
VIDEO PERMA-LINK :- https://bit.ly/3tv9tCW
PDF LINK :- Chapter-01
Q)0-structure.c
#include <stdio.h>
int main()
{
return 0;
}
====================================================================
Q)1-first.c
# include<stdio.h>
/*
This is our first c program
which is awesome!
*/
int main()
{
int tom;
int Tom;
/*are two different variables
because case sensitive*/
//Declaring variables to store error codes
int error_code; //meaningful variables
// 'J' is a character
printf("Hello I am learning C with Harry");
return 0;
}
====================================================================
Q)2-variables.c
#include <stdio.h>
/*
This is our first c program
which is awesome!
*/
int main()
{
int a = 4;
int d = 45;
//int b=8.5;//not recommended because 8.5 isn't an integer
float b = 8.5;
char c = 'u';
int e = 45 + 4;
printf("The value of a is = %d \n", a);
printf("The value of a is = %f \n", b);
printf("The value of a is = %c \n", c);
printf("Difference of a & d is %d \n", a - d);
printf("Sum of a and d is %d \n", e);
}
====================================================================
Q)3-add2no.c
#include<stdio.h>
int main()
{
int a=4+3;
printf("Sum of two numbers=%d \n",a);
return 0;
}
====================================================================
Q)4-input.c
#include <stdio.h>
int main()
{
int a, b;
printf("Enter the value of a\n");
scanf("%d", &a); //& -> "address of <memory/variable>"
printf("Enter the value of b\n");
scanf("%d", &b);
printf("The sum of a & b is=%d\n", a + b);
return 0;
}
/* scanf syntax
scanf("<library function>", <&variable>);
Example:-
float c;
scanf("%f" ,&c);
*/
====================================================================
PRACTICE PROBLEMS:-
PDF LINK :- Chapter-01 Practice Set
Q)1-practice.c
#include <stdio.h>
//Find Area of rectangle
int main()
{
//a)using hard coded inputs
int ar=5*3;
printf("Area=%d\n",ar);
//b)using user input format
int l, b;
printf("Enter length and breadth\n");
scanf("%d", &l);
scanf("%d", &b);
printf("Area=%d\n", l * b);
return 0;
}
====================================================================
Q)2-practice.c
#include <stdio.h>
/*#include <curses.h> for linux systems.
Modern header that provides similar functions as <conio.h>*/
double main()
{
//Calculate area of circle
double r, h;
printf("Enter radius of circle\n");
scanf("%lf", &r);
printf("Area of circle is=%lf\n", 3.141 * r * r);
//Calculate area of Cylinder
printf("Enter radius and height of cylinder\n");
scanf("%lf", &r);
scanf("%lf", &h);
printf("Area of cylinder=%lf\n", (2 * 3.141 * r * h) + (2 * 3.141 * r * r));
return 0.0;
}
====================================================================
Q)3-practice.c
#include <stdio.h>
double main()
{
//Convert celsius to fahrenheit
double c, f;
printf("Enter temp in celsius\n");
scanf("%lf", &c);
f = c * 1.8 + 32;
printf("Temp in fahrenheit scale=%lf\n", f);
return 0.0;
}
====================================================================
Q)4-practice.c
#include <stdio.h>
double main()
{
//Calculate SI
printf("Enter principal, rate and time\n");
double p, r, t;
scanf("%lf", &p);
scanf("%lf", &r);
scanf("%lf", &t);
double si = (p * r * t) / 100.0;
printf("SI is=%lf\n", si);
return 0.0;
}
====================================================================
CHAPTER-02
PDF LINK :- Chapter-02
Q)5-instructions.c
#include <stdio.h>
int main()
{
// //int a=4;//Type declaration instruction
// int a = 4, b, c; //Type declaration instruction
// b = c = a;
// printf("The value of a is=%d\n", a);
// printf("The value of b is=%d\n", b);
// printf("The value of c is=%d\n", c);
float a = 1.1;
float b = a + 8.9;
printf("The value of c is=%f\n", b);
/*
float b=a+8.9;
float a=1.1;
printf("The value of c is=%f\n", b);
won't run because compiler can't find variable/value of a assigned before the execution
*/
return 0;
}
====================================================================
Q)6-arithmetic_instructions.c
#include <stdio.h>
#include <math.h> //used to import mathematical functionality like pow
int main()
{
int a = 4;
int b = 8;
int z;
z = b * a; //legal
//b*a=z illegal because '=' is assignment operator
printf("The value of a+b is : %d\n", a + b);
printf("The value of a-b is : %d\n", a - b);
printf("The value of a*b is : %d\n", a * b);
printf("The value of a/b is : %d\n", a / b);
printf("The value of z is : %d\n", z);
printf("5 when divided by 2 leaves a remainder=%d\n", 5 % 2);
//in '%' operator the remainder sign is same as that of numerator
printf("-5 when divided by 2 leaves a remainder=%d\n", -5 % 2);
//No operator is assumed to be present
//printf("The value of 4 multiplied by 5 is=%d\n", 4.5); is illegal won't return 20
//printf("The value of 4 multiplied by 5 is=%d\n", (4)(5)); is illegal won't return 20
//The operation should be written as
printf("The value of 4 multiplied by 5 is=%d\n", 4 * 5); //is legal
//There is no exponential operator in C
printf("The value of 4 to the power 2 is=%d\n", 4 ^ 2); //won't return 16
//'^' is a bitwise XOR operator in C
/*But we can use pow function to solve exponential problems*/
printf("The value of 4 to the power 2 is=%lf\n", pow(4, 2));
printf("The value of 6+5 is=%d\n", 6 + 5);
printf("The value of 6+5.6 is=%lf\n", 6 + 5.6);
printf("The value of 6.1+5.6 is=%lf\n", 6.1 + 5.6);
printf("The value of 5/2 is=%d\n", 5 / 2); //will return 2 not 2.5 because of int & int operation
printf("The value of 5/2 is=%lf\n", 5.0 / 2.0);
printf("The value of 3.0/9 is=%lf\n", 3.0 / 9);
return 0;
}
====================================================================
Q)7-operator_precedence.c
#include <stdio.h>
#include <math.h>
int main()
{
int x = 2;
int y = 3;
printf("The value of 3*x-8*y is=%d\n", 3 * x - 8 * y);
printf("The value of 8*y/3*x is=%d\n", 8 * y / 3 * x);
/* 8 * 3 / 3 * 2 = 24 / 6 = 4 is wrong
The right execution is
24 / 3 * 2
8 * 2
16 answer is justified */
return 0;
}
====================================================================
PRACTICE PROBLEMS:-
1)
i. invalid. Because variable b is not declared.
ii. valid. Because '^' is a bitwise operator in C.
iii. invalid. Because char takes only one character enclosed within single quotes as value.
2) double data type. (C language assumes a decimal value as double data type by default)
3)5-practice.c
#include <stdio.h>
#include <math.h>
double main()
{
double n;
printf("Enter a number\n");
scanf("%lf", &n);
/*
We use fmod() to find the modulus of a float/double number
When fmod() is used in a program, to prevent a exception error thrown by gcc,
we have to compile using -lm.
Complete Syntax is:-
gcc <program name.c> -lm -o /path to output/<filename>
*/
if (fmod(n, 97.0) == 0.0)
printf("No. is divisible by 97=%lf\n", n);
else
printf("No. is not divisible by 97=%lf\n", n);
return 0.0;
}
4) 3*x/y-z+k
= 6/3-3+1
= 2-3+1
= -1+1
= 0
5) Floating Point number.
====================================================================
CHAPTER-03
PDF LINK :- Chapter-03
Q)8-if_basic.c
//C program to check if the number is even or odd
#include <stdio.h>
int main()
{
int a, b;
printf("Enter a number\n");
scanf("%d", &a);
if (a % 2 == 0)
{
printf("%d is even\n", a);
}
else
{
printf("%d is odd\n", a);
}
return 0;
}
====================================================================
Q)9-if_else.c
#include <stdio.h>
int main()
{
int age;
printf("Enter your age\n");
scanf("%d", &age);
if (age >= 90)
{
printf("You are above 90, you can't drive\n");
}
else
{
printf("You can drive\n");
}
if (age == 50) /* if (age = 50) then error won't be thrown but loop will break.
Any non zero number is considered to be true in C.*/
{
printf("Half century\n");
}
return 0;
}
====================================================================
Q)10-logical_operators.c
#include <stdio.h>
int main()
{
int age;
int vipPass = 0;
printf("Enter your age\n");
scanf("%d", &age);
printf("If emergency then enter 1\nElse enter 0\n");
scanf("%d", &vipPass);
if ((age <= 70 && age >= 18) || vipPass == 1)
//! operator -> is true if condition is false, is false if condition is true.
{
if (vipPass >= 1)
{
printf("You have vip Pass, You can Drive!!\nYour age is=%d\n", age);
}
else
{
printf("You are above 18 and below 70, you can drive\n");
}
}
else
{
printf("You can't drive\n");
}
if (age == 50) /* if (age = 50) then error won't be thrown but loop will break.
Any non zero number is considered to be true in C.*/
{
printf("Half century\n");
}
return 0;
}
====================================================================
Q)11-if_else_if.c
#include <stdio.h>
int main()
{
int num;
printf("Enter your number\n");
scanf("%d", &num);
//if else if ladder
if (num == 1)
{
printf("Your number is 1\n");
}
else if (num == 2)
{
printf("Your number is 2\n");
}
else if (num == 3)
{
printf("Your number is 3\n");
}
else //this is optional
{
printf("Your number is not 1,2 or 3!\n");
}
return 0;
}
====================================================================
Q)12-ternary.c
#include <stdio.h>
int main()
{
int a;
printf("Enter a\n");
scanf("%d", &a);
//One liner
(a < 5) ? printf("a is less than 5\n") : printf("a is not less than 5\n");
return 0;
}
====================================================================
Q)13-switch_case.c
#include <stdio.h>
int main()
{
int rating;
printf("Enter your rating(1-5)\n");
scanf("%d", &rating);
switch (rating)
{
case 1:
printf("Your rating is 1\n");
break;
case 2:
printf("Your rating is 2\n");
break;
case 3:
printf("Your rating is 3\n");
break;
case 4:
printf("Your rating is 4\n");
break;
case 5:
printf("Your rating is 5\n");
break;
default:
printf("Invalid Rating\n");
break;
}
return 0;
}
====================================================================
Q)14-switch_quiz.c
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks (1-100)\n");
scanf("%d", &marks);
if (marks <= 100 && marks >= 0)
{
printf("Your Grade is\n");
switch (marks)
{
case 90:
printf("A\n");
break;
case 91:
printf("A\n");
break;
case 92:
printf("A\n");
break;
case 93:
printf("A\n");
break;
case 94:
printf("A\n");
break;
case 95:
printf("A\n");
break;
case 96:
printf("A\n");
break;
case 97:
printf("A\n");
break;
case 98:
printf("A\n");
break;
case 99:
printf("A\n");
break;
case 100:
printf("A\n");
break;
case 80:
printf("B\n");
break;
case 81:
printf("B\n");
break;
case 82:
printf("B\n");
break;
case 83:
printf("B\n");
break;
case 84:
printf("B\n");
break;
case 85:
printf("B\n");
break;
case 86:
printf("B\n");
break;
case 87:
printf("B\n");
break;
case 88:
printf("B\n");
break;
case 89:
printf("B\n");
break;
case 70:
printf("C\n");
break;
case 71:
printf("C\n");
break;
case 72:
printf("C\n");
break;
case 73:
printf("C\n");
break;
case 74:
printf("C\n");
break;
case 75:
printf("C\n");
break;
case 76:
printf("C\n");
break;
case 77:
printf("C\n");
break;
case 78:
printf("C\n");
break;
case 79:
printf("C\n");
break;
case 60:
printf("D\n");
break;
case 61:
printf("D\n");
break;
case 62:
printf("D\n");
break;
case 63:
printf("D\n");
break;
case 64:
printf("D\n");
break;
case 65:
printf("D\n");
break;
case 66:
printf("D\n");
break;
case 67:
printf("D\n");
break;
case 68:
printf("D\n");
break;
case 69:
printf("D\n");
break;
default:
printf("F\n");
break;
}
}
else
{
printf("Invalid Mark entered=%d\n", marks);
}
return 0;
}
====================================================================
PRACTICE PROBLEMS:-
PDF LINK :- Chapter-03 Practice Set
1)I am 11
2)7-practice.c
#include <stdio.h>
int main()
{
double math, comp, eng, total = 0.0;
printf("Enter marks of math, computer and english out of 100\n");
scanf("%lf", &math);
scanf("%lf", &comp);
scanf("%lf", &eng);
if (math >= 33.0 && comp >= 33.0 && eng >= 33.0)
{
total = ((math + comp + eng) / 300.0) * 100.0;
if (total >= 40.0)
printf("Passed\n");
else
printf("Not passed\n");
}
else
printf("Not passed\n");
return 0;
}
====================================================================
3)8-practice.c
#include <stdio.h>
int main()
{
double slab, tax = 0.0;
printf("Enter your income\n");
scanf("%lf", &slab);
if (slab >= 250000.0 && slab <= 500000.0)
tax = (5 / 100.0) * slab;
if (slab > 500000.0 && slab <= 1000000.0)
tax = (20 / 100.0) * slab;
if (slab > 1000000.0)
tax = (30 / 100.0) * slab;
if (slab < 250000.0)
tax = 0.0;
printf("Income Tax is=%lf\n", tax);
return 0;
}
====================================================================
4)9-practice.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h> //used to write exit(0) syntax
int main()
{
int year;
printf("Enter a year\n");
scanf("%d", &year);
if (fmod(year, 4) == 0)
{
if (fmod(year, 100) == 0)
{
if (fmod(year, 400) == 0)
{
printf("Is Leap year=%d\n", year);
exit(0); //used to force exit from a C-program
}
else
{
printf("Not Leap year=%d\n", year);
exit(0);
}
}
printf("Is Leap year=%d\n", year);
}
else
printf("Not Leap year=%d\n", year);
return 0;
}
====================================================================
5)10-practice.c
#include <stdio.h>
int main()
{
char ch;
int as;
printf("Enter a character\n");
scanf("%c", &ch);
as = (int)ch;
if (as >= 97 && as <= 122)
printf("Character is in lowercase=%c\n", ch);
if (as >= 65 && as <= 90)
printf("Character is in uppercase=%c\n", ch);
return 0;
}
====================================================================
6)11-practice.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a, b, c, d;
double z = 0.0;
printf("Enter four numbers\n");
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c);
scanf("%lf", &d);
z = (a > b) ? ((a > c) ? ((a > d) ? "%lf", a : 1.0) : 1.0) : 1.0;
if (z > 1.0)
{
printf("The greatest number is=%lf\n", z);
exit(0);
}
z = (b > a) ? ((b > c) ? ((b > d) ? "%lf", b : 1.0) : 1.0) : 1.0;
if (z > 1.0)
{
printf("The greatest number is=%lf\n", z);
exit(0);
}
z = (c > a) ? ((c > b) ? ((c > d) ? "%lf", c : 1.0) : 1.0) : 1.0;
if (z > 1.0)
{
printf("The greatest number is=%lf\n", z);
exit(0);
}
z = (d > a) ? ((d > b) ? ((d > c) ? "%lf", d : 1.0) : 1.0) : 1.0;
printf("The greatest number is=%lf\n", z);
return 0;
}
====================================================================
CHAPTER-04
PDF LINK :- Chapter-4
Q)15-loop_intro.c
#include <stdio.h>
int main()
{
int a = 0;
//Loops are used to repeat similar part of a code snippet efficiently
/*
printf("Hello\n");
printf("World\n");
a = 1;
printf("%d\n", a);
a++;
printf("%d\n", a);
a++;
printf("%d\n", a);
a++;
printf("%d\n", a);
a++;
printf("%d\n", a);
a++;
printf("%d\n", a);
a++;
*/
return 0;
}
====================================================================
Q)16-while_loops.c
#include <stdio.h>
int main()
{
int a;
printf("Enter a value\n");
scanf("%d", &a);
printf("The values are\n");
//a=11;while(a>10) Then it be an infinite loop
while (a < 10)
{
printf("%d\n", a);
a++;
}
return 0;
}
====================================================================
Q)17-quick_quiz.c
#include <stdio.h>
int main()
{
int i = 0;
while (i <= 20)
{
if (i >= 10)
{
printf("The value of i is=%d\n", i);
}
i++; //i=i+1;
}
return 0;
}
====================================================================
Q)18-increment_op.c
#include <stdio.h>
int main()
{
int i = 5;
printf("The value after i++ is=%d\n", i++);
i++; //-> First use then change
++i; //-> First change then use
printf("The value of i=%d\n", i);
printf("The value of i=%d\n", i += 10); //compound assignment operator
return 0;
}
====================================================================
Q)19-do_while.c
#include <stdio.h>
int main()
{
int i = 220;
do
{
printf("The value of i is %d\n", i);
i++;
} while (i < 10); //first executes then checks the condition. This loop at least runs once.
return 0;
}
====================================================================
Q)20-quick_quiz.c
#include <stdio.h>
int main()
{
int a;
int b = 1;
printf("Enter a positive integer\n");
scanf("%d", &a);
printf("Output is\n");
do
{
printf("%d\n", b++);
} while (b <= a);
return 0;
}
====================================================================
Q)21-for_loop.c
#include <stdio.h>
int main()
{
for (int a = 0; a < 10; a++)
{
printf("The value of a is=%d\n", a);
}
return 0;
}
====================================================================
Q)22-quick_quiz.c
#include <stdio.h>
int main()
{
//print first n natural nos. using for loop in C
int n;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("The numbers are\n");
for (int a = 1; a <= n; a++)
printf("%d\n", a);
return 0;
}
====================================================================
Q)23-quick_quiz.c
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n\n");
scanf("%d", &n);
for (int i = n; i; i--)
printf("The value of i is=%d\n", i);
return 0;
}
====================================================================
Q)24-break_loop.c
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf("The value of i is=%d\n", i);
if (i == 4)
break; //breaks from the loop & prints upto 4
i++;
} while (i < 10);
return 0;
}
====================================================================
Q)25-continue_loop.c
#include <stdio.h>
int main()
{
int skip = 5, i = 0;
while (i < 10)
{
i++;
if (i != skip)
continue;
else
printf("%d\n", i);
}
return 0;
}
====================================================================
Q)26-random_no_generator.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number;
srand(time(0));
number = rand() % 100 + 1; //Generates a random number between 1 to 100
printf("The random no is=%d\n", number);
//Keep running the loop until the no is guessed
return 0;
}
====================================================================
Q)27-project1game.c
PDF LINK :- Project-01-Game
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number, guess, nguesses = 1;
srand(time(0));
number = rand() % 100 + 1; //Generates a random number between 1 to 100
//printf("The random no is=%d\n", number);
//Keep running the loop until the no is guessed
do
{
printf("Guess the number between 1 to 100\n");
scanf("%d", &guess);
if (guess > number)
{
printf("Lower number please!\n");
}
else if (guess < number)
{
printf("Higher number please!\n");
}
else
{
printf("You guessed the number in %d attempts!\n", nguesses);
}
nguesses++;
} while (guess != number);
return 0;
}
====================================================================
PRACTICE PROBLEMS:-
PDF LINK :- Chapter-4 Practice Set
1)12-practice.c
#include <stdio.h>
//print multiplication table for number n
int main()
{
int n;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Multiplication Table\n");
for (int i = 0; i <= 10; i++)
{
printf("%dx%d=%d\n", n, i, n * i);
}
return 0;
}
====================================================================
2)13-practice.c
#include <stdio.h>
//print multiplication table for number n
int main()
{
int n = 10;
printf("Value of n is=%d\n", n);
printf("Multiplication Table of %d Reversed\n", n);
for (int i = 10; i >= 0; i--)
{
printf("%dx%d=%d\n", n, i, n * i);
}
return 0;
}
====================================================================
3) 1-At least once.
4)True
5)14-practice.c
#include <stdio.h>
//find sum of first 10 natural nos using while loop
int main()
{
int i = 0, s = 0;
while (i <= 10)
{
s = s + i;
i++;
}
printf("The sum of first 10 natural numbers is=%d\n", s);
return 0;
}
====================================================================
6)15-practice.c
#include <stdio.h>
//to find sum of first 10 natural nos using for loop & do while loop
int do_while_loop()
{
int s = 0, i = 0;
do
{
s = s + i;
i++;
} while (i <= 10);
return s;
}
int for_loop()
{
int s = 0;
for (int i = 1; i <= 10; i++)
{
s = s + i;
}
return s;
}
int main()
{
printf("The sum of first 10 natural nos using do_while_loop is=%d\n", do_while_loop());
printf("The sum of first 10 natural nos using for_loop is=%d\n", for_loop());
return 0;
}
====================================================================
7)16-practice.c
#include <stdio.h>
int main()
{
int s = 0;
for (int i = 1; i <= 10; i++)
{
s = s + (8 * i);
}
printf("The sum of the numbers occuring in the multiplication table of 8=%d\n", s);
return 0;
}
====================================================================
8)17-practice.c
#include <stdio.h>
//Calculate factorial of a no using for loop
int main()
{
int n, f = 1;
printf("Enter a number\n");
scanf("%d", &n);
for (int c = 1; c <= n; c++)
{
f = f * c;
}
printf("The factorial of the given number is=%d\n", f);
return 0;
}
====================================================================
9)18-practice.c
#include <stdio.h>
//Calculate factorial of a no using while loop
int main()
{
int n;
printf("Enter a number\n");
scanf("%d", &n);
int f = 1, c = 1;
while (c <= n)
{
f = f * c;
c = c + 1;
}
printf("The factorial of the given no. is=%d\n", f);
return 0;
}
====================================================================
10)19-practice.c
#include <stdio.h>
//prime number check using for loop in C
int main()
{
int n, k = 0;
printf("Enter a number\n");
scanf("%d", &n);
for (int c = 1; c <= n; c++)
{
if (n % c == 0)
k = k + 1;
}
if (k == 2)
printf("The number %d is prime \n", n);
else
printf("The number %d is not prime \n", n);
return 0;
}
====================================================================
11)20-practice.c
#include <stdio.h>
//prime number check using while and do while loop in C
int while_loop(int z)
{
int c = 1, k = 0;
while (c <= z)
{
if (z % c == 0)
k = k + 1;
c++;
}
return k;
}
int do_while_loop(int x)
{
int c = 1, k = 0;
do
{
if (x % c == 0)
k = k + 1;
c++;
} while (c <= x);
return k;
}
int main()
{
int n;
printf("Enter a number\n");
scanf("%d", &n);
printf("Output of while Loop\n");
if (while_loop(n) == 2)
printf("The number is prime=%d\n", n);
else
printf("The number is not prime=%d\n", n);
printf("Output of do_while_loop\n");
if (do_while_loop(n) == 2)
printf("The number is prime=%d\n", n);
else
printf("The number is not prime=%d\n", n);
return 0;
}
====================================================================
CHAPTER-05
PDF LINK :- Chapter-05
Q)28-function_basic.c
#include <stdio.h>
void display(); //function prototype
int main()
{
int a;
printf("Initiializing display function\n");
display(); //Function Call
printf("Display function finished its work\n");
return 0;
}
//Function Definition
void display()
{
printf("This is display\n");
}
====================================================================
Q)29-quick_quiz.c
#include <stdio.h>
void GoodMorning();
void GoodAfternoon();
void GoodNight();
int main()
{
GoodMorning();
GoodAfternoon();
GoodNight();
return 0;
}
void GoodMorning()
{
printf("Good Morning\n");
}
void GoodAfternoon()
{
printf("Good Afternoon\n");
}
void GoodNight()
{
printf("Good Night\n");
}
====================================================================
Q)30-function_inside_function
#include <stdio.h>
void GoodMorning();
void GoodAfternoon();
void GoodNight();
int main()
{
GoodMorning();
return 0;
}
void GoodMorning()
{
printf("Good Morning\n");
GoodAfternoon();
}
void GoodAfternoon()
{
printf("Good Afternoon\n");
GoodNight();
}
void GoodNight()
{
printf("Good Night\n");
}
====================================================================
Q)31-sum_function.c
#include <stdio.h>
//sum is a function which takes a and b as input and returns an integer as output
int sum(int a, int b); //function prototype declaration
int main()
{
int c = sum(2, 5); //funtion call
printf("The value of c is=%d\n", c);
return 0;
}
int sum(int a, int b)
{
int c;
c = a + b;
return c;
}
====================================================================
Q)32-change.c
#include <stdio.h>
void change(int a);
int main()
{
int b = 344;
printf("The value of b before change is=%d\n", b);
change(b);
printf("The value of b after change is=%d\n", b);
return 0;
}
void change(int b)
{
b = 77;
}
====================================================================
Q)33-quick_quiz.c
#include <stdio.h>
#include <math.h>
int main()
{
double side;
printf("Enter the value of side\n");
scanf("%lf", &side);
printf("The value of area is=%lf\n", pow(side, 2));
//use -lm in gcc output
return 0;
}
====================================================================
Q)34-recursion.c
#include <stdio.h>
int factorial(int x);
int main()
{
int a;
printf("Enter a number\n");
scanf("%d", &a);
printf("The value of factorial of %d is=%d\n", a, factorial(a));
return 0;
}
int factorial(int x)
{
printf("Calling factorial %d\n", x);
if (x == 1 || x == 0)
{
return 1;
}
else
{
return x * factorial(x - 1);
}
}
====================================================================
PRACTICE PROBLEMS:-
#include <stdio.h>
double average(double m, double n, double o);
int main()
{
double x, y, z;
printf("Enter three number\n");
scanf("%lf", &x);
scanf("%lf", &y);
scanf("%lf", &z);
printf("The average of %lf, %lf and %lf is=%lf\n", x, y, z, average(x, y, z));
return 0;
}
double average(double m, double n, double o)
{
return (m + n + o) / 3.0;
}
====================================================================
2)22-practice.c
#include <stdio.h>
double farenheit(double n);
int main()
{
double m;
printf("Enter temperature in Celsius\n");
scanf("%lf", &m);
printf("The temperature in Fahrenheit is=%lf\n", farenheit(m));
return 0;
}
double farenheit(double n)
{
return n * 1.8 + 32;
}
====================================================================
3)23-practice.c
#include <stdio.h>
double accn(double n);
int main()
{
double m;
printf("Enter the mass of the body\n");
scanf("%lf", &m);
printf("The force of attraction on a body of mass exerted by earth=%lf\n", accn(m));
return 0;
}
double accn(double n)
{
return n * 9.8;
}
====================================================================
4)24-practice.c
#include <stdio.h>
int fib(int m);
int main()
{
int n;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("****Fibonacci series****\n");
if (n <= 0)
printf("No fibonacci series possible\n");
if (n == 1)
printf("0");
if (n == 2)
{
printf("0,");
printf("1");
}
if (n > 2)
{
printf("0,");
printf("1,");
for (int i = 2; i < n - 1; i++)
{
printf("%d,", fib(i));
}
printf("%d", fib(n - 1));
}
return 0;
}
int fib(int m)
{
if (m == 1 || m == 2)
{
return 1;
}
else if (m != 0)
{
return fib(m - 2) + fib(m - 1);
}
else
{
return 0;
}
}
====================================================================
5)25-practice.c
#include <stdio.h>
int main()
{
int a = 3;
printf("%d%d%d\n", a, ++a, a++);
return 0;
}
OUTPUT
553 -> (This is due to the fact that gcc compiles the program from right to left order. Hence, the result).
====================================================================
6)26-practice.c
#include <stdio.h>
int sum(int m);
int main()
{
int n;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("The sum of the first %d natural numbers is=%d\n", n, sum(n));
return 0;
}
int sum(int m)
{
if (m != 0)
{
return m + sum(m - 1);
}
else
{
return 0;
}
}
====================================================================
7)27-practice.c
#include <stdio.h>
int main()
{
for (int x = 1; x <= 5; x += 2)
{
for (int y = 1; y <= x; y++)
{
printf("*");
}
printf("\n");
}
return 0;
}


Comments
Post a Comment