Expriment No.-: Title-Name - Roll No - Class - PRN No. - Subj
Expriment No.-: Title-Name - Roll No - Class - PRN No. - Subj
- 3
Title- Program for if - else statement
#include<stdio.h>
int main(){
int num;
setbuf(stdout,NULL);
printf("enter number :");
scanf("%d",&num);
if(num%2==0){
printf("given number is even number");
}
else{
printf("given number is odd number");
}
}
Output
enter number :
35
given number is odd number
#include <stdio.h>
int main() {
char op;
int num1, num2;
setbuf(stdout,NULL);
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%d %d", &num1, &num2);
switch (op) {
case '+':
printf("%d + %d = %.d", num1,num2,num1+num2);
break;
case '-':
printf("%d + %d = %.d", num1,num2,num1+num2);
break;
case '*':
printf("%d + %d = %.d", num1,num2,num1+num2);
break;
case '/':
printf("%d + %d = %.d", num1,num2,num1+num2);
break;
// operator doesn't match any case constant
Default:
printf("Error! operator is not correct");
}
return 0;
}
Outut :
Enter an operator (+, -, *, /): +
Vision:Enter
To be
twoanoperands:
excellent department
10 20 of the institute promoting academic as well
as holistic
10 +development
20 = 30 of students and
encouraging their creative potential.
Mission: M1: To develop technical and communicative skills to make the students
industry ready.
M2: To promote innovation and research for catering to the needs of the
society at large
M3: To inculcate ethical values and promote social responsibility through
an innovative learning process in basic
sciences and humanities
Expriment No.- 3
Title- Program read a number and display its reverse using while loop
#include<stdio.h>
int main()
{
int i=10;
while(i>=1)
{
printf(“%d \n”,i);
i--;
return 0;
}
Output :
10
9
8
7
6
5
4
3
2
#include<stdio.h>
int fact(int f);
int main()
{
int n,factorial;
setbuf(stdout,NULL);
printf(“Enter a number to find factorial”);
scanf(“%d”,&n);
factorial= fact(n);
printf("Factorial of a Given Number is: %d ",factorial);
return 0;
}
int fact(int f)
{
int fact=1;
for(i=1; i<=f; i++)
{
fact=fact*i;
}
return fact;
}
Output :
#include<stdio.h>
int sum(int n);
int main()
{
int num, result;
printf(“Enter a number:”);
scanf(“%d”,&num);
result = sum(num);
printf(“Sum of natural numbers :%d\n”,result);
return 0;
}
int sum(int n)
{
int k;
if(n==0)
{
return 0;
}
k = n +sum(n-1);
return (k);
}
Output :
Enter a number:3
Sum of natural numbers : 6
#include <stdio.h>
#include <string.h>
int main()
{
int len1, len2;
Output :
Length : 13
Length : 13
#include <stdio.h>
#include <string.h>
int main()
{
// defining strings
char source[] = "Humpty";
char target[20];
// Copying the source string to dest
strcpy(target, source);
// printing result
printf("Source: %s\n", source);
printf("Target: %s\n", target);
return 0;
}
Output :
Source : Humpty
Target : Humpty
#include <stdio.h>
int main()
{
char target[50] = "PPS";
char source[50] = " Welcome";
// concatenating src at the end of dest
strcat(target,source);
printf("Source: %s\n", source);
printf("Target: %s\n", target);
return 0;
}
Output :
Source : PPS
Target: WelcomePPS
#include <stdio.h>
int main() {
int matrix1[2][2]={{1,2},{3,4}};
int matrix2[2][2]={{5,6},{7,8}};
,int sum[2][2];
// Add the two matrices
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Display the result
printf("Resultant 2x2 Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Resultant 2x2 Matrix:
68
10 12
#include <stdio.h>
int main() {
int num1, num2, temp;
int *ptr1 = &num1, *ptr2 = &num2;
printf("Enter two numbers:\n");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", *ptr1, *ptr2);
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
printf("After swapping: num1 = %d, num2 = %d\n", *ptr1, *ptr2);
return 0;
}
#include <stdio.h>
struct Student
{
int rollno;
char name[50];
float marks;
};
int main()
{
struct Student s1;
setbuf(stdout,NULL);
printf("Enter roll number: ");
scanf("%d", &s1.rollno);
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter marks: ");
scanf("%f", &s1.marks);
printf("\n--- Student Details ---\n");
printf("Roll Number: %d\n", s1.rollno);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
Output :
Enter roll number 11
Enter name : john
Enetr marks : 76.8
Student Details
Roll number :11
Vision: To be an excellent department of the institute promoting academic as well
Name :john
as holistic development of students and
Marks:76.8 encouraging their creative potential.
Mission: M1: To develop technical and communicative skills to make the students
industry ready.
M2: To promote innovation and research for catering to the needs of the
society at large
M3: To inculcate ethical values and promote social responsibility through
an innovative learning process in basic
sciences and humanities
Expriment No.- 10
Title- File open in read mode.
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("test.txt","r");
if(fp != NULL)
{
printf("File open successfull");
}
else
{
printf("File open unsuccessfull");
}
return 0;
}
#include<stdio.h>
int main()
{
FILE *fp;
char content[1000];
fp=fopen("test.txt","r");
if(fp != NULL)
{
fgets(content,1000,fp);
printf("%s",content);
}
else
{
printf("File open unsuccessfull");
}
return 0;
}
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("newfile.txt","w");
fputs("I love c programmimg",fp);
fclose(fp);
return 0;
}