Tuesday, April 7, 2020

Write a program in C to count number of words and characters in a file.

Write a program in C to count number of words and characters in a file.

#include
#include

void main()
{
    FILE *fptr;
    char ch;
    int wrd=1,charctr=1;
    char fname[20];
    printf("\n\n Count the number of words and characters in a file :\n");
printf("---------------------------------------------------------\n");
printf(" Input the filename to be opened : ");
scanf("%s",fname);   

    fptr=fopen(fname,"r");
    if(fptr==NULL)
     {
         printf(" File does not exist or can not be opened.");
      }
    else
        {
          ch=fgetc(fptr);
          printf(" The content of the file %s are : ",fname);
          while(ch!=EOF)
            {
                printf("%c",ch);
                if(ch==' '||ch=='\n')
                    {
                        wrd++;
                    }
                    else
                    {
                        charctr++;
                    }
                ch=fgetc(fptr);
            }
        printf("\n The number of words in the  file %s are : %d\n",fname,wrd-2);
        printf(" The number of characters in the  file %s are : %d\n\n",fname,charctr-1);
        }
    fclose(fptr);
}


Output:

Count the number of words and characters in a file : 
-----------------------------------------------------   
Input the filename to be opened : sample.txt
The content of the file sample.txt are :
test line 1
test line 2
The number of words in the  file test.txt are : 6 
The number of characters in the  file test.txt are : 18

Write a C program to count number of words in a given text or sentence.

Write a C program to count number of words in a given text or sentence.

#include
#include

void main()
{
    char s[200];
    int count = 0, i;

    printf("Enter the string:\n");
    scanf("%[^\n]s", s);
    for (i = 0;s[i] != '\0';i++)
    {
        if (s[i] == ' ' && s[i+1] != ' ')
            count++;    
    }
    printf("Number of words in given string are: %d\n", count + 1);
}





Output:

Enter the string:
welcome to vijaymarwaha.blogspot.com blog
Number of words in given string are: 4

Write a program in php to print the multiplication table of 9

Write a program in php to print the multiplication table of 9:

Answer:

<? php
define('a', 9);   
for($i=1; $i<=10; $i++)   
{   
  echo $i*a;   
  echo '<br>';     
}  
?>

Write a php program to print factorial of a number

Write a php program to print factorial of a number:
Answer:

$num = 7; 
$factorial = 1; 
for ($x=$num; $x>=1; $x--) 

  $factorial = $factorial * $x; 

echo "Factorial of $num is $factorial"; 
?>


Output:

Factorial of 7 is 5040

What can you never eat for breakfast?

What can you never eat for breakfast?

Answer:
 Dinner.

What three letters change a girl into a woman?

Question:
What three letters change a girl into a woman?

Answer:
AGE.

Monday, April 6, 2020

What is the extension of PHP?

.php is the extension of PHP

PHP originally stood for

PHP originally stood for:
Personal Home Page in 1994


The acronym was formally changed to PHP: HyperText Preprocessor, in 1997

Write a php program to check wether a number is prime or not

Write a php program to check wether a number is prime or not 

<?php 
// PHP code to check wether a number is prime or Not

function primeCheck($number)

    if ($number == 1) 

    return 0; 

    for ($i = 2; $i <= $number/2; $i++)

        if ($number % $i == 0) 

            return 0; 

    } 

    return 1; 

  
$number = 31; 

$flag = primeCheck($number); 

if ($flag == 1) 

    echo "Prime number"; 
else

    echo "Not Prime number"
?>

Write a program in php to check given number is even or odd

Write a program in php to check given number is even or odd

Answer:
<html>
<body>
<h3>Odd or Even PHP Example</h3>
<form action="" method="post">
<label>Enter a number</label>
<input type="text" name="number" />
<input type="submit" />
</form>
</body>
</html>
<?php 
if($_POST){
$number = $_POST['number']; 
//check if the enter value is a number or throw an error
if (!(is_numeric($number) && is_int(0+$number))){
echo "<p style='color:red;'>Error: You entered a string. Please enter an Integer</p>";
die();
}
//divide the entered number by 2 
//if the reminder is 0 then the number is even otherwise the number is odd
if(($number % 2) == 0){
echo "You entered an Even number";
}else{
echo "You entered an Odd number";
}
}
?>

Featured posts

Mongolia

 Mongolia! Mongolia is a vast and sparsely populated country in East Asia, known for its stunning natural beauty, rich history, and unique c...

Popular posts