Chapter 1 - Introduction To C
I. Fill in the blanks.
Chapter 2 - First C Program
I. Fill in the blanks
II. Write programs.
main()
{
printf(“P.Srikanth”);
printf(“\n304, Srikanth Technologies, Eswar Paradise, Dwarkanagar, Vizag-16”);
}
main()
{
printf(“P.Srikanth”);
printf(“\n304\tSrikanth Technologies\tEswar Paradise\tDwarkanagar\tVizag-16”);
}
main()
{
printf(“\a \a \a \a\a Bell is over”);
}
Chapter 3 - Language Elements
I. Fill in the blanks.
II. Identify invalid identifiers.
III. Write program.
main()
{
int n1,n2;
/* Accept two numbers */
printf("Enter two numbers :");
scanf("%d%d",&n1,&n2);
printf("Octal of %d = %o Hex of %d = %x",n1,n1,n2,n2);
}
main()
{
float amt, disper,net;
/* accept amount and discount percentage */
printf("Enter amount and discount percentage : ");
scanf("%f%f", &amt,&disper);
net = amt - ( amt * disper / 100 );
printf("Net amount : %f ", net);
}
main()
{
int n1,n2;
/* accept two numbers */
printf("Enter two numbers ");
scanf("%d%d", &n1, &n2);
/* print numbers in the reverse order */
printf("Numbers in reverse order : %d %d ", n2, n1);
}
Chapter 4 - Operators
I. Fill in the blanks
II. Write Programs.
Sum, difference, product, quotient, and remainder.
main()
{
int n1,n2;
/* accept two numbers */
printf("Enter two numbers ");
scanf("%d%d", &n1, &n2);
/* print sum, differnce, product and quotient and remainder */
printf("\nSum = %d ", n1 + n2);
printf("\nDifference = %d ", n1 - n2);
printf("\nProduct = %d ", n1 * n2);
printf("\nQuotient = %d ", n1 / n2);
printf("\nRemainder = %d ", n1 % n2);
}
main()
{
int n;
/* accept a number */
printf("Enter a number ");
scanf("%d",&n);
printf("\nSqare of %d = %d ", n , n * n );
}
main()
{
float pamt, rate, interest;
int ny;
printf("Enter principal amount : ");
scanf("%f",&pamt);
printf("Enter interest rate : ");
scanf("%f",&rate);
printf("Enter number of years : ");
scanf("%d",&ny);
interest = pamt * rate * ny / 100;
printf("Total Simple Interest = %f", interest );
}
main()
{
int a,b;
printf("Enter value for a and b : ");
scanf("%d%d",&a,&b);
printf(" Result of = %d " , a * a + b * b);
}
Chapter 5 - Control Structure
Write Programs
main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
if ( n % 2 == 0 )
printf(" %d is even number");
else
printf(" %d is odd number");
}
/* using conditional expression */
main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf(" %d is %s ", n, n % 2 == 0 ? "Even" : "Odd");
}
main()
{
int marks,pm,subject;
printf("Enter subject code [1-4]: ");
scanf("%d",&subject);
printf("Enter marks : ");
scanf("%d",&marks);
switch(subject)
{
case 1:
pm = 40; break;
case 2:
pm = 50; break;
case 3:
pm = 45; break;
case 4:
pm = 55; break;
}
if (marks >= pm )
printf(" Passed");
else
printf(" Failed");
}
main()
{
int n1,n2;
printf("Enter two numbers : ");
scanf("%d%d",&n1,&n2);
if ( n1 == n2 )
printf("Both the numbers are equal");
else
if ( n1 > n2 )
printf("First number is bigger");
else
printf("Second number is bigger");
}
main()
{
int n1,n2,n3,big;
printf("Enter three numbers : ");
scanf("%d%d%d",&n1,&n2,&n3);
big = n1 > n2 ? n1 : n2;
big = n3 > big ? n3: big;
printf(" The biggest of %d %d %d is %d ", n1,n2,n3,big);
}
Chapter 6 - Looping Structures
I. Fill in the blanks
II.Write programs
main()
{
int i;
for ( i = 100 ; i >= 0 ; i --)
printf("%d\n", i);
}
main()
{
int i,n,sum=0;
printf("Enter a number: ");
scanf("%d", &n);
for ( i = 1 ; i <= n ; i ++)
sum += i;
printf(" Sum of 1 to %d = %d ", n,sum);
}
main()
{
int i;
for ( i = 100 ; i <= 200 ; i += 2)
printf("%d\n",i);
}
main()
{
int i=0,n;
float sum=0;
while (1) /* always true loop */
{
printf("Enter a number [0 to stop] : ");
scanf("%d", &n);
if ( n == 0 )
break; /* terminate loop */
sum += n;
i++;
}
printf(" Sum = %f Average = %f ",sum, sum / i);
}
main()
{
int i,n,big,small;
big = -32768; /* set big to smallest possible number */
small = 32767; /* set small to biggest possible number */
for ( i=1; i <= 10 ; i++)
{
printf("Enter a number : ");
scanf("%d", &n);
if(n > big )
big = n;
if ( n < small )
small = n;
}
printf(" The smallest = %d and biggest = %d ", small, big);
}
Sample output:
7 * 1 = 7
7 * 2 = 14
...
7 * 20 = 140
main()
{
int i;
for ( i = 1; i <= 20 ; i++)
printf(" 7 * %d = %d\n", i, 7 * i);
}
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
main()
{
int i,j;
for ( i = 1; i <= 5 ; i++)
{
printf("\n");
for ( j = 1 ; j <= 5 ; j ++)
printf("%5d", j);
}
}
Chapter 7 - Character Handling
I. Fill in the blanks.
II. Write programs.
#include <ctype.h>
#include <stdio.h>
main()
{
char ch;
int i,nu,nd,nl;
nu = nd = nl = 0;
for ( i = 1 ; i <= 10 ; i ++)
{
ch = getchar();
if (isupper(ch))
nu ++;
else
if (islower(ch))
nl ++;
else
if (isdigit(ch))
nd ++;
}
}
#include <ctype.h>
#include <stdio.h>
main()
{
char ch;
printf("Enter a character : ");
ch = getchar();
if ( isupper(ch))
putchar( tolower(ch));
else
if (islower(ch))
putchar(toupper(ch));
}
#include <ctype.h>
#include <stdio.h>
main()
{
char ch;
while (1)
{
printf("Enter a character : ");
ch = getchar();
fflush(stdin); /* clear keyboard buffer */
if ( ch == '*' )
break;
putch( tolower(ch));
}
}
Chapter 8 - Arrays
Write Programs
main()
{
int ar[20];
int i, sum = 0;
for ( i = 0 ; i < 20 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
sum += ar[i];
}
printf("Entered Numbers \n");
for ( i = 0 ; i < 20 ; i++)
{
printf("%d\n", ar[i]);
}
printf("Sum = %d Average = %d ", sum, sum / 20);
}
main()
{
int ar[10];
int i, j, temp;
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
/* sort array in descending order */
for ( i = 0 ; i < 9 ; i++)
{
for ( j = i+1; j < 10 ; j ++)
{
if ( ar[j] > ar[i])
{
/* interchange */
temp = ar[j];
ar[j] = ar[i];
ar[i] = temp;
}
} /* end of j loop */
} /* end of i loop */
/* display sorted array */
printf("Entered Numbers \n");
for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
}
main()
{
int ar[20];
int i,avg,sum=0;
for (i = 0 ; i < 20 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
sum += ar[i];
}
avg = sum / 10;
/* Display values that are below average of the array */
for ( i = 0 ; i < 20 ; i++)
{
if ( ar[i] < avg )
printf("%d \n", ar[i]);
}
}
main()
{
int ar[10];
int i,num, pos;
for (i = 0 ; i < 9 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
/* take number and position */
printf("Enter position and number : " );
scanf("%d%d", &pos, &num);
/* push all number from position to right */
for ( i = 9 ; i > pos ; i-- )
ar[i] = ar [i - 1];
/* insert new value at position */
ar[pos] = num;
/* Display array after insertion */
for ( i = 0 ; i < 10 ; i++)
{
printf("%d \n", ar[i]);
}
}
main()
{
int ar[10];
int i,temp;
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element : ",i);
scanf("%d", &ar[i]);
}
/* interchange first 5 elements with last 5 elements */
for ( i = 0 ; i < 5 ; i-- )
{
temp = ar [i];
ar[i] = ar [9-i];
ar[9-i] = temp;
}
/* Display array after interchange */
for ( i = 0 ; i < 10 ; i++)
{
printf("%d \n", ar[i]);
}
}
Chapter 9 - Multidimensional Array
main()
{
int ar[5][5];
int i,j;
for (i = 0 ; i < 5 ; i++)
{
for( j = 0 ; j < 5 ; j ++)
{
printf("Enter number for [%d][%d] element : ",i,j);
scanf("%d", &ar[i][j]);
}
}
/* display array as a matrix */
for (i = 0 ; i < 5 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 5 ; j ++)
{
printf("%5d",ar[i][j]);
}
}
}
main()
{
int ar[4][4];
int i,j;
for (i = 0 ; i < 4 ; i++)
{
for( j = 0 ; j < 4 ; j ++)
{
if ( i == j )
ar[i][j] = 1;
else
ar[i][j] = 0;
}
}
/* display array as a matrix */
for (i = 0 ; i < 4 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 4 ; j ++)
{
printf("%5d",ar[i][j]);
}
}
}
main()
{
int ar[5][5];
int i,j,num;
for (i = 0 ; i < 5 ; i++)
{
for( j = 0 ; j < 5 ; j ++)
{
printf("Enter number for [%d][%d] element : ",i,j);
scanf("%d", &ar[i][j]);
}
}
printf("Enter a number : ");
scanf("%d", &num);
/* display all elements that are greater than the
given number */
for (i = 0 ; i < 5 ; i++)
{
for( j = 0 ; j < 5 ; j ++)
{
if ( ar[i][j] > num )
printf("%d \n",ar[i][j]);
}
}
}
#include <stdlib.h>
main()
{
int ar[6][6];
int i,j,temp;
for (i = 0 ; i < 6 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 6 ; j ++)
{
ar[i][j] = random(100);
printf("%5d",ar[i][j]);
}
}
/* interchange */
for (i = 0 ; i < 3 ; i++)
{
for( j = 0 ; j < 6 ; j ++)
{
temp = ar[i][j];
ar[i][j] = ar[5-i][j];
ar[5-i][j] = temp;
}
}
printf("\n\n");
for (i = 0 ; i < 6 ; i++)
{
/* move to next line before each row */
printf("\n");
for( j = 0 ; j < 6 ; j ++)
{
printf("%5d",ar[i][j]);
}
} /* end of i loop */
} /* end of main */