## INDEX

### C program source code

This is for beginners

1
Write a c program to find out the sum of infinite G.P. series.
Source Code
#include<stdio.h>

int main(){

float a,r;
float sum=0;

printf("Enter the first number of the G.P. series: ");
scanf("%f",&a);

printf("Enter the common ratio of G.P. series: ");
scanf("%f",&r);

if(1 > r)
sum = a/(1-r);
else
sum = a/(r-1);

printf("\nSum of the infinite G.P. series: %f",sum);

return 0;
}

Sample output:

Enter the first number of the G.P. series: 1
Enter the common ratio of G.P. series: .5

Sum of the infinite G.P. series: 2.000000

Enter the first number of the G.P. series: 5
Enter the common ratio of G.P. series: 2

Sum of the infinite G.P. series: 5.000000

Definition of geometric progression (G.P.):

A sequence of numbers is termed a geometric sequence when the ratio between any two consecutive numbers remains constant. This constant ratio is referred to as the 'common ratio'.

Example of G.P. series:

2 4 8 16 32 64
Here common difference is 2 since ratio any two consecutive numbers for example 32 / 16 or 64/32 is 2.

Sum of G.P. series:
Sn =a(1–rn+1)/(1-r)

Tn term of G.P. series:

Tn = arn-1

Sum of infinite G.P. series:

Sn = a/(1-r)  if 1 > r
= a/(r-1)  if r > 1

2
Write a c program to find out the sum of in A.P. series.
Source Code
#include<stdio.h>
#include<math.h>

int main(){

int a,d,n,i,tn;
int sum=0;

printf("Enter the first number of the A.P. series: ");
scanf("%d",&a);

printf("Enter the total numbers in the A.P. series: ");
scanf("%d",&n);

printf("Enter the common difference of A.P. series: ");
scanf("%d",&d);

sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;
tn = a + (n-1) * d;
printf("Sum of the series A.P.: ");

for(i=a;i<=tn; i= i + d ){
if (i != tn)
printf("%d + ",i);
else
printf("%d = %d ",i,sum);
}

return 0;
}

Sample output:

Enter the first number of the A.P. series: 1
Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 3
Sum of the series: 1 + 4 + 7 + 10 + 13 = 35

Definition of arithmetic progression (A.P.):

A sequence of numbers is identified as an arithmetic sequence when the difference between any two consecutive numbers remains constant. This constant difference is known as the 'common difference'.

Example of A.P. series:
5 10 15 20 25 …

Here common difference is 5 since difference of any two consecutive numbers for example 20 – 15 or 25 -20 is 5.

Sum of A.P. series:
Sn = n/2(2a + (n-1) d)

Tn term of A.P. series:

Tn = a + (n-1) d

3
C code to find largest and smallest number in an array.
Source Code
#include<stdio.h>
int main(){
int a[50],size,i,big,small;

printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);

big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);

small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);

return 0;
}

Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1

4
Insert an element in an array at desired position using c program.
Source Code
#include<stdio.h>
int main(){
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;iscanf("%d",&a[i]);
printf("\nEnter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}

5
Delete element from an array at desired position using c.
Source Code
#include<stdio.h>
int main(){
int a[50],i,pos,size;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\nEnter position where to delete: ");
scanf("%d",&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10){
a[i]=a[i+1];
i++;
}
size--;
for(i=0;i<size;i++)
printf(" %d",a[i]);
return 0;
}

6
Write a c program to find out second smallest element of an unsorted array.
Source Code
#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

7

### Write a c program to find the size of structure without using sizeof operator.

Source Code
#include<stdio.h>

struct student{
int roll;
char name[100];
float marks;
};

int main(){

struct student *ptr = 0;

ptr++;
printf("Size of the structure student:  %d",ptr);

return 0;
}

8

### Write a c program to find the volume and surface area of a cube.

Source Code

Formula of surface area of cube:
Surface_area = 6 * a2

Formula of volume of cube:
Volume = a3

#include<stdio.h>

int main(){

float a;
float surface_area,volume;

printf("Enter size of any side of a cube : ");
scanf("%f",&a);

surface_area = 6 * (a * a);
volume = a * a * a;

printf("Surface area of cube is: %.3f",surface_area);
printf("\nVolume of cube is : %.3f",volume);

return 0;
}

Sample output:

Enter size of any side of a cube: 3
Surface area of cube is: 54.000
Volume of cube is: 27.000

9
Write a c program to find the area of a rectangle.
Source Code
Formula of area of triangle:

Area = length * width

#include<stdio.h>

int main(){

float l,w;
float area;

printf("Enter size of each sides of the rectangle : ");
scanf("%f%f",&l,&w);

area = l * w;
printf("Area of rectangle is: %.3f",area);

return 0;
}

Sample output:

Enter size of each sides of the rectangle: 5.2 20
Area of rectangle is: 104.000

10
Write a c program to find the area of an equilateral triangle.
Source Code

Formula of area of equilateral triangle:
Area = (√3)/4 * a2

#include<stdio.h>
#include<math.h>

int main(){

float a;
float area;

printf("Enter size of side of the equilateral triangle : ");
scanf("%f",&a);

area = sqrt(3)/4*(a*a);

printf("Area of equilateral triangle is: %.3f",area);

return 0;
}

Sample output:

Enter size of side of the equilateral triangle: 5
Area of equilateral triangle is: 10.825

11
Write a c program to find the volume and surface area of cone.
Source Code

Formula of surface area of cone:
Surface_area = Pie * r * (r + √ (r2 + h2))

Formula of volume of cone:
Volume = 1/3 * Pie * r* h
Pie = 22/7 or 3.14159265358979323846264338327950288419716939937510...

#include<stdio.h>
#include<math.h>

int main(){

float r,h;
float surface_area,volume;

printf("Enter size of radius and height of a cone : ");
scanf("%f%f",&r,&h);

surface_area = M_PI * r * (r + sqrt(r*r + h*h));
volume = (1.0/3) * M_PI * r * r * h;

printf("Surface area of cone is: %.3f",surface_area);
printf("\nVolume of cone is : %.3f",volume);

return 0;
}

Sample output:

Enter size of radius and height of a cone: 3 10
Surface area of cone is: 126.672
Volume of cone is: 94.248

12

### Factorial of 100 in c.

Source Code
#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];

int main(){
int num;
int i;

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

fact[0]=1;

factorialof(num);

printf("Factorial is : ");
for(i=length;i>=0;i--){
printf("%d",fact[i]);
}
return 0;
}
void factorialof(int num){
int i;
for(i=2;i<=num;i++){
multiply(i);
}
}
void multiply(int num){
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++){
arr[i]=fact[i];
}

for(i=0;i<=length;i++){
fact[i] = (arr[i]*num + r)%10;
r = (arr[i]*num + r)/10;
//printf("%d ",r);
}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r= r/10;
i++;
}
}
length = i-1;
}

Note: You can change the value of MAX to find factorial of too large numbers.

Factorial of 100 from above code is: 933262154439441526 816992388562667004907159682643816214685929638952175999322991560894146397615651828625369792082722375825118521091686400000000000

Logic for factorial of large numbers

In C, there is no native data type designed to store extremely large numbers. For instance, when attempting to solve an expression that involves very large numbers, the limitations of standard data types in C become apparent.:

100!

Factorial of 100 is very big number which beyond the range of even long int or long double.  Then question is how to store such a big numbers in c?

The solution is straightforward — leveraging arrays. The aforementioned program employs a similar logic to calculate the factorial of a number, with the key distinction being the use of an array to store the data instead of conventional variables.

13

### C program for ATM transactions

Source Code
C code for atm transaction while currencies are 1000, 500 and 100:

#include<stdio.h>

int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;

int main(){

unsigned long withdrawAmount;
unsigned long totalMoney;

int thousand=0,fiveHundred=0,oneHundred=0;

printf("Enter the amount in multiple of 100: ");
scanf("%lu",&withdrawAmount);

if(withdrawAmount %100 != 0){
printf("Invalid amount;");
return 0;
}

totalMoney = totalThousand * 1000 + totalFiveFundred* 500 +  totalOneHundred*100;

if(withdrawAmount > totalMoney){
printf("Sorry,Insufficient money");
return 0;
}

thousand = withdrawAmount / 1000;
if(thousand > totalThousand)
thousand = totalThousand;
withdrawAmount = withdrawAmount - thousand * 1000;

if (withdrawAmount > 0){
fiveHundred = withdrawAmount / 500;
if(fiveHundred > totalFiveFundred)
fiveHundred = totalFiveFundred;
withdrawAmount = withdrawAmount - fiveHundred * 500;
}

if (withdrawAmount > 0)
oneHundred = withdrawAmount / 100;

printf("Total 1000 note: %d\n",thousand);
printf("Total  500 note: %d\n",fiveHundred);
printf("Total  100 note: %d\n",oneHundred);

return 0;
}

Sample output:

Enter the amount in multiple of 100: 7800
Total 1000 note: 7
Total  500 note: 1
Total  100 note: 3

14

### Write a scanf function in c which accept sentence from user.

Source Code
#include<stdio.h>
#define MAX 500

int main(){

char arr[MAX];

printf("Enter any sentence which can include spaces.\n");
printf("To exit press enter key.\n");
scanf("%[^\n]s",arr);

printf("%s",arr);

return 0;
}

Sample output:

Enter any sentence which can include spaces.
To exit press enter key.

15

### Palindrome in c without using string function.

Source Code
#include<stdio.h>

int main(){
char str[100];
int i=0,j=-1,flag=0;

printf("Enter a string: ");
scanf("%s",str);

while(str[++j]!='\0');
j--;

while(i<j)
if(str[i++] != str[j--]){
flag=1;
break;
}

if(flag == 0)
printf("The string is a palindrome");
else
printf("The string is not a palindrome");

return 0;
}

16
Write a c program to find out the size and drive where file has stored of any given file?
Source Code
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
int main(){
struct stat status;
FILE *fp;
fp=fopen("test.txt","r");
fstat(fileno(fp),&status);
printf("Size of file : %d",status.st_size);
printf("Drive name   : %c",65+status.st_dev);
return 0;
}

Explanation:
Function int fstat (char *, struct stat *) store the information of open file in form of structure struct stat

Structure struct stat has been defined in sys\stat.h as

struct stat {
short  st_dev,   st_ino;
int    st_uid,   st_gid;
short  st_rdev;
long   st_size,  st_atime;
long   st_mtime, st_ctime;
};
Here

(a) st_dev: Indicates the drive where the file is stored on your computer and returns a numerical identifier.

(b) st_mode: Describes the various modes of the file, such as whether it's read-only, write-only, a folder, or a character file, among others.

(c) st_size: Represents the size of the file in bytes.

(d) st_ctime: Specifies the last date of modification of the file, presented in date format.

Note: The ASCII value of 'A' is 65. Therefore, by adding the value of `status.st_dev` with 65, you can derive the corresponding drive name on your computer.

17
Write a c program to know the last date of modification of any file.
Source Code
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
int main(){
struct stat status;
FILE *fp;
fp=fopen("test.txt","r");
fstat(fileno(fp),&status);

printf("Last date of modification : %s",ctime(&status.st_ctime));
return 0;
}
Explanation:
Function int fstat(char *, struct stat *) store the  information of open file in form of  structure struct stat

Structure struct stat has been defined in sys\stat.h as

struct stat {
short  st_dev,   st_ino;
int    st_uid,   st_gid;
short  st_rdev;
long   st_size,  st_atime;
long   st_mtime, st_ctime;
};
Here

(e) `st_dev`: Indicates the drive where the file is stored on your computer.

(f) `st_mode`: Describes various modes of the file, such as whether it's read-only, write-only, a folder, or a character file, among others.

(g) `st_size`: Represents the size of the file in bytes.

(h) `st_ctime`: Specifies the last date of modification of the file, presented in date format.

The `ctime` function converts a date type into a string format.

18

### Display source code as output in c program.

Source Code
#include<stdio.h>
int main(){
FILE *p;
char ch;
p=fopen("raja.c","r");
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
return 0;
}

19
C code to convert any number to English word.
Source Code
#include<stdio.h>
#include<string.h>

void toWord(int,int);
char * getPositionValue(int);
char * digitToWord(int);

char  word[100][30];
int i =0;

int main(){

int j,k,subnumer;
unsigned long int number;

printf("Enter any postive number: ");
scanf("%lu",&number);

if(number ==0){
printf("Zero");
return 0;
}

while(number){

if(i==1){
toWord(number %10,i);
number = number/10;
}else{
toWord(number %100,i);
number = number/100;
}

i++;

}

printf("Number in word: ");
*word[i-1] = *word[i-1] - 32;
for(j=i-1;j>=0;j--){
printf("%s",word[j]);
}

return 0;

}

void toWord(int number,int position){

char  numberToword[100]={" "};

if(number ==0){
}else if (number < 20 ||number %10==0){
strcpy(numberToword,digitToWord(number));
}else{
strcpy(numberToword,digitToWord((number/10)*10));
strcat(numberToword,digitToWord(number%10));
}

strcat(numberToword,getPositionValue(position));
strcpy(word[i],numberToword);
}

char * getPositionValue(int postion){

static char positionValue[10]=" ";

switch(postion){

case 1: strcpy(positionValue,"hundreds "); break;
case 2: strcpy(positionValue,"thousand "); break;
case 3: strcpy(positionValue,"lakh "); break;
case 4: strcpy(positionValue,"crore "); break;
case 5: strcpy(positionValue,"arab "); break;
case 6: strcpy(positionValue,"kharab "); break;
case 7: strcpy(positionValue,"neel "); break;
}

return positionValue;
}

char * digitToWord(int digit){

static char digitInWord[10]=" ";

switch(digit){
case 1: strcpy(digitInWord , "one "); break;
case 2: strcpy(digitInWord , "two "); break;
case 3: strcpy(digitInWord , "three "); break;
case 4: strcpy(digitInWord , "four "); break;
case 5: strcpy(digitInWord , "five "); break;
case 6: strcpy(digitInWord , "six "); break;
case 7: strcpy(digitInWord , "seven "); break;
case 8: strcpy(digitInWord , "eight "); break;
case 9: strcpy(digitInWord , "nine ");break;
case 10: strcpy(digitInWord , "ten "); break;
case 11: strcpy(digitInWord , "eleven "); break;
case 12: strcpy(digitInWord , "twelve "); break;
case 13: strcpy(digitInWord , "thirteen "); break;
case 14: strcpy(digitInWord , "fourteen "); break;
case 15: strcpy(digitInWord , "fifteen "); break;
case 16: strcpy(digitInWord , "sixteen "); break;
case 17: strcpy(digitInWord , "seventeen "); break;
case 18: strcpy(digitInWord , "eighteen "); break;
case 19: strcpy(digitInWord , "nineteen "); break;
case 20: strcpy(digitInWord , "twenty "); break;
case 30: strcpy(digitInWord , "thirty "); break;
case 40: strcpy(digitInWord , "fourty "); break;
case 50: strcpy(digitInWord , "fifty "); break;
case 60: strcpy(digitInWord , "sixty "); break;
case 70: strcpy(digitInWord , "seventy "); break;
case 80: strcpy(digitInWord , "eighty "); break;
case 90: strcpy(digitInWord,"ninety "); break;
}

return digitInWord;
}

20
C code to covert each digits of a number in English word.
Source Code
#include<stdio.h>

int main(){

int number,i=0,j,digit;
char * word[1000];

printf("Enter any integer: ");
scanf("%d",&number);

while(number){

digit = number %10;
number = number /10;

switch(digit){
case 0: word[i++] = "zero"break;
case 1: word[i++] = "one"break;
case 2: word[i++] = "two"break;
case 3: word[i++] = "three"break;
case 4: word[i++] = "four"break;
case 5: word[i++] = "five"break;
case 6: word[i++] = "six"break;
case 7: word[i++] = "seven"break;
case 8: word[i++] = "eight"break;
case 9: word[i++] = "nine"break;

}
}

for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}

return 0;

}

Sample output:

Enter any integer: 23451208
two three four five one two zero eight