0% found this document useful (0 votes)
2 views23 pages

C Programming Project

The document contains a project on 'Programming In C' by Biswajit Dutta, showcasing various C programs that perform tasks such as calculating averages, simple interest, checking voting eligibility, temperature conversion, and more. Each program includes code snippets, outputs, and explanations of functionality. The document serves as a practical guide for learning C programming through examples.

Uploaded by

S Duta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views23 pages

C Programming Project

The document contains a project on 'Programming In C' by Biswajit Dutta, showcasing various C programs that perform tasks such as calculating averages, simple interest, checking voting eligibility, temperature conversion, and more. Each program includes code snippets, outputs, and explanations of functionality. The document serves as a practical guide for learning C programming through examples.

Uploaded by

S Duta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

SESSON :- 2022-2023

Project on “Programming In C”

Name – Biswajit Dutta.


Roll-
Reg. No. :-068830.
Paper:- Computer Fundamentals and Programming in C.
1. Write a code to calculate the average of three number
given by user.
Code:
1. #include<stdio.h>
2. int main() {
3. int x,y,z;
4. printf ("Give the 1st number \n");
5. scanf("%d",&x);
6. printf("Give the 2nd numbr\n");
7. scanf("%D",&y);
8. printf("Give the 3rd number\n");
9. scanf("%d",&z);
10.
11. printf("The avarage is = %d", (x+y+z)/3);
12.
13. return 0;
14. }

Output:
Give the 1st number

Give the 2nd numbr

Give the 3rd number

The avarage is = 5
2. c program to calculate simple interest.
int main()
{
float amount,rate,time,si;

printf("Enter principal (Amount) :");


scanf("%f",&amount);

printf("Enter rate :");


scanf("%f",&rate);

printf("Enter time (in years) :");


scanf("%f",&time);

si=(amount*rate*time)/100;

printf("\nSimple Interest is = %f",si);

return 0;
}

Output

Enter principal (Amount) :15000


Enter rate :2.8
Enter time (in years) :2

Simple Interest is = 840.000000


3. C program to check whether a person is eligible for
voting or not?
#include<stdio.h>

int main()
{
int a ;

//input age
printf("Enter the age of the person: ");
scanf("%d",&a);

//check voting eligibility


if (a>=18)
{
printf("Eigibal for voting");
}
else
{
printf("Not eligibal for voting\n");
}

return 0;
}

Output
First run:
Enter the age of the person: 21
Eigibal for voting

Second run:
Enter the age of the person: 15
Not eligibal for voting
4. C program to convert temperature from Fahrenheit
to Celsius and Celsius to Fahrenheit.
#include <stdio.h>

int main()
{

float fh,cl;
int choice;

printf("\n1: Convert temperature from Fahrenheit to Celsius.");


printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);

if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}

Output

First Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 1

Enter temperature in Fahrenheit: 98.6


Temperature in Celsius: 37.00

Second Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 2

Enter temperature in Celsius: 37.0


Temperature in Fahrenheit: 98.60
5. C programme to Print ASCII value of entered
character.

#include <stdio.h>

int main(){
char ch;
int asciiValue;

printf("Enter any character: ");


scanf("%c",&ch);

asciiValue=(int)ch;

printf("ASCII value of character: %c is: %d\n",ch,asciiValue);

Output

Enter any character: x


ASCII value of character: x is: 120
6. C program to find sum of all numbers from 0 to N
without using loop.

#include <stdio.h>

int main(void) {

int n, sum;

//input value of n
printf("Enter the value of n: ");
scanf("%d", &n);

//initialize sum with 0


sum =0;

//use formula to get the sum from 0 to n


sum = n*(n+1)/2;

//print sum
printf("sum = %d\n", sum);

return 0;
}

Output

Run1:
Enter the value of n: 5
sum = 15

Run2:
Enter the value of n: 10
sum = 55
7. C program to find factorial of a number.

#include <stdio.h>

int main()
{
int num,i;
long int fact;

printf("Enter an integer number: ");


scanf("%d",&num);

/*product of numbers from num to 1*/


fact=1;
for(i=num; i>=1; i--)
fact=fact*i;

printf("\nFactorial of %d is = %ld",num,fact);

return 0;
}

Output

Enter an integer number: 7

Factorial of 7 is = 5040
8. C program to print all prime numbers from 1 to N.
#include <stdio.h>

int checkPrime(int num)


{
int i;
int flg = 0;
/*if number (num) is divisble by any number from 2 to num/2
number will not be prime.*/
for (i = 2; i < (num - 1); i++) {
if (num % i == 0) {
flg = 1;
break;
}
}
if (flg)
return 0;
else
return 1;
}

int main()
{
int i, n;

printf("Enter the value of N: ");


scanf("%d", &n);

printf("All prime numbers are from 1 to %d:\n", n);


for (i = 1; i <= n; i++) {
if (checkPrime(i))
printf("%d,", i);
}

return 0;
}

Output:

Enter the value of N: 100


All prime numbers are from 1 to 100:
1,2,3,5,7,11,13,17,19,23,29,31,37,41,
43,47,53,59,61,67,71,73,79,83,89,97,
9. C program to read weekday number and print
weekday name using switch.

#include <stdio.h>

int main()
{
int wDay;

printf("Enter weekday number (0-6): ");


scanf("%d",&wDay);

switch(wDay)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
default:
printf("Invalid weekday number.");
}
printf("\n");
return 0;
}

Output

First Run:
Enter weekday number (0-6): 3
Wednesday

Second run:
Enter weekday number (0-6): 9
Invalid weekday number.
10.C program to print indexes of a particular character
in a string
#include <stdio.h>

int main()
{
char str[30],ch;
int ind[10],loop,j;

printf("Enter string: ");


scanf("%[^\n]s",str);

printf("Enter character: ");


getchar();
ch=getchar();

j=0;
for(loop=0; str[loop]!='\0'; loop++)
{
if(str[loop]==ch)
ind[j++]=loop;
}

printf("Input string is: %s\n",str);


printf("Indexes: ");
for(loop=0; loop<j; loop++)
printf("%d \t",ind[loop]);

return 0;
}

Output
Enter string: Hi there, how are you?
Enter character: o
Input string is: Hi there, how are you?
Indexes: 11 19
11. C program to capitalize first character of each word
in a string.
#include <stdio.h>
#define MAX 100

int main()
{
char str[MAX]={0};
int i;

//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces

//capitalize first character of words


for(i=0; str[i]!='\0'; i++)
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
++i;
//check next character is lowercase alphabet
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
}
else
{
//all other uppercase characters should be in lowercase
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32; //subtract 32 to make it
small/lowercase
}
}

printf("Capitalize string is: %s\n",str);

return 0;
}

Output
Enter a string: HELLO FRIENDS HOW ARE YOU?
Capitalize string is: Hello Friends How Are You?
12. C program to remove all spaces from a given string
#include <stdio.h>

// function to remove white spaces from the string


void remove_spaces(char *buf , int len)
{
int i=0,j=0;
char temp[100]={0};

for(i=0,j=0 ; i<len ; i++)


{
if(buf[i] == ' ' && buf[i]!=NULL)
{
for(j=i ; j<len ; j++)
{
buf[j] = buf[j+1];
}
len--;
}
}
}

// main function
int main()
{
// declare a char buffer
char string[100]={0};

// declare some local int variables


int i=0,len=0;

printf("\nEnter your string : ");


gets(string);

// calculate the length of the string


len = strlen(string);

remove_spaces(string , len);

printf("\nNew string is : %s\n",string);


return 0;
}

Output

Run 1 :
Enter your string : C is the master of all
New string is : Cisthemasterofall

Run 2 :
Enter your string : I love myself
New string is : Ilovemyself
13. C program to print fibonacci series using recursion
#include <stdio.h>

//function to print fibonacii series


void getFibonacii(int a,int b, int n)
{
int sum;
if(n>0)
{
sum=a+b;
printf("%d ",sum);
a=b;
b=sum;
getFibonacii(a,b,n-1);
}
}
int main()
{
int a,b,sum,n;
int i;

a=0; //first term


b=1; //second term

printf("Enter total number of terms: ");


scanf("%d",&n);

printf("Fibonacii series is : ");


//print a and b as first and second terms of series
printf("%d\t%d\t",a,b);
//call function with (n-2) terms
getFibonacii(a,b,n-2);
printf("\n");
return 0;
}

Output

Enter total number of terms: 10


Fibonacii series is : 0 1 1 2 3 5 8 13 21 34
14. C program to merge two arrays in third array which
is creating dynamically
#include <stdio.h>
#include <stdlib.h>
int main()
{
// declare two int arrays
int array_1[30] = {0};
int array_2[30] = {0};

// declare an int pointer which


// will store the combination of
// array_1 and array_2
int *c;

// declare some local variables


int i=0 , j=0 , k=0;

// x will store the number of elements for array_1


// y will store the number of elements for array_2
// z will store the total number of elements of array_2 array_1
int x=0 , y=0 , z=0;

printf("\nEnter the number of elements for both arrays..");


printf("\nFor array_1 : ");
scanf("%d",&x);
printf("\nFor array_2 : ");
scanf("%d",&y);

printf("\nEnter the elements for array_1..\n");


for(i=0 ; i<x ; i++)
{
printf("array_1[%d] : ",i);
scanf("%d",&array_1[i]);
}

printf("\nEnter the elements for array_2..\n");


for(i=0 ; i<x ; i++)
{
printf("array_2[%d] : ",i);
scanf("%d",&array_2[i]);
}

// Calculate the total elements for pointer "c"


z = x +y;

printf("\nTotal elements are : %d\n",z);

// now allocate dynamic memory to pointer "c"


// but according to the "z"
c = (int*)malloc(z * sizeof(int));

for(i=0,j=0,k=0 ; i<z,j<x,k<y ; i++)


{
c[i] = array_1[j++];
if(i>=x)
{
c[i] = array_2[k++];
}
}

printf("\nThe final array after merging the two arrays is..");


for(i=0;i<z;i++)
{
printf("\nC[%d] : %d",i,c[i]);
}

return 0;
}

Output

Enter the number of elements for both arrays..


For array_1 : 3

For array_2 : 3

Enter the elements for array_1..


array_1[0] : 10
array_1[1] : 20
array_1[2] : 30

Enter the elements for array_2..


array_2[0] : 40
array_2[1] : 50
array_2[2] : 60

Total elements are : 6

The final array after merging the two arrays is..


C[0] : 10
C[1] : 20
C[2] : 30
C[3] : 40
C[4] : 50
C[5] : 60
15. C Program to Cyclically Permute the Elements of an
Array
#include <stdio.h>
#include <stdlib.h>

//function to print the array


void print(int* a,int n){
printf("printing ........\n");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}

int* cyclicallyPermute(int* a,int n){


int temp=a[0];//store a[0]

for(int i=0;i<n;i++){
//for the last element in the modified array
//it will be starting elemnt
if(i==n-1)
a[i]=temp;
//for other element shift left
else
a[i]=a[i+1];
}

return a;
}

int main()
{
int n;

printf("enter array length,n: ");


scanf("%d",&n);

//allocating array dynamically


int* a=(int*)(malloc(sizeof(int)*n));

printf("enter elements: \n");


for(int i=0;i<n;i++)
scanf("%d",&a[i]);

printf("array before permutation\n");


print(a,n);

//function to permute cyclically


//returning base adress of modified array
a=cyclicallyPermute(a,n);

printf("array after permutation\n");


print(a,n);

return 0;
}
Output

enter array length,n: 10


enter elements:
4 5 6 7 8 10 11 34 56 1
array before permutation
printing ........
4 5 6 7 8 10 11 34 56 1
array after permutation
printing ........
5 6 7 8 10 11 34 56 1 4

16. C program to print box pattern using loops


#include <stdio.h>

int main()
{
int n, i, j, t; //n is representing number of the output box

//input n
printf("Enter the value of n: ");
scanf("%d", &n);

t = 2 * n - 1;
i = t; //i and j are the number of rows and columns of the box.
j = t;

// Declare box as a 2-D matrix having i number of rows


//and j number of columns
int a[i][j], k, m, p;
p = n;
m = 0;

for (k = 0; k < p; k++) {


for (i = m; i < t; i++) {
for (j = m; j < t; j++) {
if (i == m || i == (t - 1) || j == m || j == (t - 1)) {
a[i][j] = n;
if (n == 1) {
break;
}
}
}
}
t = t - 1;
n = n - 1;
m = m + 1;
}
t = 2 * m - 1;
for (i = 0; i < t; i++) {
for (j = 0; j < t; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Output

First run:
Enter the value of n: 2
2 2 2
2 1 2
2 2 2

Second run:
Enter the value of n: 4
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

17. C program to swap two numbers using pointers


#include <stdio.h>

// function : swap two numbers using pointers


void swap(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}

int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
//print values before swapping
printf("Before Swapping: num1=%d, num2=%d\n",num1,num2);
//call function by passing addresses of num1 and num2
swap(&num1,&num2);
//print values after swapping
printf("After Swapping: num1=%d, num2=%d\n",num1,num2);
return 0;
}

Output

Enter value of num1: 10


Enter value of num2: 20
Before Swapping: num1=10, num2=20
After Swapping: num1=20, num2=10
18. C program to compare contents of two files
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])


{
FILE *fp1 ;
FILE *fp2 ;

int cnt1 = 0;
int cnt2 = 0;
int flg = 0;

if( argc < 3 )


{
printf("Insufficient Arguments!!!\n");
printf("Please use \"program-name file-name1 file-name2\"
format.\n");
return -1;
}

fp1 = fopen(argv[1],"r");
if( fp1 == NULL )
{
printf("\n%s File can not be opened : \n",argv[1]);
return -1;
}

// move file pointer to end and get total number of bytes


fseek(fp1,0,SEEK_END);
cnt1 = ftell(fp1);

fp2 = fopen(argv[2],"r");
if( fp2 == NULL )
{
printf("\n%s File can not be opened : \n",argv[2]);
return -1;
}

// move file pointer to end and get total number of bytes


fseek(fp2,0,SEEK_END);
cnt2 = ftell(fp2);

fseek(fp1,0,SEEK_SET);
fseek(fp2,0,SEEK_SET);

// check for the total number of bytes


if( cnt1 != cnt2 ){
printf("\nFile contents are not same\n");
}
else
{
while( ! feof(fp1) )
{
if( fgetc(fp1) != fgetc(fp2) )
{
flg = 1;
break;
}
}

if( flg ) printf("\nFile contents are not same.\n");


else printf("\nFile contents are same.\n");
}

fclose(fp1);
fclose(fp2);

return 0;
}

Output

Terminal command: ./compPrg file1.txt file2.txt


File contents are not same.
19. C program to calculate sum, product of all One
Dimensional Array Elements
#include <stdio.h>

int main()
{
int arr[10];
int sum,product,i;

/*Read array elements*/


printf("\nEnter elements : \n");
for(i=0; i<10; i++)
{
printf("Enter arr[%d] : ",i);
scanf("%d",&arr[i]);
}

/*calculate sum and product*/


sum=0;
product=1;
for(i=0; i<10; i++)
{
sum=sum+arr[i];
product=product*arr[i];
}

printf("\nSum of array is : %d" ,sum);


printf("\nProduct of array is : %d\n",product);

return 0;
}

Output

Enter elements :
Enter arr[0] : 11
Enter arr[1] : 22
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 5
Enter arr[5] : 66
Enter arr[6] : 7
Enter arr[7] : 8
Enter arr[8] : 9
Enter arr[9] : 10

Sum of array is : 145


Product of array is : 534965504
20. C program to find SUM and AVERAGE of two integer
Numbers using User Define Functions
#include <stdio.h>

/*function declarations*/
int sumTwoNum(int, int); /*to get sum*/
float averageTwoNum(int, int); /*to get average*/

int main()
{
int number1, number2;
int sum;
float avg;

printf("Enter the first integer number: ");


scanf("%d", &number1);

printf("Enter the second integer number: ");


scanf("%d", &number2);

/*function calling*/
sum = sumTwoNum(number1, number2);
avg = averageTwoNum(number1, number2);

printf("Number1: %d, Number2: %d\n", number1, number2);


printf("Sum: %d, Average: %f\n", sum, avg);

return 0;
}

int sumTwoNum(int x, int y)


{
/*x and y are the formal parameters*/
int sum;
sum = x + y;
return sum;

}
float averageTwoNum(int x, int y)
{
/*x and y are the formal parameters*/
float average;
return ((float)(x) + (float)(y)) / 2;

Output:

Enter the first integer number: 100


Enter the second integer number: 201
Number1: 100, Number2: 201
Sum: 301, Average: 150.500000

You might also like