CS3271-C Programming (2nd Batch)
CS3271-C Programming (2nd Batch)
Program:
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
if (num1 > num2) {
printf("The maximum number is: %d\n", num1);
} else if (num2 > num1) {
printf("The maximum number is: %d\n", num2);
} else {
printf("Both numbers are equal.\n");
}
return 0;
}
O/P:
Enter the first number: 9
Enter the second number: 11
The maximum number is: 11
Program:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
O/P:
1
2
3
4
5
6
7
8
9
10
3. Write a C program to concatenate two strings.
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[50];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
O/P:
Enter the first string: Hello
Enter the second string: World
Concatenated string: HelloWorld
O/P:
Enter three numbers: 4 9 2
The biggest number is: 9
5. Write a C program to compare two strings using pointers.
Program:
#include <stdio.h>
int main() {
char str1[100], str2[100];
char *p1, *p2;
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
p1 = str1;
p2 = str2;
while (*p1 == *p2) {
if (*p1 == '\0') break;
p1++;
p2++;
}
if (*p1 == '\0' && *p2 == '\0')
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
O/P:
Enter first string: hello
Enter second string: hello
Strings are equal.
7. Write a C program to swap values of two variables with and without using third variable.
Program:
O/P:
Enter a: 4
Enter b: 5
After swapping:
a=5
b=4
O/P:
Enter a number: 4
Factorial is 24
O/P:
Enter number: 456
Reversed: 654
10. Write a C program in C to copy one string to another string.
Program:
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0;
printf("Enter a string: ");
scanf("%s", str1);
while (str1[i] != '\0') {
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
printf("Copied string: %s\n", str2);
return 0;
}
O/P:
Enter a string: hello
Copied string: hello