Professional Documents
Culture Documents
C Written Book
C Written Book
C Written Book
1.What is Program?
4.What is IDE?
Ans: An integrated development environment (IDE) is a software
application that helps programmers develop software code efficiently. It
increases developer productivity by combining capabilities such as
software editing, building, testing, and packaging in an easy-to-use
application.
CodeBlocks is one of the famous IDE for C Programming.
1
Basic Definition of Programming
5.What is Compiler?
Ans: A compiler is a special program that translates a programming
language's source code into machine code, bytecode or another
programming language.
6.What is variable?
Ans: Variables are containers for storing data values, like numbers and
characters.
7.what is constant?
Ans: A constant is a named piece of memory where the value cannot be
changed while a program runs.
9.What is interpreter?
Ans: An interpreter is a computer program, which converts each
high-level program statement into the machine code.
2
History of C Programming Language:
The name "C" was chosen because it followed the previous language, B.
C was an improvement over B and included features like data types,
structures, and pointers.
In the late 1970s and early 1980s, the popularity of C grew rapidly as it
was adopted by many other operating systems, including MS-DOS,
3
Windows, and Mac OS. The development of the C compiler made it
easier to write C code on a variety of platforms.
1. Efficiency
2. Low-level programming
3. Portability
4. Modularity
5. Structured programming
6. Procedural programming
7. Extensibility
4
way of indicating to the computer how to interpret and handle the data
that is being used in a program.
Format Specifire:
Float=%f
Char=%c
Int=%d
5
Here is an example of a declaration statement in the C programming
language that declares an integer variable named "x":
int x;
Some programming languages require you to specify an initial value for
the variable at the time of declaration. For example, in C, you can
initialize the integer variable "x" to the value 10 as follows:
int x = 10;
C tokens:
1. Keywords: These are reserved words in C that have a specific meaning
and cannot be used as variable or function names. Examples include
if, while, int, float, else, etc.
2. Identifiers: These are user-defined names that represent variables,
functions, or other program elements. Identifiers must begin with a
letter or underscore, followed by any combination of letters,
underscores, or digits. For example, sum, _count, calculateArea, etc.
3. Constants: These are fixed values that do not change during the
execution of a program. There are two types of constants in C:
Integer constants: These represent whole numbers and can be
6
Floating-point constants: These represent decimal numbers and
include a decimal point or an exponent. For example , 3.14159, 2.5e-
3, etc.
4. String literals: These are sequences of characters enclosed in double
quotes. For example, "Hello, world!".
5. Operators: These are symbols that perform mathematical or logical
operations on operands. Examples include +, -, *, /, %, ==, !=, &&, ||,
etc.
6. Punctuators: These are special characters that are used to separate or
group parts of a program. Examples include {, }, (), [], ,, ;, etc.
Oparator in C:
7
individual bits that make up the operands. Examples include &
(bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), <<
(left shift), and >> (right shift).
5. Assignment operators: These operators assign a value to a variable.
Examples include = (simple assignment), += (add and assign), -=
(subtract and assign), *= (multiply and assign), /= (divide and
assign), %= (modulus and assign), <<= (left shift and assign), >>=
(right shift and assign), &= (bitwise AND and assign), |= (bitwise OR
and assign), and ^= (bitwise XOR and assign).
6. Increment and decrement operators: These operators are used to
increment or decrement the value of a variable by one. Examples
include ++ (increment) and -- (decrement).
7. Conditional operator: This operator is also known as the ternary
operator, and it is used to assign a value to a variable based on a
condition. The syntax is condition ? value1 : value2, where condition
is a boolean expression, value1 is the value to be assigned if the
condition is true, and value2 is the value to be assigned if the
condition is false.
#include <stdio.h>
8
1. Single-line comments: These are comments that span only one
line. They are useful for providing brief descriptions or
explanations of code. To add a single-line comment, you can use
two forward slashes ( //) followed by the text of the comment. For
example:
// This is a single-line comment
Anything after the // is considered a comment and will not be executed
by the compiler.
int main() {
// This is a single-line comment
/*
This is a multi-line comment that spans multiple lines.
It can be used to provide more detailed descriptions or explanations
of code, or to temporarily disable code that you don't want to execute.
*/
printf("Hello, world!\n"); // Another single-line comment
return 0;
9
}
\n=new line.
\t=tab (1 tab means 4 space)
Syntex of c program:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
10
Overall, the basic structure of a C program includes a main() function
that serves as the entry point for execution, as well as any necessary
return 0;
}
Explanation:
11
1. We start by including the stdio.h header file, which contains
functions for input and output.
2. We declare three variables: num1, num2, and sum. num1 and num2
are used to store the two numbers entered by the user, while sum
will hold their sum.
3. We use the printf function to prompt the user to enter the first
number, and then we use the scanf function to read the input
value and store it in the num1 variable.
4. We do the same for the second number, using printf to prompt
the user and scanf to read the input and store it in num2.
5. We calculate the sum of the two numbers and store the result in
sum.
6. Finally, we use printf to display the result to the user, with the
original numbers and their sum.
7. The return 0 statement at the end of the main function indicates
that the program has executed successfully.
p.p-1. Write a c program to enter three numbers and find their sum.
#include <stdio.h>
int main() {
float length, breadth, area, perimeter;
12
printf("Enter the breadth of the rectangle: ");
scanf("%f", &breadth);
return 0;
}
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, diameter, circumference, area;
diameter = 2 * radius;
circumference = 2 * PI * radius;
area = PI * radius * radius;
13
return 0;
}
#include <stdio.h>
int main() {
float base, height, area;
return 0;
}
#include <stdio.h>
int main() {
float cm, m, km;
14
scanf("%f", &cm);
m = cm / 100;
km = cm / 100000;
return 0;
}
c/5=(f-32)/9=(k-273)/5
#include <stdio.h>
int main() {
float celsius, fahrenheit;
return 0;
}
15
#include <stdio.h>
int main() {
float fahrenheit, celsius;
return 0;
}
#include <stdio.h>
int main() {
int days, years, weeks, remaining_days;
return 0;
}
16
Q-9. Write a c program to find the power of any number(x^y).
Solution:
#include <stdio.h>
#include <math.h>
int main() {
float base, exponent, result;
return 0;
}
Q-10.Write a c program to enter any number and calculate its squre
root.
Solution:
#include <stdio.h>
#include <math.h>
int main() {
float number, square_root;
square_root = sqrt(number);
return 0;
17
}
#include <stdio.h>
int main() {
float angle1, angle2, angle3;
return 0;
}
#include <stdio.h>
18
#include <math.h>
int main() {
float side, area;
printf("The area of the equilateral triangle is: %.2f square units\n", area);
return 0;
}
#include <stdio.h>
int main() {
float marks[5], total = 0, avg, percentage;
int i;
19
printf("Total marks obtained: %.2f\n", total);
printf("Average marks obtained: %.2f\n", avg);
printf("Percentage of marks obtained: %.2f%%\n", percentage);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
float P, T, R, SI, CI;
20
printf("Simple interest = %.2f\n", SI);
printf("Compound interest = %.2f\n", CI);
return 0;
}
if (condition) {
else {
Note that the else block is optional. If it is not included, then only the
code inside the if block will be executed if the condition is true, and
nothing will happen if the condition is false.
21
Q-15. Write a c program to check whether a number is
negative ,positive or zero.
Solution:
Number>0=positive
Number<0=negative
Number=0 then zero count
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.", num);
}
else if (num < 0) {
printf("%d is negative.", num);
}
else {
printf("The number is zero.");
}
return 0;
}
22
If the number is less than 0, it's considered negative and the
program prints a message indicating that.
If the number is equal to 0, it's considered zero and the program
prints a message indicating that.
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
Alphabet=(a-z)&(A-Z)
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
23
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is an alphabet.", ch);
}
else {
printf("%c is not an alphabet.", ch);
}
return 0;
}
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
return 0;
}
24
Q-19. Write a c program to input any character and check whether it
is alphabet,digit or special character.
Solution:
Digit=(0-9)
Alphabet=(a-z)&(A-Z)
Special character=@,%,+,-,*,/,!,?.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is an alphabet.", ch);
}
else if (ch >= '0' && ch <= '9') {
printf("%c is a digit.", ch);
}
else {
printf("%c is a special character.", ch);
}
return 0;
}
Q-20. Write a c program to input week number and print week day.
Solution:
#include <stdio.h>
int main() {
int week;
printf("Enter week number (1-7): ");
scanf("%d", &week);
if (week == 1) {
25
printf("Monday");
}
else if (week == 2) {
printf("Tuesday");
}
else if (week == 3) {
printf("Wednesday");
}
else if (week == 4) {
printf("Thursday");
}
else if (week == 5) {
printf("Friday");
}
else if (week == 6) {
printf("Saturday");
}
else if (week == 7) {
printf("Sunday");
}
else {
printf("Invalid week number!");
}
return 0;
}
Alphabet uppercase=(A-Z)
Alphabet lowercase=(a-z)
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
26
scanf("%c", &ch);
return 0;
}
int main() {
float angle1, angle2, angle3;
printf("Enter the three angles of the triangle: ");
scanf("%f %f %f", &angle1, &angle2, &angle3);
return 0;
}
27
Q-23. Write a c program to input all sides triangle and check whether
triangle is valid or not.
Solution:
#include <stdio.h>
int main() {
float side1, side2, side3;
printf("Enter the three sides of the triangle: ");
scanf("%f %f %f", &side1, &side2, &side3);
return 0;
}
#include <stdio.h>
int main() {
float side1, side2, side3;
printf("Enter the three sides of the triangle: ");
scanf("%f %f %f", &side1, &side2, &side3);
28
// Check if any side is zero or negative
if (side1 <= 0 || side2 <= 0 || side3 <= 0) {
printf("Invalid input: All sides must be positive.\n");
}
// Check if the sum of any two sides is greater than the third side
else if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 >
side1) {
// Check if all sides are equal
if (side1 == side2 && side1 == side3) {
printf("The triangle is an equilateral triangle.\n");
}
// Check if any two sides are equal
else if (side1 == side2 || side1 == side3 || side2 == side3) {
printf("The triangle is an isosceles triangle.\n");
}
// Otherwise, all sides are different
else {
printf("The triangle is a scalene triangle.\n");
}
}
else {
printf("The input does not form a valid triangle.\n");
}
return 0;
}
#include <stdio.h>
int main() {
float costPrice, sellingPrice, profit, loss;
29
if(sellingPrice > costPrice) {
profit = sellingPrice - costPrice;
printf("Profit = %.2f\n", profit);
} else if(costPrice > sellingPrice) {
loss = costPrice - sellingPrice;
printf("Loss = %.2f\n", loss);
} else {
printf("No profit or loss\n");
}
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different. Root 1 = %.2f and Root 2 = %.2f", root1,
root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and equal. Root 1 = Root 2 = %.2f;", root1);
} else {
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different. Root 1 = %.2f + %.2fi and Root 2 =
%.2f - %.2fi", realPart, imaginaryPart, realPart, imaginaryPart); }
return 0;
}
30
Q-27. Write a c program to count total number of notes(money) in given
amount.
Solution:
#include <stdio.h>
int main() {
int amount, notes;
31
if(amount >= 20) {
printf("Notes of 20: %d\n", amount / 20);
amount = amount % 20;
}
if(amount >= 5) {
printf("Notes of 5: %d\n", amount / 5);
amount = amount % 5;
}
if(amount >= 2) {
printf("Notes of 2: %d\n", amount / 2);
amount = amount % 2;
}
if(amount >= 1) {
printf("Notes of 1: %d\n", amount);
}
return 0;
}
32
break;
// more case statements can be added as needed
default:
// code to be executed when none of the above cases are matched
break;
}
The default case is executed if none of the constants match the value of
the expression. It is optional and can be omitted if there is no default
behavior required.
int main() {
char ch;
switch(ch) {
case 'a':
printf("%c is a vowel.", ch);
33
break;
case 'e':
printf("%c is a vowel.", ch);
break;
case 'i':
printf("%c is a vowel.", ch);
break;
case 'o':
printf("%c is a vowel.", ch);
break;
case 'u':
printf("%c is a vowel.", ch);
break;
case 'A':
printf("%c is a vowel.", ch);
break;
case 'E':
printf("%c is a vowel.", ch);
break;
case 'I':
printf("%c is a vowel.", ch);
break;
case 'O':
printf("%c is a vowel.", ch);
break;
case 'U':
printf("%c is a vowel.", ch);
break;
default:
printf("%c is a consonant.", ch);
}
return 0;
}
34
Here, we declare a character variable ch to store the alphabet entered by
the user. We prompt the user to enter an alphabet using the printf()
function followed by a call to scanf() function to read the input value.
Q-29. Write a c program to print day of week name using switch case.
Solution:
#include <stdio.h>
int main() {
int day;
switch(day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
35
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid day");
}
return 0;
}
#include<stdio.h>
int main()
{
int x,y;
printf("enter the two number:");
scanf("%d%d",&x,&y);
switch(x>y){
case 1 :
printf("%d is big ",x);
break;
default :
printf("%d is big",y);
break;
}
return 0;
}
36
Q-31. Write a c program to check a number is even or odd using switch
case.
Solution:
#include <stdio.h>
int main() {
int num, remainder;
printf("Enter a number: ");
scanf("%d", &num);
remainder = num % 2;
switch (remainder) {
case 0:
printf("%d is an even number.\n", num);
break;
case 1:
printf("%d is an odd number.\n", num);
break;
default:
printf("Invalid input.\n");
}
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, root1, root2, discriminant;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
37
discriminant = b * b - 4 * a * c;
return 0;
}
int main() {
char operator;
float num1, num2, result;
38
switch (operator) {
case '+':
result = num1 + num2;
printf("%.2f + %.2f = %.2f", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2f - %.2f = %.2f", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%.2f * %.2f = %.2f", num1, num2, result);
break;
case '/':
if (num2 == 0) {
printf("Error: Division by zero!");
} else {
result = num1 / num2;
printf("%.2f / %.2f = %.2f", num1, num2, result);
}
break;
default:
printf("Error: Invalid operator!");
}
return 0;
}
Conditional/Ternary Oparator:
39
Here, condition is a boolean expression that evaluates to either
true or false. If condition is true, the value of expression1 is
returned. If condition is false, the value of expression2 is returned.
Q-34. Write a c program to find maximum among three number using
ternary operator.
Solution:
#include <stdio.h>
int main() {
int a, b, c, max;
return 0;
}
Next, we use the ternary operator to find the maximum among these
three numbers. We first compare a and b, and if a is greater than b, we
compare a and c to get the maximum. Otherwise, we compare b and c to
get the maximum.
Finally, we print out the maximum value using the printf() function.
40
p-p-34. Write a c program to find minimum among three number using
ternary operator.
#include <stdio.h>
int main() {
int num;
return 0;
}
41
int main() {
int year;
return 0;
}
int main() {
char c;
42
p-p-37. Write a c program to check whether a character is lowercase or
uppercase alphabet using ternary operator.
Loop Programming:
Q. What is loop?
In computer programming, a loop is a sequence of instructions that is executed
repeatedly until a certain condition is met. The basic idea of a loop is to allow a block
of code to be executed repeatedly, either a fixed number of times or until a certain
condition is met.
Types of loop:
There are several types of loops, such as for loops, while loops, and do-while loops.
initialization: This is where you set an initial value for the loop
variable. This statement is executed only once, at the beginning of
the loop.
condition: This is the condition that is checked at the beginning of
each iteration of the loop. If the condition evaluates to true, the
loop body is executed. If the condition evaluates to false, the loop
exits.
increment/decrement: This statement is executed at the end of each
iteration of the loop, after the loop body has been executed. The
purpose of this statement is usually to modify the loop variable so
that the loop eventually terminates.
43
code to be executed repeatedly: This is the block of code that will be
executed repeatedly as long as the condition is true .
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
44
code to be executed repeatedly: This is the block of code that will be
executed repeatedly as long as the condition is true.
Example of while loop:
*** write a c program to print 1 to 10 using while loop.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
45
continue to execute. If the condition evaluates to false, the loop
exits.
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10.
int main() {
int n, i;
46
i = n;
while (i >= 1) {
printf("%d ", i);
i--;
}
return 0;
}
p-p-38. Write a c program to print all natural number in reverse (from 1
to n)-using while loop.
int main() {
char alphabet = 'a';
return 0;
}
47
Q-40.Write a c program to print of all even numbers between 1 to n.
Solution:
#include <stdio.h>
int main() {
int n;
return 0;
}
48
for (int i = 2; i <= n; i = i+2) {
sum =sum+ i;
}
printf("Sum of all even numbers between 1 and %d is %d", n, sum);
return 0;
}
int main() {
int number;
return 0;
}
49
Solution:
#include <stdio.h>
int main() {
int number, digit, sum = 0;
return 0;
}
Q-44.Write a c program to calculate product of digit of any number.
Solution:
#include <stdio.h>
int main() {
int number, digit, product = 1;
50
while (number > 0) {
digit = number % 10;
number = number/10;
product = product*digit;
}
return 0;
}
#include<stdio.h>
int main()
{
int n,last_digit;
printf("enter the number:");
scanf("%d",&n);
last_digit =n%10;
while(n>=10){
n=n/10;
}
printf("first digit: %d\n",n);
printf("last_digit: %d\n",last_digit);
51
return 0;
}
Q-46.Write a c program to find the sum of first and last digit any
number.
Solution:
#include<stdio.h>
void main()
{
int last,n,sum=0;
printf("enter the number:");
scanf("%d",&n);
last=n%10;
while(n>=10){
n=n/10;
sum=n+last;
}
printf("the total:%d+%d=%d\n",last,n,sum);
52
are control codes that are used to control devices or formatting, such as
line breaks and tab characters.
65: 'A'
97: 'a'
48: '0'
32: space character
9: tab character
10: newline character
int main() {
int i = 0;
while(i <= 127) {
printf("%d: %c\n", i, i);
i++;
}
return 0;
}
53
p.p-47. Write a c program to print all ASCII character with their value
using for loop.
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
#include <stdio.h>
54
int main() {
double num, i;
double factorial = 1;
printf("Enter a positive integer: ");
scanf("%lf", &num);
for(i=1; i<=num; i++) {
factorial =factorial*i;
}
printf("Factorial of %lf = %lf", num, factorial);
return 0;
}
p-p-49.Write a c program to enter any number and calculate its factorial
using while loop.
Solution:
#include<stdio.h>
int main()
{
int i,n,j;
printf("enter the range:");
scanf("%d ",&n);
for(i=2; i<=n; i++)
{
for(j=2; j<=i; j++)
{
55
if(i%j==0)
break;
}
if(i==j)
printf("%d\n", j);
}
return 0;
}
if(n%i==0){
printf("%d is not prime",n);
break;
}
}
if(i==n)
printf("%d prime number", n);
return 0;
#include<stdio.h>
void main()
{
int n1,n2,num1,num2,r,gcd,lcm;
printf("enter the two number:");
scanf("%d%d",&n1,&n2);
num1=n1;
num2=n2;
while(n2!=0){
r=n1%n2;
n1=n2;
n2=r;
}
gcd=n1;
lcm=num1*num2/gcd;
printf("gcd=%d\n",gcd);
printf("lcm=%d\n",lcm);
}
57
int a = 10;
int b = 20;
a = a + b; // a now holds the sum of the original values of a and b
b = a - b; // b now holds the original value of a
a = a - b; // a now holds the original value of b
After this code executes, the value of a will be 20 and the value of b will be 10.
#include<stdio.h>
int main()
{
int x,y;
printf("enter the number of x & y:");
scanf("%d%d",&x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("the swap number is x & y:%d %d\n",x,y);
return 0;
}
#include<stdio.h>
main()
{
int fact=1,n,i;
float s=0;
printf("enter the number:");
scanf("%d",&n);
58
for(i=1;i<=n;i++){
fact=fact*i;
s= s + i*1.0/fact;
}
printf("the factor:%f", s);
}
Q-55. Write a c program to find sum of series
1+(1+2)+(1+2+3)+(1+2+3+4)+………
Solution:
#include <stdio.h>
int main() {
int n, s=0,s1=0;
59
scanf("%d",&n);
for(i=1;i<=n;i++){
fact=fact*i;
s=s+(i*1.0)/fact;
}
printf("the sum is:%f",s);
}
Q.57.
*
**
***
****
*****
Solution:
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
}
Q.58.
60
11111
11111
11111
11111
11111
Solution:
#include<stdio.h>
int main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
printf("1");
}
printf("\n");
}
}
Q.59.
1
12
123
1234
12345
Solution:
61
#include<stdio.h>
void main()
{
int n,row,col;
printf("Enter the Range=");
scanf("%d",&n);
for(row=1;row<=n;row++){
for(col=1;col<=row;col++){
printf("%d",col);
}
printf("\n");
}
}
Declaration: To declare an array in C, you specify the data type of the elements and
the size of the array. The syntax is as follows:
data_type array_name[array_size];
Q.60-
62