exercises

Simple program exercise – c language

Program to print full pyramid using *
#include
int main()
{
int i, space, rows, k=0;

printf(“Enter number of rows: “);
scanf(“%d”,&rows);

for(i=1; i<=rows; ++i, k=0) { for(space=1; space<=rows-i; ++space) { printf(" "); } while(k != 2*i-1) { printf("* "); ++k; } printf("\n"); } return 0; }

TRY

Program to print pyramid using numbers
Program to print half pyramid using *
Program to Check Even or Odd
#include
int main()
{
int number;

printf(“Enter an integer: “);
scanf(“%d”, &number);

// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf(“%d is even.”, number);
else
printf(“%d is odd.”, number);

return 0;
}

TRY

Program to Check Odd or Even Using Conditional Operator

Program to Count Number of Digits in an Integer
#include
int main()
{
long long n;
int count = 0;

printf(“Enter an integer: “);
scanf(“%lld”, &n);

while(n != 0)
{
// n = n/10
n /= 10;
++count;
}

printf(“Number of digits: %d”, count);
}

Remove Characters in String Except Alphabets
#include

int main()
{
char line[150];
int i, j;
printf(“Enter a string: “);
gets(line);

for(i = 0; line[i] != ‘\0’; ++i)
{
while (!( (line[i] >= ‘a’ && line[i] <= 'z') || (line[i] >= ‘A’ && line[i] <= 'Z') || line[i] == '\0') ) { for(j = i; line[j] != '\0'; ++j) { line[j] = line[j+1]; } line[j] = '\0'; } } printf("Output String: "); puts(line); return 0; }

Copy String Manually Without Using strcpy()
#include
int main()
{
char s1[100], s2[100], i;

printf(“Enter string s1: “);
scanf(“%s”,s1);

for(i = 0; s1[i] != ‘\0’; ++i)
{
s2[i] = s1[i];
}

s2[i] = ‘\0’;
printf(“String s2: %s”, s2);

return 0;
}