0% found this document useful (0 votes)
27 views33 pages

C Programs

Uploaded by

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

C Programs

Uploaded by

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

// a program to use switch case to perform mathematical

functions
#include<stdio.h>

int main()
{
int a,b,choice;

int sum,sub,mul;
float div;
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("enter 1 for addition\n”);
printf("enter 2 for subtraction\n”);
printf("enter 3 for division\n”);
printf("enter 4 for multiplication\n”);
printf("Enter your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :

sum=a+b;
printf(“the sum is %d",sum);
break;
case 2 :

sub=a-b;
printf(“the difference is %d",sub”);
break;
case 3 :

div=a/b;
printf(“the quotient is %f",div);
break;
case 4 :

mul=a*b;
printf(“the multiplication is %d",mul);
break;
default :
printf(" Enter Correct Choice(1-4)");
break;
}
return 0;
}

//A program to compare two numbers


#include<stdio.h>
void main()
{
int a, b;
printf(“enter the value for a,b\n”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is bigger\n”);
else
printf(“b is bigger\n”);
}

// a program to compare three numbers


#include <stdio.h>

int main()
{
int A = 10, B = 22, C = 9;
printf("The numbers A, B and C are: %d, %d, %d\n", A,
B,C);

if (A >= B)
{
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}

return 0;

// a program to calculate square of entered


number
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int square = num * num;
printf("The square of %d is %d", num, square);
return 0;
}

// program to calculate average of real numbers


#include <stdio.h>
int main ( )
{
int num1, num2, num3, sum;
float avg;
printf("Enter the First Number = ");
scanf ("%d", &num1) ;
printf("Enter the Second Number = ");
scanf ("%d", &num2) ;
printf("Enter the Third Number = ");
scanf ("%d", &num3) ;
sum = num1 + num2 + num3;
avg = sum / 3;
printf("\nThe Sum of Three Numbers= %d", sum);
printf("\nThe Average of Three Numbers = %f\n",avg);

// C program to print ASCII Value of Character


#include <stdio.h>
int main()
{
char c = 'k';

// %d displays the integer value of a


character
// %c displays the actual character
printf("The ASCII value of %c is %d", c,
c);
return 0;
}

/*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
printf("\nSize of char is: %ld", sizeof(charType));
// Calculate and Print the size of floatType
printf("\nSize of float is: %ld", sizeof(floatType));

// Calculate and Print the size of doubleType


printf("\nSize of double is: %ld", sizeof(doubleType));
return 0;
}

/ C program to Demonstrate the


// Multiplication table of a number
#include <stdio.h>
void print_table(int range, int num)
{
// Declaring a variable mul to store the product.
int mul;

// For loop to calculate the Multiplication table.


for (int i = 1; i <= range; i++) {
// To store the product.
mul = num * i;

// Printing the Multiplication Table.


printf("%d * %d = %d", num, i, mul);

// Proceeding to the next line.


printf("\n");
}
}
// Driver code
int main()
{

// The range of the


// Multiplication table
int range = 10;

// The number to calculate the


// Multiplication table
int num = 5;

// Calling the Function.


print_table(range, num);

return 0;
}

// Multiplication table of a number


#include <stdio.h>
int main()
{
int i=1,mul,range=10,num;
printf(“enter the value of number for which table isto be printed\n”);
scanf(“%d”,&num);
while( i <= range)
{
mul = num * i;
// Printing the Multiplication Table.
printf("%d * %d = %d", num, i, mul);

printf("\n");
i++;
}
Return 0;
}

// Driver code
int main()
{

// The range of the


// Multiplication table
int range = 10;

// The number to calculate the


// Multiplication table
int num = 5;

// Calling the Function.


print_table(range, num);

return 0;
}

#include<stdio.h>

int main()

char c;

int lowercase_vowel, uppercase _vowel;

printf(“enter an alphabet”);

scanf(“%c”, &c);

lowercase_vowel=(c == ‘a’ || c == ‘e’ || c == ‘i’ || c == ‘o’ || c == ‘u’);

uppercase_vowel=( c == ‘A’ || c == ‘E’ || c == ‘I’ || c == ‘O’ || c == ‘U’);

if (lowercase_vowel || uppercase_vowel)

printf(“%c is a vowel \n”, c);

else

printf(“%c is a consonant\n”, c);

return 0;

// C program to implement mathematical function


#include <math.h>
#include <stdio.h>
int main()
{
double number, squareroot;
number = 12.5;
// Computing the square root
squareroot = sqrt(number);
printf("Square root of %.2lf = %.2lf",number,
squareroot);
return 0;
}
// aprogram to find power of a number
#include <math.h>
#include <stdio.h>
int main()
{
double base, power, result;
base = 10.0;
power = 2.0;
result = pow(base, power);
printf("%.1lf^%.1lf = %.2lf",base, power, result);
return 0;}
// a program to use logarithm functions
#include<stdio.h>
#include <math.h>
int main()
{
float i = 0.314;
float j = 0.25;
float k = 6.25;
float sin_value = sin(i);
float cos_value = cos(i);
float tan_value = tan(i);
float sinh_value = sinh(j);
float cosh_value = cosh(j);
float tanh_value = tanh(j);
float log_value = log(k);
float log10_value = log10(k);
float exp_value = exp(k);

printf("The value of sin(%f) : %f \n", i, sin_value);


printf("The value of cos(%f) : %f \n", i, cos_value);
printf("The value of tan(%f) : %f \n", i, tan_value);
printf("The value of sinh(%f) : %f \n", j, sinh_value);
printf("The value of cosh(%f) : %f \n", j, cosh_value);
printf("The value of tanh(%f) : %f \n", j, tanh_value);
printf("The value of log(%f) : %f \n", k, log_value);
printf("The value of log10(%f) : %f \n",k,log10_value);
printf("The value of exp(%f) : %f \n",k, exp_value);
return 0;
}
// C code to illustrate the use of ceil, floor, abs, trunc,
round functions
#include <math.h>
#include <stdio.h>
int main()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = -2.8;
val3 = 1.2;
val4 = -2.3;
int val5, val6, val7,val8;
double x1 = 2.0, x2 = 3.9;
double num1 = 2.3;
double num2 = 3.8;
val5 = abs(22);
val6 = abs(-43);
// long int version of abs()
val7 = labs(1234355L);
val8 = labs(-4325600L);
printf("value1 = %.1f\n", ceil(val1));
printf("value2 = %.1lf\n", ceil(val2));
printf("Value3 = %.1lf\n", floor(val3));
printf("Value4 = %.1lf\n", floor(val4));
printf(" Truncated value is %lf \n", trunc(x1) );
printf(" Truncated value is %lf \n", trunc(x2) );
printf("Rounded value of num1: %.1f\n", round(num1));
printf("Rounded value of num2: %.1f\n", round(num2));
printf(" absolute value = %d\n", val5);
printf("absolute value = %d\n", val6);
printf(" Labsolute value = %d\n", val7);
printf("Labsolute value = %d\n", val8);
return (0);
}
// C program to find the simple interest
//p=principal, r=rate,t=time
#include <stdio.h>
int main()
{
// Input values
float p, r, t, si;
printf(“enter the values of principal, time &rate\n”);
scanf(“%f\n, %f\n, %f\n, &p,&r,&t”);

// Calculate simple interest


si = (p * t * r) / 100;

// Print Simple Interest


printf("Simple Interest = %f\n", &si);
return 0;
}

/*Basic salary of an employee is input through the


keyboard. The DA is 25% of the basic salary while the HRA
is 15% of the basic salary. Provident Fund is deducted at
the rate of 10% of the gross salary(BS+DA+HRA).Program to
Calculate the Net Salary.

*/

#include <stdio.h>

main() {

float basic_sal, da, hra, pf, gross_sal, net_sal;


printf("\nEnter basic salary of the employee: Rs. ");
scanf("%f", &basic_sal);
da = (basic_sal * 25)/100;
hra = (basic_sal * 15)/100;
gross_sal = basic_sal + da + hra;
pf = (gross_sal * 10)/100;
net_sal = gross_sal - pf;
printf("\n\nNet Salary: Rs. %.2f", net_sal);
getch(); }

// C program to implement
// the getchar() function unformatted function
#include <conio.h>
#include <stdio.h>
// Driver code
int main()
{
// Declaring a char type variable
char ch;
printf("Enter the character: ");
// Taking a character from keyboard
ch = getchar();
// Displays the value of ch
printf("%c", ch);
return 0;
}

// C program to implement
// the getchar & putchar() function
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
return 0;
}
// C program to implement
// the gets() & puts() function
#include <stdio.h> //
int main()
{
char name[50];
printf("Enter your text:\n ");
// Reads string from user
gets(name);
printf("Your text is:\n ");
// Displays string
puts(name);
return 0;
}

// C program to implement
// getch() function
#include <conio.h>
#include <stdio.h>

// Driver code
int main()
{
printf("Enter any character: ");

// Reads a character but


// not displays
getch();

return 0;
}
// C program to implement
// the putch() functions
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character:\n ");
// Reads a character from the keyboard
ch = getch();
printf("\nEntered character is: ");
// Displays that character on the console
putch(ch);
return 0;
}

// C program to implement
// the getche() function
#include <conio.h>
#include <stdio.h>

// Driver code
int main()
{
printf("Enter any character: ");
// Reads a character and
// displays immediately
getche();
return 0;
}
#include<stdio.h>
int add(int m, int n); //function declaration
int sub(int x, int y);
int div(int p, int q);
int mul(int a, int b);
main()
{
int a, b, sum, sub1,div1,mul1;
printf(“enter the value of a and b\n”);
scanf(“%d%d”, &a,&b);
sum=add(a,b); // calling of sub function
sub1=sub(a,b);
div1=div(a,b);
mul1=mul(a,b);
printf(“the sum is %d\n”, sum);
printf(“the difference is%d\n”, sub1);
printf(“the division output is%d\n”,div1);
printf(“the multiply output is %d\n”,mul1);
}
int add(int m, int n) //function definition
{
int sum1;
sum1=m+n;
return(sum1);
}
int sub(int x, int y)
{
Int sub1;
sub1=x-y;
return(sub1);
}
int div(int p, int q)
{
float div1;
div1=p/q;
return(div1);
}
int mul(int a, int b)
{
int mul1;
mul1=a*b;
return(mul1);
}
// c program to calculate factorial of number using functions
// C program to Find the Factorial Using for Loop
#include <stdio.h>
int factorial(int n)
{
int fact = 1, i;//local variables
// Loop from 1 to N to get the factorial
for (i = 1; i <= n; i++) //executable statements
{
//fact *= i;
fact=fact*i;
}
return fact;
}
int main()
{
int n, fact;
printf(“enter the number\n”);
scanf(“%d”, &n);
fact = factorial(n);// FUNCTION CALLING
printf("Factorial of %d is %d", n, fact);
return 0;
}

// C program to Find the Factorial Using for Loop


#include <stdio.h>
/*int factorial(int n)
{
int fact = 1, i;//local variables
// Loop from 1 to N to get the factorial
for (i = 1; i <= n; i++) //executable statements
{
fact *= i; //fact=fact*i;
}
return fact;
}*/
int main()
{
int num, fact=1,i;
printf(“enter the number\n”);
scanf(“%d”, &num);
// fact = factorial(num);
for (i = 1; i <= num; i++) //executable statements
{
fact *= i; //fact=fact*i;
}

printf("Factorial of %d is %d", num, fact);


return 0;
}
//output
Factorial of 5 is 120
// C program to find factorial of given number
// using recursion
#include <stdio.h>
int factorial( int n)
{
if (n == 1) // Base Case:
{ return 1; }
return n * factorial(n - 1);
}
int main()
{
int num ;
printf("enter the number\n");
scanf("%d",&num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
//Fibonacci Series using Recursion
#include<stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
int n;
printf(“enter value of n”);
scanf(“%d”,&n);
printf("%d\n", fib(n));
return 0;
}
// C Program to demonstrate array initialization
#include <stdio.h>

int main()
{

// array initialization using initialier list


int arr[5] = { 10, 20, 30, 40, 50 };

// array initialization using initializer list without


// specifying size
int arr1[] = { 1, 2, 3, 4, 5 };

// array initialization using for loop


float arr2[5];
for (int i = 0; i < 5; i++) {
arr2[i] = (float)i * 2.1;
}
return 0;
}

// C Program to illustrate element access using array


// subscript
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 15, 25, 35, 45, 55 };

// accessing element at index 2 i.e 3rd element


printf("Element at arr[2]: %d\n", arr[2]);

// accessing element at index 4 i.e last element


printf("Element at arr[4]: %d\n", arr[4]);

// accessing element at index 0 i.e first element


printf("Element at arr[0]: %d", arr[0]);

return 0;
}

// C Program to demonstrate the use of array


#include <stdio.h>

int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };

// modifying element at index 2


arr[2] = 100;

// traversing array using for loop


printf("Elements in Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

return 0;
}

// a program to get values in array and print elements of array


#include <stdio.h>
int main()
{
int arr[100];
int i, n;
// GET INPUT OF ARRAY ELEMENTS
printf("Enter the size of the array: ");
scanf("%d", &n);

printf("Input %d elements in the array:\n", n);


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

// Display the elements in the array


printf("\nElements in the array are:\n");
for(i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}

// a program to read and print a matrix


#include <stdio.h>
int main()
{
int i, j, m, n;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* Input data in matrix */
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}

/* Display the matrix */


for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
//program to perform the addition of two matrices
#include <stdio.h>

int main() {

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

printf("Enter the number of rows (between 1 and 100): ");

scanf("%d", &r);

printf("Enter the number of columns (between 1 and 100): ");

scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

printf("Enter elements of 2nd matrix:\n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("Enter element b%d%d: ", i + 1, j + 1);

scanf("%d", &b[i][j]);

// adding two matrices


for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

sum[i][j] = a[i][j] + b[i][j];

// printing the result

printf("\nSum of two matrices: \n");

for (i = 0; i < r; ++i)

for (j = 0; j < c; ++j) {

printf("%d ", sum[i][j]);

if (j == c - 1) {

printf("\n\n");

return 0; }

//C Program to illustrate the strcat(string concatenation)function


#include <stdio.h>
#include <string.h>

int main()
{
char dest[50] = "This is an";
char src[50] = " example";

printf("dest Before: %s\n", dest);


// concatenating src at the end of dest
strcat(dest, src);
printf("dest After: %s\n", dest);

return 0;
}

// C program to demonstrate the strlen() function


#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "GeeksforGeeks";
int length = strlen(str);
printf("String: %s\n", str);
printf("Length: %d\n", length);
return 0;
}
// C program to demonstrate the strcmp() function
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Geeks";
char str2[] = "For";
char str3[] = "Geeks";
// Compare 'str1' and 'str2' using strcmp() function and
int result1 = strcmp(str1, str2);
// Compare 'str2' and 'str3' using strcmp() function and
int result2 = strcmp(str2, str3);
// Compare 'str1' and 'str1' using strcmp() function and
int result3 = strcmp(str1, str1);
// Print the result of the comparison between 'str1' and 'str2'
printf("Comparison of str1 and str2: %d\n", result1);
// Print the result of the comparison between 'str2' and 'str3'
printf("Comparison of str2 and str3: %d\n", result2);
// Print the result of the comparison between 'str1' and 'str1'

printf("Comparison of str1 and str1: %d\n", result3);


return 0;
}

// a program to copy one string to another


#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[20], str2[20];
printf("Enter the string: ");
gets(str1);
printf("\nString 1 = %s", str1);
strcpy(str2, str1);
printf("\nString 2 = %s", str2);
getch();
return 0;
}
// C program to reverse a string using strrev()
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
// Reversing string using strrev()
printf("Reversed String: %s", strrev(str));
return 0;
}

strlwr() strupr()
// a program to use strncpy()
#include<stdio.h>
#include<string.h>
int main()
{
char first[100] = "I am studying in PCTE";
char second[100] = "and I am a student of BCA IB";
printf("String s1 before is: %s \n", first);

/* this function has copied first 10 chars of s2 into s1*/


strncpy(first,second, 10);
printf("String s1 after is: %s", first);
return 0;
}
strncmp() strncat()

// whether a number is prime or not


#include <stdio.h>
int main()
{
int i, num, temp = 0;
// read input from user.
printf("Enter any numb to Check for Prime: ");
scanf("%d", &num);
// iterate up to n/2.
for (i = 2; i <= num / 2; i++)
{
// check if num is divisible by any number.
if (num % i == 0)
{
temp++;
break;
}
}
// check for the value of temp and num.
if (temp == 0 && num != 1)
{
printf("%d is a Prime number", num);
}
else
{
printf("%d is not a Prime number", num);
}
return 0;
}
// a program to find whether a number is prime or not
#include <stdio.h>
#include<math.h>

int main()
{
int num;
printf("Enter the number\n");
scanf("%d",&num);
int count=0;
for(int i=2;i<=sqrt(num);i++) //Iterate from 2 to sqrt(num)
{
if(num%i==0)
{
count++;
break;
}
}
if(count!=0) //Check whether prime or not
{
printf("Not a prime number\n");
}
else
{
printf("Prime number\n");
}

return 0;
}

// WRITE A PROGRAM using function to find the largest of three numbers.


#include<stdio.h>

int biggest(int, int, int); // function prototype or declaration

int main()

int a, b, c;

printf("Enter 3 integer numbers\n");

scanf("%d%d%d", &a, &b, &c);

//function call biggest(a, b, c)

printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c)); //call

return 0;

// function definition

int biggest(int x, int y, int z)


{

if(x > y && x > z)

return x;

else

if(y > z)

return y;

else

return z;

// a program to print square of 20 numbers


#include<stdio.h>
int main()
{
int i, sq;
for( i = 1; i <= 20; i++)
{
sq = i * i;
printf(“the square is %d\n”, sq);
}
return 0;
}
// a program to use structure
#include<stdio.h>

struct student
{
char sname[20];
char class[10];
int age;
} struct student std1;

int main()
{
//struct student std1;

printf("Enter the age");


scanf("%d", &std1.age);

printf("Enter the name\n");


scanf("%s", std1.sname);

printf("Enter the class \n");


scanf("%s", std1.class);

printf("____Details are as:______\n");

printf("Name: %s\n", std1.sname);


printf("Class: %s\n", std1.class);
printf("Age: %d \n", std1.age);

// a program to use structure


#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

int main()
{
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i)
{
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}

// C program to pass structure as an argument to the


#include <stdio.h>
struct car
{
char name[30];
int price;
};

void print_car_info(struct car c)


{
printf("Name : %s", c.name);
printf("\nPrice : %d\n", c.price);
}
int main()
{
struct car c = { "Tata", 1021 };
print_car_info(c);
return 0;
}

// C Program to demonstrate Structure pointer


#include <stdio.h>
#include <string.h>
struct Student
{
int roll_no;
char name[30];
char branch[40];
int batch;
};

int main()
{

struct Student s1;


struct Student* ptr = &s1;

s1.roll_no = 27;
strcpy(s1.name, "Kamlesh Joshi");
strcpy(s1.branch, "Computer Science And Engineering");
s1.batch = 2019;
printf("Roll Number: %d\n", (*ptr).roll_no);
printf("Name: %s\n", (*ptr).name);
printf("Branch: %s\n", (*ptr).branch);
printf("Batch: %d", (*ptr).batch);

return 0;
}

You might also like