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

CS3271-C Programming (2nd Batch)

The document contains ten C programming exercises, each with a program and sample output. Topics include finding maximum numbers, printing integers, string concatenation, using pointers, calculating area and circumference, swapping variables, factorial calculation, reversing integers, and copying strings. Each program is accompanied by user input prompts and corresponding output examples.

Uploaded by

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

CS3271-C Programming (2nd Batch)

The document contains ten C programming exercises, each with a program and sample output. Topics include finding maximum numbers, printing integers, string concatenation, using pointers, calculating area and circumference, swapping variables, factorial calculation, reversing integers, and copying strings. Each program is accompanied by user input prompts and corresponding output examples.

Uploaded by

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

1. Write a C program to input two numbers and display the maximum number.

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

2. Write a C program to print positive integers from 1 to 10.

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

4. Write a C program to find biggest among three numbers using pointer.


Program:
#include <stdio.h>
int main() {
int a, b, c;
int *big;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
big = &a;
if (b > *big) {
big = &b;
}
if (c > *big) {
big = &c;
}
printf("The biggest number is: %d\n", *big);
return 0;
}

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.

Enter first string: hello


Enter second string: hell
Strings are not equal.

6. Write a C program to calculate area and circumference of a circle.


Program:
#include <stdio.h>
int main() {
float r, area, circ;
float pi = 3.14;
printf("Enter radius: ");
scanf("%f", &r);
area = pi * r * r;
circ = 2 * pi * r;
printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circ);
return 0;
}
O/P:
Enter radius: 3
Area = 28.26
Circumference = 18.84

7. Write a C program to swap values of two variables with and without using third variable.
Program:

1) Swap using third variable


#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
temp = a;
a = b;
b = temp;
printf("After swapping:\na = %d\nb = %d\n", a, b);
return 0;
}

O/P:
Enter a: 4
Enter b: 5
After swapping:
a=5
b=4

2) Swap without using third variable


#include <stdio.h>
int main() {
int a, b;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping:\na = %d\nb = %d\n", a, b);
return 0;
}
O/P:
Enter a: 4
Enter b: 5
After swapping:
a=5
b=4

8. Write a C program to find the factorial of a number.


Program:
#include <stdio.h>
int main() {
int n, i;
long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
fact = 1;
for (i = 1; i <= n; i++) {
fact = fact * i;
}
printf("Factorial is %ld\n", fact);
return 0;
}

O/P:
Enter a number: 4
Factorial is 24

9. Write a C program to reverse a given integer.


Program:
#include <stdio.h>
int main() {
int n, rev = 0;
printf("Enter number: ");
scanf("%d", &n);
while (n > 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
printf("Reversed: %d\n", rev);
return 0;
}

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

You might also like