0% found this document useful (0 votes)
17 views21 pages

C Programming Basics and Examples

The document contains various C programming examples, including programs for printing student details, calculating areas, performing arithmetic operations, and checking conditions like pass/fail, positivity, and voting eligibility. It also explains control structures such as if statements, for loops, and switch statements, providing syntax and examples for each. Additionally, it includes steps for compiling and running C programs in a terminal environment.

Uploaded by

daliyajeet238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views21 pages

C Programming Basics and Examples

The document contains various C programming examples, including programs for printing student details, calculating areas, performing arithmetic operations, and checking conditions like pass/fail, positivity, and voting eligibility. It also explains control structures such as if statements, for loops, and switch statements, providing syntax and examples for each. Additionally, it includes steps for compiling and running C programs in a terminal environment.

Uploaded by

daliyajeet238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

demo program

#include<stdio.h>
int main()
{

printf("C.C. SHAH HIGH SCHOOL \n");


printf("CLASS=10C \n");
printf("NAME - TIYA PATIL");

/* Print the following Pattern */

*******************************
******* Name – Dina Shah ********
********** Class – 10/A **********
********* Roll No. – 01 **********
*******************************

#include <stdio.h>
int main()
{
printf("*******************************\n");
printf("******* Name – Liley Shah *******\n");
printf("********** Class – 10/A **********\n");
printf("********* Roll No. – 01 **********\n");
printf("*******************************\n");

return 0;
}
2. /* Area of Rectangle */

#include<stdio.h>
int main()
{

float area,l,w;

printf("Enter Value of length :\n");


scanf("%f",&l);

printf("Enter Value of width :\n");


scanf("%f",&w);

area=l*w;

printf("Area of Rectangle : %f", area);

OUTPUT : -

Enter Value of length : 5

Enter Value of Width : 4

Area of Rectangle : 20
3. /* Addition of two numbers */

#include<stdio.h>
int main()
{

int sum,a,b;

printf("Enter value 1 :");


scanf("%d",&a);

printf("Enter value 2 :");


scanf("%d",&b);

sum=a+b;

printf("sum=%d",sum);
printf("addition of %d and %d is %d",a,b,sum);

}
4. /* Circumference of Circle */

#include<stdio.h>
#define PI 3.14 main()
{

float r,c;

printf("Enter Value of Radius : ");


scanf("%f",&r);

c=2*PI*r;

printf("Circumference of Circle = %d",c);


}
5. /* Write a C program to Find Square of anu number */

#include <stdio.h>

int main()
{
int num, square;

printf("Enter a number: ");


scanf("%d", &num);

square = num * num;

printf("Square of %d is %d\n", num, square);

return 0;
}
6. /* Find Average of three numbers */

#include <stdio.h>

int main()
{
float a, b, c, average;

printf("Enter three numbers: ");


scanf("%f %f %f", &a, &b, &c);

average = (a + b + c) / 3;

printf("Average = %.2f\n", average);

return 0;
}
7. /* Find Cube of any number */

#include <stdio.h>

int main()
{
int num, cube;

printf("Enter a number: ");


scanf("%d", &num);

cube = num * num * num;

printf("Cube of %d is %d\n", num, cube);

return 0;
}
9. /* Calculate Simple Interest */

#include <stdio.h>

int main()
{
float P, R, T, SI;

printf("Enter Principal amount: ");


scanf("%f", &P);

printf("Enter Rate of Interest: ");


scanf("%f", &R);

printf("Enter Time (in years): ");


scanf("%f", &T);

SI = (P * R * T) / 100;

printf("Simple Interest = %.2f\n", SI);

return 0;
}
10. /* Find Area of Circle */

#include <stdio.h>

int main()
{
float r, area;
float pi = 3.14;

printf("Enter radius of the circle: ");


scanf("%f", &r);

area = pi * r * r;

printf("Area of circle = %.2f\n", area);

return 0;
}
11. /* Find out student is Pass or Fail */

#include <stdio.h>

int main()
{
int marks;

printf("Enter student's marks: ");


scanf("%d", &marks);

if (marks <= 33)


{
printf("Result: FAIL\n");
}
else
{
printf("Result: PASS\n");
}

return 0;
}
12. /* Find Given Number is Possitive or Negative */

#include <stdio.h>

int main()
{
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num > 0)
{
printf("The number is Positive\n");
}
else if (num < 0)
{
printf("The number is Negative\n");
}
else
{
printf("The number is Zero\n");
}

return 0;
}
13. /* Write a program to find person is eligible for vote or not
*/

#include <stdio.h>

int main()
{
int age;

printf("Enter age of the person: ");


scanf("%d", &age);

if (age >= 18)


{
printf("Person is eligible to vote\n");
}
else
{
printf("Person is NOT eligible to vote\n");
}

return 0;
}
14. /* Write a program to print 1 to 10 numbers using For
Loop */

#include <stdio.h>

int main()
{
for (int i = 1; i <= 10; i++)
{
printf("%d\n", i);
}
return 0;
}

OUTPUT : -

1
2
3
4
5
6
7
8
9
10
15. /* Print Hello word for 5 times using For Loop */

#include <stdio.h>

int main()
{
for (int i = 1; i <= 5; i++)
{
printf("Hello\n");
}
return 0;
}

OUTPUT : -

Hello
Hello
Hello
Hello
Hello
-: Execution of C Programs :-
Step 1 : To move out from unwanted folders
CD..
CD..

Step 2 : To Go Inside the Program


CD
Ex. 1 - If you are in C Drive and Folder Name is 10adina
CD 10adina

Ex. 1 - If you are in D Drive and Folder Name is 10adina


CD D:\10adina

Step 3 : Compilation Code


[If my File name is code1.c the code is]
gcc code1.c -o [Link]

Step 4: To Run Code


.\[Link]

Terminal Screen
Use of if Statements

1 if statement

Syntax:

if (condition)
{
// code to execute if condition is true
}

Example:

int num = 10;


if (num > 0)
{
printf("Number is positive\n");
}

Description:

 Executes the block only if the condition is true.


 If the condition is false, it skips the block.
2 if..else statement

Syntax:

if (condition)
{
// code if condition is true
}
else
{
// code if condition is false
}

Example:

int num = -5;


if (num > 0)
{
printf("Number is positive\n");
}
else
{
printf("Number is not positive\n");
}

Description:

 Executes one block depending on the condition.


 If condition is true → first block runs.
 If condition is false → else block runs.
3 else if ladder statement

Syntax:

if (condition1)
{
// code if condition1 is true
}
else if (condition2)
{
// code if condition2 is true
}
else
{
// code if all conditions are false
}

Example:

int num = 0;
if (num > 0)
{
printf("Positive number\n");
}
else if (num < 0)
{
printf("Negative number\n");
}
else
{
printf("Number is zero\n");
}

Description:

 Checks multiple conditions in sequence.


 The first true condition executes its block, and the rest are skipped.
 else is optional and executes if all conditions are false.
4. for Loop in C Language

Syntax
for (initialization; condition; increment/decrement)
{
// statements to be executed
}

Description

 Initialization: Initializes the loop control variable (executed only once).


 Condition: Checked before every iteration. If it is true, the loop body
executes.
 Increment/Decrement: Updates the loop control variable after each
iteration.
 The loop continues until the condition becomes false.

The for loop is generally used when the number of iterations is known in
advance.

Example
#include <stdio.h> Explanation

int main()  i = 1 → initialization


{  i <= 5 → condition
int i;  i++ → increment
for (i = 1; i <= 5; i++)  The loop prints numbers from 1
{ to 5.
printf("%d\n", i);
}
return 0;
}
Output
1
2
3
4
5
* Switch Statement in C

The switch statement is used to execute one block of code from multiple
choices, based on the value of an expression.

When to use switch

 Multiple fixed options


 Menu-driven programs
 Better readability than if-else ladder

Syntax

switch (expression)
{
case constant1:
// code
break;

case constant2:
// code
break;

default:
// code
}
Example

int choice = 2;

switch (choice)
{
case 1:
printf("Monday");
break;

case 2:
printf("Tuesday");
break;

case 3:
printf("Wednesday");
break;

default:
printf("Invalid choice");
}

Output:

Tuesday

Description

 expression must be integer or character


 case values must be constant
 break stops execution after a matched case
 default runs when no case matches

You might also like