0% found this document useful (0 votes)
16 views

Basic Programs C Language

The document contains several C programs demonstrating basic concepts like printing text, taking user input, performing arithmetic operations, loops, functions and patterns. The programs cover concepts like Hello World, name printing, integer input/output, addition, prime checking, data type sizes, simple interest calculation and more.

Uploaded by

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

Basic Programs C Language

The document contains several C programs demonstrating basic concepts like printing text, taking user input, performing arithmetic operations, loops, functions and patterns. The programs cover concepts like Hello World, name printing, integer input/output, addition, prime checking, data type sizes, simple interest calculation and more.

Uploaded by

Anupama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

BASIC PROGRAMS

// Simple C program to display "Hello World"

// Header file for input output functions


#include <stdio.h>

// main function -
// where the execution of program begins
int main()
{

// prints hello world


printf("Hello World");

return 0;
}
TO PRINT YOUR NAME

// C program to demonstrate printing of


// our own name using printf()
#include <stdio.h>

int main()
{
// print name
printf("Name : GeeksforGeeks");
return 0;
}
NAME USING SCANF

// C program to demonstrate printing of


// our own name using scanf()
#include <stdio.h>

int main()
{
char name[20];
printf("Enter name: ");

// user input will be taken here


scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
PRINT INTEGER VALUE

// C Program to Print
// Integer value
#include <stdio.h>

// Driver code
int main()
{
// Declaring integer
int x = 5;

// Printing values
printf("Printing Integer value %d", x);
return 0;
}
READ INTEGER VALUE
// C program to take an integer
// as input and print it
#include <stdio.h>

// Driver code
int main()
{
// Declare the variables
int num;

// Input the integer


printf("Enter the integer: ");
scanf("%d", &num);

// Display the integer


printf("Entered integer is: %d", num);

return 0;
}
ADD TWO NUMBERS
// C program to add two numbers
#include <stdio.h>
int main()
{
int A, B, sum = 0;

// Ask user to enter the two numbers


printf("Enter two numbers A and B : \n");

// Read two numbers from the user || A = 2, B = 3


scanf("%d%d", &A, &B);

// Calculate the addition of A and B


// using '+' operator
sum = A + B;

// Print the sum


printf("Sum of A and B is: %d", sum);

return 0;
TO CHECK NUMBER IS PRIME OR NOT
// C Program to check for prime number using
Simple Approach
#include <stdio.h>

// Function to check prime number


void checkPrime(int N)
{
// initially, flag is set to true or 1
int flag = 1;

// loop to iterate through 2 to N/2


for (int i = 2; i <= N / 2; i++) {

// if N is perfectly divisible
by i
// flag is set to 0 i.e false
if (N % i == 0) {
flag = 0;
break;
}
}
TO MULTIPLY TWO FLOATING POINT NUMBERS
// C program to multiply two
// floating point numbers
#include <stdio.h>

// Function to multiply floating


point
// numbers
float multiply(float a, float b)
{
return a * b;
}

// Driver code
int main()
{
float A = 2.12, B =
3.88, product;

// Calling product
function
product = multiply(A,
TO FIND SIZE OF DATATYPE
// C program to find the size of int,
char,
// float and double data types

#include <stdio.h>

int main()
{
int integerType;
char charType;
float floatType;
double doubleType;

// Calculate and Print


// the size of integer type
printf("Size of int is: %ld",
sizeof(integerType));

// Calculate and Print


// the size of charType
SIMPLE INTEREST
// C program to implement
// the above approach
#include <stdio.h>

// Driver code
int main()
{
// We can change values here
for
// different inputs
float P = 1, R = 1, T = 1;

// Calculate simple interest


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

// Print Simple Interest


printf("Simple Interest = %f\
n", SI);
SUM OF NATURAL NUMBER USING WHILE
LOOP
// C Program to demonstrate
// Sum of Natural Numbers
// using while loops

#include <stdio.h>
int main()
{
int i, s = 0;
int n = 10;
i = 1;

// while loop executes


// the statements until the
// condition is false
while (i <= n) {

// adding natural numbers


// up to given number n
s += i;
SUM OF NATURAL NUMBER USING FOR
LOOP
// C Program to demonstrate
// Sum of Natural Numbers
// using for loops
#include <stdio.h>

int main()
{

int i, s = 0;
int n = 10;

for (i = 0; i <= n; i++) {

// adding natural
numbers
// up to given
number n
s += i;
SUM OF NATURAL NUMBER USING
RECURSION
// C Program to demonstrate
// Sum of Natural Numbers
// using recursion
#include <stdio.h>

int sumofnaturalnumbers(int num)


{
if (num != 0)

// adding natural numbers up


to given number n
return num +
sumofnaturalnumbers(num - 1);
else
return num;
}

int main()
SUM OF NATURAL NUMBERS USING
FUNCTIONS
// C Program to demonstrate
// Sum of Natural Numbers
// using functions
#include <stdio.h>

int sumofnaturalnumbers(int
num)
{
int i, s = 0;
for (i = 0; i <= num; i++)
{

// adding
natural numbers
// up to given
number n
s += i;
}
SUM OF NATURAL NUMBER USING
FORMULA
// C Program to demonstrate
// Sum of Natural Numbers
#include <stdio.h>
int main()
{

int num = 10;

int s,x;
s=num*(num+1);
x=(int)(s/2);
printf("Sum = %d", x);
return 0;

}
AREA AND PERIMETER OF RECTANGLE
// C program to demonstrate the
// area and perimeter of rectangle
#include <stdio.h>

int main()
{

int l = 10, b = 10;


printf("Area of rectangle is : %d", l *
b);
printf("\nPerimeter of rectangle is :
%d", 2 * (l + b));
return 0;
}
PRINT ALPHABETS USING WHILE LOOP
// C program to find the print
// Alphabets from (A to Z) and
// (a to z) using while loop

#include <stdio.h>

int main()
{
// Declaring the variable
char i;

// Display the alphabets


printf("The Alphabets from A
to Z are: \n");

// Traversing each character


// with the help of while loop

i = 'A';
PRINT ALPHABETS USING FOR LOOP
// C program to find the print
// Alphabets from A to Z

#include <stdio.h>

int main()
{
// Declare the variables
char i;

// Display the alphabets


printf("The Alphabets from A to Z
are: \n");

// Traverse each character


// with the help of for loop
for (i = 'A'; i <= 'Z'; i++) {
PRINT ALPHABETS USING DO WHILE LOOP
// C program to find the print
// Alphabets from (A to Z) and
// (a to z) using do-while loop

#include <stdio.h>

int main()
{
// Declaring the
variable
char i;

// Display the
alphabets
printf("The Alphabets
from A to Z are: \n");

// Traversing each
character
// with the help of do
FIND FACTORIAL USING FOR LOOP
// C program to implement the above approach
#include <stdio.h>

// Function to find factorial of given number


unsigned int factorial(unsigned int n)
{
int result = 1, i;

// loop from 2 to n to get the factorial


for (i = 2; i <= n; i++) {
result *= i;
}

return result;
}

// Driver code
int main()
{
int num = 5;
TO PRINT SIMPLE PYRAMID PATTERN OF NUMBERS

// C program to print right half pyramid


pattern of star
#include <stdio.h>

int main()
{
int rows;
printf("Number of rows: ");
scanf("%d", &rows);

// first loop for printing rows


for (int i = 1; i <= rows; i++) {

// second loop for


printing similar number in each
// rows
for (int j = 1; j <= i; j+
+) {
printf("%d
", i);
TO PRINT ALPHABET
// C program to print character
PATTERN
pattern using character

#include <stdio.h>

int main()
{
int i, j;

// input entering
number of rows
int rows = 5;

// taking first character


of alphabet
// which is useful to
print pattern
char character = 'A';

// first for loop is used


to identify number rows
TO CHECK ROOTS OF QUADRATIC
FUNCTION USING (FUNCTIONS)
// C program to find roots of
// a quadratic equation
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

// Prints roots of quadratic


// equation ax*2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is
// not quadratic, but linear
if (a == 0) {
printf("Invalid");
return;
}

int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
CHECK PRIME NUMBER USING FOR LOOP
(FUNCTION)
// C program to demonstrate whether
// a number is prime or not using
// for loop
#include <stdio.h>

// Defining the function


int primenumber(int number)
{
int i;

// Condition for checking the


// given number is prime or
// not
for (i = 2; i <= number / 2; i++)
{
if (number % i != 0)
continue;
else
return 1;
TO PRINT 2D ARRAY
#include <stdio.h>
const int M = 3;
const int N = 3;

void print(int arr[M][N])


{
int i, j;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
printf("%d ", arr[i][j]);
}

int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr);
return 0;
}
TO FIND LARGEST ELEMENT IN ARRAY
// C program to find maximum in
// arr[] of size n
#include <stdio.h>

// C function to find maximum


// in arr[] of size n
int largest(int arr[], int n)
{
int i;

// Initialize maximum
element
int max = arr[0];

// Traverse array elements


from
// second and compare every
// element with current max
for (i = 1; i < n; i++)
TO FIND MAXIMUM AND MINIMUM IN
ARRAY
/* structure is used to return two values from minMax() */
#include<stdio.h>
struct pair
{
int min;
int max;
};

struct pair getMinMax(int arr[], int n)


{
struct pair minmax;
int i;

/*If there is only one element then return it as min and max both*/
if (n == 1)
{
minmax.max = arr[0];
minmax.min = arr[0];
return minmax;
AVERAGE OF ELEMENTS IN ARRAY
// C program to demonstrate
// average of array elements
#include <stdio.h>

// Function that return average


// of given array.
double average(int a[], int n)
{
// Find the sum of array element
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];

return (double)sum / n;
}

// Driver code
int main()
{
// Input array
TO SORT ELEMENTS IN ASCENDING ORDER
IN ARRAY
// C program to sort the array in an
// ascending order using selection sort
#include <stdio.h>

void swap(int* xp, int* yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// Function to perform Selection Sort


void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of


// unsorted subarray
TO SORT ELEMENTS IN DESCENDING
ORDER IN ARRAY
// C program to sort array elements in
descending order

#include <stdio.h>

int main()
{

int a[5] = { 45, 22, 100, 66, 37 };


int n = 5, i, j, t = 0;

// iterates the array elements


for (i = 0; i < n; i++) {

// iterates the array


elements from index 1
for (j = i + 1; j < n; j++) {

// comparing the
array elements, to set array
USING LOOP TO CALCULATE LENGTH OF
STRING
// C program to find the length of string
#include <stdio.h>
#include <string.h>

int main()
{
char Str[1000];
int i;

printf("Enter the String: ");


scanf("%s", Str);

for (i = 0; Str[i] != '\0'; ++i);

printf("Length of Str is %d", i);

return 0;
}
ADD TWO STRINGS
#include <iostream>
#include <string>

using namespace std;

int main() {
string str1 = "Geeks";
string str2 = "ForGeeks";
string result = str1 + str2;
cout << result << endl;
return 0;
}
SORT A STRING USING ARRAYS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Defining comparator function as per the


requirement
static int myCompare(const void* a, const
void* b)
{

// setting up rules for comparison


return strcmp(*(const char**)a,
*(const char**)b);
}

// Function to sort the array


void sort(const char* arr[], int n)
{
// calling qsort function to sort the
array
TO REVERSE AN ARRAY OR STRING
// Iterative C program to reverse an array
#include <stdio.h>

/* Function to reverse arr[] from start to end*/


void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

/* Utility that prints out an array on a line */


void printArray(int arr[], int size)
{
TO RETURN A POINTER FROM A FUNCTION
// C program to illustrate the concept of
// returning pointer from a function
#include <stdio.h>

// Function returning pointer


int* fun()
{
int A = 10;
return (&A);
}

// Driver Code
int main()
{
// Declare a pointer
int* p;

// Function call
p = fun();
DECLARE 2D ARRAY OF POINTERS
// C program to illustrate the concept of
// returning pointer from a function
#include <stdio.h>

// Function returning pointer


int* fun()
{
int A = 10;
return (&A);
}

// Driver Code
int main()
{
// Declare a pointer
int* p;

// Function call
p = fun();
SORT AN ARRAY USING POINTERS
// C program to illustrate the concept of
// returning pointer from a function
#include <stdio.h>

// Function returning pointer


int* fun()
{
int A = 10;
return (&A);
}

// Driver Code
int main()
{
// Declare a pointer
int* p;

// Function call
p = fun();
SORT 2D ARRAY OF STRINGS
// C program to sort an array of strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to sort the values


void sort(char** names, int n)
{
int i, j;

// Perform sort operation using


bubble sort
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if
(strcmp(names[j], names[j + 1]) > 0) {
char*
temp;
temp =
(char*)calloc(30, sizeof(char));
STORE INFORMATION OF STUDENTS USING
STRUCTURE
// C Program to Store Information
// of Students Using Structure
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Create the student structure


struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};

// Driver code
int main()
{
int i = 0, n = 5;
TO READ AND WRITE STRUCTURE FILE
// C program for writing
// struct to file
#include <stdio.h>
#include <stdlib.h>

// a struct to be read and written


struct person {
int id;
char fname[20];
char lname[20];
};

int main()
{
FILE* outfile;

// open file for writing


outfile = fopen("person.bin", "wb");
if (outfile == NULL) {
fprintf(stderr, "\nError
FLEXIBLE ARRAY MEMBERS IN STRUCTURE
// C program for variable length members in
// structures in GCC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// A structure of type student


struct student {
int stud_id;
int name_len;

// This is used to store size of flexible


// character array stud_name[]
int struct_size;

// Flexible Array Member(FAM)


// variable length array must be last
// member of structure
char stud_name[];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy