Friday, February 5, 2016

C program to check Identity Matrix.

#include<stdio.h>
int main()
{
int array1[3][3],i,j,flag=0; 
for ( i = 0; i < 3; i++ ){ //related to row
for ( j = 0; j < 3; j++){ //related to column
printf("Element:");
scanf("%d", &array1[i][j]) ;
}

/* output each array element's value */ 
for ( i = 0; i < 3; i++ )
for ( j = 0; j < 3; j++ )
printf(" %d\t ",array1[i][j] ); 

printf("\n");

for ( i = 0; i < 3; i++ )
for ( j = 0; j < 3; j++ )
if(array1[i][j] != 1 && array1[j][i] != 0 ) {
flag=1; break;
}

printf("\n");
}
if(flag==0)
printf("It is an Identity Matrix.");
else
printf("It is not an Identity Matrix.");
 return 0;
}

C program to print initial of given name

#include<stdio.h>
int main()
{
  char str[20];
  int i=0;
  printf("Enter a string: ");
  gets(str);

  printf("%c",str[0]);
  while(str[i]!='\0')
  {
if(str[i]==' ')
{
     i++;
     printf("%c",str[i]);
}
i++;
  }
 return 0;
}

Output of program

Enter a string: ABC PATEL
AP



C program to read and display 3X3 integer Matrix using 2-D array.

#include<stdio.h>
int main()
{
  int row,col, arr[3][3];
  //Logic to read matrix
  for(row=0; row<3; row++) // rows
  {
    for(col=0; col<3; col++) //columns
    {
       printf("Enter matrix number[%d][%d]:", row, col);
       scanf("%d", &arr[row][col]);
    }
  }
  //Logic to print matrix
  printf("The 3 X 3 matrix:\n");
  for(row=0; row<3; row++) // rows
  {
    for(col=0; col<3; col++) //columns
    {
       printf("%d ", arr[row][col]);
    }
    printf("\n");
  }
  return 0;
}

Output of program

Enter matrix number[0][0]:1
Enter matrix number[0][1]:2
Enter matrix number[0][2]:3
Enter matrix number[1][0]:4
Enter matrix number[1][1]:5
Enter matrix number[1][2]:6
Enter matrix number[2][0]:7
Enter matrix number[2][1]:8
Enter matrix number[2][2]:9
The 3 X 3 matrix:
1 2 3
4 5 6
7 8 9



C program to read and display 5 integer values in 1-D array.

#include<stdio.h>
int main()
{
int i=0, number[5];
for(i=0; i<5; i++) // This loop tracks array index 
{
printf("Enter your Number[%d]:",i);
scanf("%d",&number[i]);
}
//Logic to print number array.
for(i=0; i<5; i++) // This loop tracks array index 
{
printf("Number[%d]:%d \n", i, number[i]);
}
}

Output of program

Enter your Number[0]:2
Enter your Number[1]:4
Enter your Number[2]:6
Enter your Number[3]:8
Enter your Number[4]:10
Number[0]:2
Number[1]:4
Number[2]:6
Number[3]:8
Number[4]:10

Thursday, February 4, 2016

C program to count A in a given string.

#include<stdio.h>
int main()
{
char str1[80]="WE ARE IN AHMEDABAD";
int i, A_counter=0;

printf("Given String = %s",str1);

for(i=0; i<strlen(str1); i++)
{
if(str1[i] == 65) //65 is ascii value of A
A_counter = A_counter + 1;
}
printf("\nTotal 'A' in the given string is %d", A_counter );
 return 0;
}

Output of program

Given String = WE ARE IN AHMEDABAD
Total 'A' in the given string is 4


Tuesday, February 2, 2016

C program to count spaces in a given string.

#include<stdio.h>
#include<stdlib.h>

int main(){

  char str1[80]="Hello Student Lets count spaces in this string.";
  int i, space_counter=0;

  printf("Given String = %s",str1);

  for(i=0; i<strlen(str1); i++)
  {
if(str1[i] == 32) //32 is ascii value of space
space_counter++;
  }
  printf("\nTotal space in the given string is %d", space_counter );
  return 0;
}

Output of the program:

Given String = Hello Student Lets count spaces in this string.
Total space in the given string is 7

Interesting C String Processing Program

//Enter more than 10 characters and see the output.

#include<stdio.h>
int main()
{
char str[50],str1[10];

int i;
gets(str); //Enter more than 10 characters

for(i=0; i<10; i++)
str1[i]=str[i];

puts(str1); //You will get surprising output.
 return 0;
}