0% found this document useful (0 votes)
36 views16 pages

C Promgraming

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)
36 views16 pages

C Promgraming

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/ 16

1.

Write a program to demonstrate the use of different type of


datatypes

#include<stdio.h>
int main(){
int x=20;
float y=34.5;
char a='s';
double v=23.67;
printf("integer datatype value=%d\n",x);
printf("decimal datatype value=%f\n",y);
printf("character datatype value=%c\n",a);
printf("double datatype value=%lf\n",v);
return 0;

}
2.Write a program to declare and initialize them

#include<stdio.h>
int main(){
int x;
float y;
char a;
double v;
x=345;
y=234.678;
a='v';
v=453.9876;
printf(" value of x = %d\n",x);
printf(" value of y = %f\n",y);
printf(" value of a = %c\n",a);
printf(" value of v = %lf\n",v);
return 0;

}
3.Write a program to illustrate the use of arithmetic, logical,
relational operators
#include<stdio.h>
int main(){
int x,y,z,sum,sub,mul;
x=23,y=56, z=45;
sum = x+y;
sub = x-y;
mul = x*y;
printf("\n# Use of arithmetic operators:\n");
printf("addittion =%d \n",sum);
printf("subtraction =%d \n",sub);
printf("Multiplication =%d \n",mul);
printf("\n# Use of relational operator: \n");
if(x>y){
printf("%d is greater than %d \n",x,y);
}
else{printf("%d is greater than %d \n",y,x);}
printf("\n# Use of logical opertor:\n");
if(x>y && x>z){
printf("%d is greatest of all no.",x);
}
else if(y>x && y>z)
{
printf("%d is greatest",y);
}
else{
printf("%d is greatest of all",z);
}
return 0;
}
4.Write a program to check if the number is even or odd

#include<stdio.h>
int main(){
int x;
printf("enter your number:");
scanf("%d",&x);
if(x%2==0){
printf("%d is even.",x);
}
else{
printf("%d is odd.",x);
}
return 0;
}
5.Create the program to print a fibonnaci series using while
loop
#include<stdio.h>
int main()
{
int f1=0,f2=1,f3,i=3,len;
printf("enter length of the fibonacci series:");
scanf("%d",&len);
printf("%d\n%d",f1,f2);
while(i<=len)
{
f3=f1+f2;
printf("\n%d",f3);
f1=f2;
f2=f3;
i=i+1;
}
return 0;
}
6.Write a program using a switch statement to perform
arithmetic operations (addition, subtraction , etc) based on user
input
#include<stdio.h>
int main()
{ int a,b;
int op;
printf(" 1.Addition\t 2.Subtraction\t 3.Multiplication\t 4.Division\n");
printf("Enter your Choice : ");
scanf("%d",&op);
printf("enter your numbers:");
scanf("%d %d",&a,&b);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}
7.Create a recursive function to factorial of the number .

#include<stdio.h>
int fact(int n){
if (n==1) return 1;
return n*fact(n-1);
}
int main(){
int n;
printf("enter your number:");
scanf("%d",&n);
int fa=fact(n);
printf("%d",fa);
return 0;
}
8.Write a program to find the sum and average of an array of
the numbers
#include<stdio.h>
int main(){
int size;
printf("enter the size :");
scanf("%d",&size);
int arr[size];
int sum=0;
for(int i=0;i<size;i++){
printf("enter your elements %d :",i+1);
scanf("%d",&arr[i]);
sum= sum+arr[i];
}

printf("Sum of the given array: %d\n",sum);


printf("Average of the given array: %d",sum/size);
return 0;

}
9.Write a program to perform matrix multiplication
#include <stdio.h>
// function to get matrix elements entered by the user
void getMatrixElements(int matrix[][10], int row, int column) {
printf("\nEnter elements: \n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]); }
}
}
// function to multiply two matrices
void multiplyMatrices (int first[][10], int second[][10], int result[][10], int r1, int c1, int r2,
int c2) {
// Initializing elements of matrix multiply to 0.
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}
// Multiplying first and second matrices and storing it in result
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}
// function to display the matrix
void display(int result[][10], int row, int column) {
printf("\n Output Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main() {
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
// Taking input until 1st matrix columns is not equal to 2nd matrix row
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
// get elements of the first matrix
getMatrixElements(first, r1, c1);
// get elements of the second matrix
getMatrixElements(second, r2, c2);
// multiply two matrices.
multiplyMatrices(first, second, result, r1, c1, r2, c2);

// display the result


display(result, r1, c2);

return 0;
}
10. Write a C program to create a file, write "Hello, World!" into
it, and then close the file.

#include<stdio.h>
int main()
{
FILE *opening;
opening = fopen("kiran.txt","w");
fprintf(opening,"Hello world!");
fclose(opening);
printf("Writing to the file was successful. Closing the program\n");
return 0;
}
11.Create a C program to copy the contents of
one file to another
#include <stdio.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("kiran.txt", "r");
fp2 = fopen("kiran1.txt", "w");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
printf("copied sucessfully");
fclose(fp1);
fclose(fp2);
}
12. Write a program to swap value using call by reference and
call by value in c
#include <stdio.h>
void swap (int *x, int *y);
int main() {
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
}
void swap (int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}

You might also like