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

Basic C Program

The document contains multiple C programming examples including a for loop, addition of two integers, pyramid pattern printing, checking if a number is positive/negative/zero, demonstrating data types, swapping numbers, checking leap years, printing names, finding variable sizes, Fibonacci series generation, and implementing linear and binary search. Each program includes the code and corresponding output. These examples serve as basic programming exercises for beginners in C.
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 views9 pages

Basic C Program

The document contains multiple C programming examples including a for loop, addition of two integers, pyramid pattern printing, checking if a number is positive/negative/zero, demonstrating data types, swapping numbers, checking leap years, printing names, finding variable sizes, Fibonacci series generation, and implementing linear and binary search. Each program includes the code and corresponding output. These examples serve as basic programming exercises for beginners in C.
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
You are on page 1/ 9

PROGRAM TO FOR LOOP

#include <stdio.h>
#include<conio.h>
int main() {
int i;
clrscr();
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
OUTPUT
1 2 3 4 5 6 7 8 9 10

PROGRAM TO ADD TWO INTEGERS


#include <stdio.h>
#include<conio.h>
int main() {
int number1, number2, sum;
clrscr();
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
getch();
}

OUTPUT
Enter two integers:
12
11
12 + 11 = 23
C PROGRAM TO PRINT PYRAMID PATTERN
#include <stdio.h>
#include<conio.h>
int main() {
int n = 5;
clrscr();
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
OUTPUT
*
**
***
****
*****
CHECK IF A NUMBER IS POSITIVE, NEGATIVE OR 0 USING CONDITIONAL
STATEMENT

#include <stdio.h>
#include<conio.h>
int main() {
int n = 10;
clrscr();
printf("Number = %d \n", n);
if (n > 0) {
printf("Positive.\n");
} else if (n < 0) {
printf("Negative.\n");
} else {
printf("Zero.\n");
}return 0;
getch();

OUTPUT

Number = 10
Positive.

DATA TYPES IN C

#include <stdio.h>
#include<conio.h>
int main() {
int myNum = 5;
float myFloatNum = 5.99;
char myLetter = 'D';
clrscr();
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
getch();
}
OUTPUT
5
5.99
D

SWAP NUMBERS USING TEMPORARY VARIABLE


#include<stdio.h>
#inlude<conio.h>
int main() {
double first, second, temp;
clrscr();
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
getch();
}

OUTPUT
Enter first number: 1.20
Enter second number: 2.45

After swapping, first number = 2.45


After swapping, second number = 1.20

PROGRAM TO CHECK LEAP YEAR


#include <stdio.h>
#include<conio.h>
int main() {
int year;
clrscr();
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
getch();
}
OUTPUT 1
Enter a year: 1900
1900 is not a leap year.
OUTPUT 2
Enter a year: 2012
2012 is a leap year.

C PROGRAM TO PRINT YOUR NAME

#include <stdio.h>

#inlude<conio.h>

int main() {

char name[100];

clrscr();

printf("Enter Your Name: ");

scanf("%s", name);

printf("Your Name: %s\n", name);

return 0;

getch();

}
OUTPUT

Enter Your Name: Rahul

Your Name: Rahul

PROGRAM TO FIND THE SIZE OF VARIABLES


#include<stdio.h>
#include<conio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
clrscr();
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
getch();
}

OUTPUT
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
FIBONACCI SERIES UP TO N TERMS
#include <stdio.h>
#include<conio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
clrscr();
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

OUTPUT

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
C PROGRAM TO IMPLEMENT LINEAR SEARCH
#include <stdio.h>
#include<conio.h>
int linearSearch(int* arr, int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main() {
int arr[] = { 10, 50, 30, 70, 80, 60, 20, 90, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int i = linearSearch(arr, n, key);
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);
return 0;
}
OUTPUT
Key Found at Index: 2

C PROGRAM FOR BINARY SEARCH

#include <stdio.h>
#include<conio.h>
int binarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid;
}
if (arr[mid] < key) {
left = mid + 1;
}
else {
right = mid - 1;
}}
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 23;
int result = binarySearch(arr, 0, size - 1, key);
if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);
}
return 0;
}

OUTPUT
Element is present at index 5

You might also like