1. Write a program in C to check whether a entered number is positive, negative or zero.
#include <stdio.h>
int main(){
int num;
printf("Input a number :");
scanf("%d", &num);
if (num >= 0)
printf("%d is a positive number \n", num);
else
printf("%d is a negative number \n", num);
return 0;
}
2. Write a program in C to add two matrixes of size 3x3.
#include<stdio.h>
int main()
int matA[3][3],matB[3][3],matC[3][3];
int r,c,k;
for(r=0; r<3; r++)
for(c=0; c<3; c++)
printf("Enter first matrix : ");
scanf("%d", &matA[r][c]);
//printing matA
for(r=0; r<3; r++)
for(c=0; c<3; c++)
printf(" %d",matA[r][c]);
printf("\n");
//getting matB
for(r=0; r<3; r++)
for(c=0; c<3; c++)
printf("Enter second matrix : ");
scanf("%d", &matB[r][c]);
}
}
//printing matB
for(r=0; r<3; r++)
for(c=0; c<3; c++)
printf(" %d",matB[r][c]);
printf("\n");
//adding matA+matB
for(r=0; r<3; r++)
for(c=0; c<3; c++)
matC[r][c]=0;
for(k=0; k<3;k++)
matC[r][c] = matA[r][c] + matB[r][c];
printf("\n New addition matrix : \n");
for(r=0; r<3; r++)
for(c=0; c<3; c++)
printf(" %d",matC[r][c]);
printf("\n");
return 0;
}
3. Write a program to enter a number and check its length.
#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
// iterate until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
while (n != 0) {
n /= 10; // n = n/10
++count;
printf("Number of digits: %d", count);
return 0;
}
4. Write a program to take a string and check the number of vowels, consonants, digits and space in
it.
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '\0'; ++i) {
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
line[i] == 'U') {
++vowels;
} else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
++consonant;
} else if (line[i] >= '0' && line[i] <= '9') {
++digit;
} else if (line[i] == ' ') {
++space;
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
return 0;
}
5. Write a program to enter multiple number of strings as the user wishes and arrange them in
dictionary order.
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
puts("Number of string to be entered: ");
scanf("%d",&count);
puts("\nEnter Strings one by one: ");
for(i=0;i<=count;i++)
gets(str[i]);
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
printf("\nOrder of Sorted Strings:");
for(i=0;i<=count;i++)
puts(str[i]);
return 0;