2. /* C program to print Hello World! */
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
3.
4. /* C program to print Hello World using function */
#include <stdio.h>
// function to print Hello World!
void printMessage(void)
{
printf("Hello World!");
}
int main()
{
//calling function
printMessage();
return 0;
}
5.
6. /* C Program to find sum of two numbers */
#include<stdio.h>
int main() {
int a, b, sum;
printf("n Enter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
return(0);
}
7.
8. /* Check Whether Number is Prime or not */
#include<stdio.h>
int main()
{
int num, i, count = 0;
printf("Enter a number:");
scanf("%d", &num);
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
count++;
break;
}
}
if (count == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
return 0;
}
9.
10. /* Factorial of a Number*/
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
11.
12. /* Swap Numbers Using Temporary Variable*/
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// Value of first is assigned to temp
temp = first;
// Value of second is assigned to first
first = second;
// Value of temp (initial value of first) is assigned to second
second = temp;
printf("nAfter swapping, firstNumber = %.2lfn", first);
printf("After swapping, secondNumber = %.2lf", second);
return 0;
}
13.
14. /* C Program to generate the Fibonacci Series starting from any two numbers*/
#include<stdio.h>
int main()
{
int first, second, sum, num, counter = 0;
printf("Enter the term : ");
scanf("%d", &num);
printf("n Enter First Number : ");
scanf("%d", &first);
printf("n Enter Second Number : ");
scanf("%d", &second);
printf("n Fibonacci Series : %d %d ", first, second);
while (counter < num)
{
sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}
return (0);
}
15.
16. /* C Program to find greatest in 3 numbers*/
#include<stdio.h>
int main()
{
int a, b, c;
printf("nEnter value of a, b & c : ");
scanf("%d %d %d", &a, &b, &c);
if ((a > b) && (a > c))
printf("na is greatest");
if ((b > c) && (b > a))
printf("nb is greatest");
if ((c > a) && (c > b))
printf("nc is greatest");
return(0);
}
•
17.
18. /* Program to relate two integers using =, > or < symbol using C if...else Ladder*/
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
19.
20. /* Example of while loop*/
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
21.
22. /* do...while loop in C*/
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %dn", a);
a = a + 1;
}while( a < 20 );
return 0;
}
23.
24. /* for loop in C*/
#include <stdio.h>
int main () {
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %dn", a);
}
return 0;
}
25.
26. /*Nested For Loop in C*/
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
printf("%d, %dn",i ,j);
}
}
return 0;
}
27.
28. /* Switch Statement in C*/
1. #include <stdio.h>
2. int main ()
3. {
4. /* local variable definition */
5. char grade = 'B';
6. switch(grade) {
7. case 'A' :
8. printf("Excellent!n" );
9. break;
10. case 'B' :
11. case 'C' :
12. printf("Well donen" );
13. break;
14. case 'D' :
15. printf("You passedn" );
16. break;
17. case 'F' :
18. printf("Better try againn" );
19. break;
20. default :
21. printf("Invalid graden" );
22. }
23. printf("Your grade is %cn", grade );
24. return 0;
25. }
29.
30. 1. /* continue statement in C*/
2. #include <stdio.h>
3. int main ()
4. {
5. /* local variable definition */
6. int a = 10;
7.
8. /* do loop execution */
9. do {
10.
11. if( a == 15) {
12. /* skip the iteration */
13. a = a + 1;
14. continue;
15. }
16.
17. printf("value of a: %dn", a);
18. a++;
19.
20. } while( a < 20 );
21.
22. return 0;
23. }
31.
32. /* Example of goto statement*/
1. #include <stdio.h>
2. int main()
3. {
4. int sum=0;
5. for(int i = 0; i<=10; i++){
6. sum = sum+i;
7. if(i==5){
8. goto addition;
9. }
10. }
11. addition:
12. printf("%d", sum);
13. return 0;
14. }
• Output:
• 15
• Explanation: In this example, we have a label addition and when the value of i
(inside loop) is equal to 5 then we are jumping to this label using goto. This is
reason the sum is displaying the sum of numbers till 5 even though the loop is set
to run from 0 to 10.
33.
34. /* prime Numbers between Two Integers using function*/
1. #include <stdio.h>
2. int checkPrimeNumber(int n);
3. int main()
4. {
5. int n1, n2, i, flag;
6. printf("Enter two positive
integers: ");
7. scanf("%d %d", &n1, &n2);
8. printf("Prime numbers
between %d and %d are: ", n1,
n2);
9. for (i = n1 + 1; i < n2; ++i)
10. {
11. // flag will be equal to 1 if i
is prime
12. flag =
checkPrimeNumber(i);
13. if (flag == 1)
14. printf("%d ", i);
15. }
16. return 0;
17. }
18. // user-defined function to
check prime number
19. int checkPrimeNumber(int n)
20. {
21. int j, flag = 1;
22. for (j = 2; j <= n / 2; ++j) {
23. if (n % j == 0) {
24. flag = 0;
25. break;
26. }
27. }
28. return flag;
29. }
35.
36. /* Sum of Natural Numbers Using Recursion*/
1. #include <stdio.h>
2. int sum(int n);
3. int main()
4. {
5. int number, result;
6. printf("Enter a positive integer: ");
7. scanf("%d", &number);
8. result = sum(number);
9. printf("sum = %d", result);
10. return 0;
11. }
12. int sum(int n)
13. {
14. if (n != 0)
15. // sum() function calls itself
16. return n + sum(n-1);
17. else
18. return n;
19. }
37.
38. /* Function call by Value in C*/
1. #include <stdio.h>
2. /* function declaration */
3. void swap(int x, int y);
4. int main ()
5. {
6. /* local variable definition */
7. int a = 100;
8. int b = 200;
9. printf("Before swap, value of a : %dn", a );
10. printf("Before swap, value of b : %dn", b );
11. /* calling a function to swap the values */
12. swap(a, b);
13. printf("After swap, value of a : %dn", a );
14. printf("After swap, value of b : %dn", b );
15. return 0;
16. }
17. void swap(int x, int y)
18. {
19. int temp;
20. temp = x; /* save the value of x */
21. x = y; /* put y into x */
22. y = temp; /* put temp into y */
23. return;
24. }
39.
40. • /* Function call by reference in C*/
1. #include <stdio.h>
2. int main ()
3. {
4. /* local variable definition */
5. int a = 100;
6. int b = 200;
7. printf("Before swap, value of a : %dn", a );
8. printf("Before swap, value of b : %dn", b );
9. /* calling a function to swap the values */
10. swap(&a, &b);
11. printf("After swap, value of a : %dn", a );
12. printf("After swap, value of b : %dn", b );
13.
14. return 0;
15. }
16. void swap(int *x, int *y)
17. {
18. int temp;
19. temp = *x; /* save the value of x */
20. *x = *y; /* put y into x */
21. *y = temp; /* put temp into y */
22. return;
23. }
41.
42. • /* C - Arrays*/
1. #include <stdio.h>
2.
3. int main () {
4.
5. int n[ 10 ]; /* n is an array of 10 integers */
6. int i,j;
7.
8. /* initialize elements of array n to 0 */
9. for ( i = 0; i < 10; i++ ) {
10. n[ i ] = i + 100; /* set element at location i to i + 100 */
11. }
12.
13. /* output each array element's value */
14. for (j = 0; j < 10; j++ ) {
15. printf("Element[%d] = %dn", j, n[j] );
16. }
17.
18. return 0;
19. }
•
43.
44. • /* Simple Two dimensional (2D) Array Example*/
1. #include<stdio.h>
2. int main()
3. { /* 2D array declaration*/
4. int disp[2][3];
5. /*Counter variables for the loop*/
6. int i, j;
7. for(i=0; i<2; i++) {
8. for(j=0;j<3;j++) {
9. printf("Enter value for disp[%d][%d]:", i, j);
10. scanf("%d", &disp[i][j]);
11. } }
12. //Displaying array elements
13. printf("Two Dimensional array elements:n");
14. for(i=0; i<2; i++) {
15. for(j=0;j<3;j++) {
16. printf("%d ", disp[i][j]);
17. if(j==2){
18. printf("n");
19. } } }
20. return 0;
21. }
45.
46. /* Store Information in Structure and Display it*/
1. #include <stdio.h>
2. struct student {
3. char firstName[50];
4. int roll;
5. float marks; } s[10];
6. int main()
7. {
8. int i;
9. printf("Enter information of
students:n");
10. // storing information
11. for (i = 0; i < 5; ++i) {
12. s[i].roll = i + 1;
13. printf("nFor roll
number%d,n", s[i].roll);
14. printf("Enter first name: ");
15. scanf("%s", s[i].firstName);
16. printf("Enter marks: ");
17. scanf("%f", &s[i].marks);
18. }
19. printf("Displaying
Information:nn");
20. // displaying information
21. for (i = 0; i < 5; ++i) {
22. printf("nRoll number:
%dn", i + 1);
23. printf("First name: ");
24. puts(s[i].firstName);
25. printf("Marks: %.1f",
s[i].marks);
26. printf("n");
27. }
28. return 0;
29. }
47.
48. Write a 'C' program to accept customer details such as: Account_no, Name, Balance using structure. Assume
3 customers in the bank. Write a function to print the account no. and name of each customer whose balance
< 100 Rs.
#include<stdio.h>
/* Defining Structre*/
struct bank
{
int acc_no;
char name[20];
int bal;
}b[3];
/*Function to find the details of customer whose
balance < 100.*/
void check(struct bank b[],int n)
/*Passing Array of structure to function*/
{
int i;
printf("nCustomer Details whose Balance < 100
Rs. n");
printf("----------------------------------------------n");
for(i=0;i<n;i++)
{
if(b[i].bal<100)
{
printf("Account Number :
%dn",b[i].acc_no);
printf("Name : %sn",b[i].name);
printf("Balance : %dn",b[i].bal);
printf("------------------------------n");
}
}
}
int main()
{
int i;
for(i=0;i<3;i++)
{
printf("Enter Details of Customer %dn",i+1);
printf("------------------------------n");
printf("Enter Account Number : ");
scanf("%d",&b[i].acc_no);
printf("Enter Name : ");
scanf("%s",b[i].name);
printf("Enter Balance : ");
scanf("%d",&b[i].bal);
printf("------------------------------n");
}
check(b,3); //call function check
return 0;
}
49.
50. /* C - Unions*/
1. #include <stdio.h>
2. #include <string.h>
3. union Data
4. {
5. int i;
6. float f;
7. char str[20];
8. };
9. int main( )
10. {
11. union Data data;
12. printf( "Memory size occupied by data : %dn", sizeof(data));
13. return 0;
14. }
51.
52. /* example to define union for an employee in c*/
1. #include <stdio.h>
2. #include <string.h>
3. union employee
4. { int id;
5. char name[50];
6. }e1; //declaring e1 variable for union
7. int main( )
8. {
9. //store first employee information
10. e1.id=101;
11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char
array
12. //printing first employee information
13. printf( "employee 1 id : %dn", e1.id);
14. printf( "employee 1 name : %sn", e1.name);
15. return 0;
16. }
53.
54. /* C program to read name and marks of n number of students and store them in a file.*/
1. #include <stdio.h>
2. int main()
3. {
4. char name[50];
5. int marks, i, num;
6. printf("Enter number of students: ");
7. scanf("%d", &num);
8. FILE *fptr;
9. fptr = (fopen("C:student.txt", "w"));
10. if(fptr == NULL)
11. {
12. printf("Error!");
13. exit(1);
14. }
15. for(i = 0; i < num; ++i)
16. {
17. printf("For student%dnEnter name: ", i+1);
18. scanf("%s", name);
19. printf("Enter marks: ");
20. scanf("%d", &marks);
21. fprintf(fptr,"nName: %s nMarks=%d n", name, marks);
22. }
23. fclose(fptr);
24. return 0;
25. }
55.
56. /* C program to read name and marks of n number of students from and store them in a file. If the file
previously exits, add the information to the file.*/
1. #include <stdio.h>
2. int main()
3. {
4. char name[50];
5. int marks, i, num;
6. printf("Enter number of students: ");
7. scanf("%d", &num);
8. FILE *fptr;
9. fptr = (fopen("C:student.txt", "a"));
10. if(fptr == NULL)
11. {
12. printf("Error!");
13. exit(1);
14. }
15. for(i = 0; i < num; ++i)
16. {
17. printf("For student%dnEnter name: ", i+1);
18. scanf("%s", name);
19. printf("Enter marks: ");
20. scanf("%d", &marks);
21. fprintf(fptr,"nName: %s nMarks=%d n", name, marks);
22. }
23. fclose(fptr);
24. return 0;
25. }