Basic Programs C Language
Basic Programs C Language
// main function -
// where the execution of program begins
int main()
{
return 0;
}
TO PRINT YOUR NAME
int main()
{
// print name
printf("Name : GeeksforGeeks");
return 0;
}
NAME USING SCANF
int main()
{
char name[20];
printf("Enter name: ");
// 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;
return 0;
}
ADD TWO NUMBERS
// C program to add two numbers
#include <stdio.h>
int main()
{
int A, B, sum = 0;
return 0;
TO CHECK NUMBER IS PRIME OR NOT
// C Program to check for prime number using
Simple Approach
#include <stdio.h>
// 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>
// 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;
// Driver code
int main()
{
// We can change values here
for
// different inputs
float P = 1, R = 1, T = 1;
#include <stdio.h>
int main()
{
int i, s = 0;
int n = 10;
i = 1;
int main()
{
int i, s = 0;
int n = 10;
// 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 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 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()
{
#include <stdio.h>
int main()
{
// Declaring the variable
char i;
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;
#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>
return result;
}
// Driver code
int main()
{
int num = 5;
TO PRINT SIMPLE PYRAMID PATTERN OF NUMBERS
int main()
{
int rows;
printf("Number of rows: ");
scanf("%d", &rows);
#include <stdio.h>
int main()
{
int i, j;
// input entering
number of rows
int rows = 5;
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>
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>
// Initialize maximum
element
int max = arr[0];
/*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>
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>
#include <stdio.h>
int main()
{
// 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;
return 0;
}
ADD TWO STRINGS
#include <iostream>
#include <string>
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>
// 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>
// 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>
// 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>
// 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>
int main()
{
FILE* outfile;