Wednesday, August 19, 2020

C Program to print first 100 prime numbers

C program to print first 100 Prime numbers

This program is written for one of the the blog viewer upon her request.
 
#include<stdio.h>
int main()
{
  int i, num, count, prime_counter=0; 

  printf("First 100 Prime Numbers are: \n");
  for(num=2 ; prime_counter<100 ; num++)
  {
    count = 0;
    for (i = 2; i <= num/2; i++)
    {
if(num%i == 0)
     count++;
    }
    if(count == 0)
    {
printf(" %d ", num);
prime_counter++;
    }  

  }
  return 0;
}

Output of the program

First 100 Prime Numbers are:
 2  3  5  7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97  101  103  107  109  113  127  131  137  139  149  151  157  163  167  173  179  181  191  193  197  199  211  223  227  229  233  239  241  251  257  263  269  271  277  281  283  293  307  311  313  317  331  337  347  349  353  359  367  373  379  383  389  397  401  409  419  421  431  433  439  443  449  457  461  463  467  479  487  491  499  503  509  521  523  541


Sunday, June 21, 2020

C program to find all factors of a given number

C program to find all factors of a given number.

Hints: 
  • Factors of 6 are 1, 2, 3, 6
  • Factors of 15 are 1, 3, 5, 15
  • Factors of 17 are 1, 17

#include <stdio.h>


int main()
{
    int i, number;

    // Read a number from user
    printf("Enter number to find its factor:");
    scanf("%d", &number);

    printf("Factors of %d are as under:\n", number);

    /* Loop to find factors */
    for(i=1; i<=number; i++)
    {
        if(number % i == 0)
        {
            printf("%d\n",i);
        }
    }
    return 0;
}

Output of program

Enter number to find its factor:15
Factors of 15 are as under:
1
3
5
15

Back to Home >


Sunday, April 26, 2020

MCQs C String Set - 2 Answer

MCQs C String Set - 2 Answer



The _________ string function can be used to read multiple words from user.
A. getc()
B. gets()
C. getchar()
D. getche()
ANSWER: B. gets()

Which of the following string function can be used to copy n characters from one string to another string?
A. strncpy
B. strcat
C. strncat
D. strlen
ANSWER: A. strncpy

What will be the output of following C program?
#include<stdio.h>
#include<string.h>
int main(){
  char state[20]="Gujarat India";
  printf("%d", strlen(state));
  return 0;
}
A. 13
B. 14
C. 20
D. none of these
ANSWER: A. 13

What will be the output of following C program?
#include<stdio.h>
#include<string.h>
int main() {
    char fname[20] = "K";
    char sname[20] = "Patel";
    printf("%s\n", strcpy(strcat(fname, sname), sname));
    return 0;
}
A. K Patel
B. Patel
C. K
D. none of these
ANSWER: B. Patel

What will be the output of following C program?
#include<stdio.h>
#include<string.h>
int main(){
    char str1[30] = "C Programming";
    char str2[30] = "Top C Practicals";
    printf("%s", strncpy(str1, str2, 16));
    return 0;
}
A. Top C Practicals
B. C Programming Top C Practicals
C. C Programming Top
D. none of these
ANSWER: A. Top C Practicals

Back to Questions Bank >



MCQs C String - Set 2

MCQs C String - Set 2



Question 1. The _________ string library function can be used to read multiple words from user. 
A. getc()
B. gets()
C. getchar()
D. getche()

Question 2. Which of the following string function can be used to copy n characters from one string to another string? 
A. strncpy() 
B. strcat() 
C. strncat() 
D. strlen

Question 3. What will be the output of following C program?

#include<stdio.h>
#include<string.h>
int main(){
  char state[20]="Gujarat India";
  printf("%d", strlen(state));
  return 0;

}
A. 13
B. 14 
C. 20
D. none of these

Question 4. What will be the output of following C program?

#include<stdio.h>
#include<string.h>
int main()
{
    char fname[20] = "K";
    char sname[20] = "Patel";
    printf("%s\n", strcpy(strcat(fname, sname), sname));
    return 0;
} 
A. K Patel 
B. Patel 
C. K 
D. none of these

Question 5. What will be the output of following C program?


#include<stdio.h>
#include<string.h>
int main(){
    char str1[30] = "C Programming";
    char str2[30] = "Top C Practicals";
    printf("%s", strncpy(str1, str2, 16));
    return 0;
}
A. Top C Practicals 
B. C Programming Top C Practicals 
C. C Programming Top 
D. none of these

   

Score out of 5 =   Percentage =


Check Your Answer Here

Back to Questions Bank >



Thursday, March 26, 2020

Program to download a web page

Program to Download a Web page


Java program to download a given web page using URL Class. 


import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

public class DownloadWebPageDemo {

    public static void DownloadWebPageDemo(String webpage) {
        try {

            // Creating URL object 
            URL url = new URL(webpage);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

            // Filename in which downloaded content to be saved  
            BufferedWriter writer = new BufferedWriter(new FileWriter("HomePage.html"));

            // read all line from input stream upto end 
            String line;
            while ((line = br.readLine()) != null) {
                writer.write(line);
            }

            br.close();
            writer.close();
            System.out.println("Webpage Downloaded Successfully.");
            System.out.println("Downloaded file saved as HomePage.html");
        }  
        catch (MalformedURLException MalurlEx) {
            System.out.println("URL Exception raised");
        } catch (IOException ie) {
            System.out.println("IOException raised");
        }
    }

    public static void main(String args[])
            throws IOException {
        String url = "https://cprogrampracticals.blogspot.com";
        DownloadWebPageDemo(url);
    }
}

Output of program

Web page Downloaded Successfully.
Downloaded file saved as HomePage.html

Note: HomePage.html file will be downloaded into the folder where a Source file is stored.

* * * * *






Friday, March 6, 2020

Sum of series 1 + 1/2 + 1/3 + 1/4 + ....N

C Program to print sum of series 1 + 1/2 + 1/3 + 1/4 + ....N for the given value of N.

For example, if N=5, output will be sum of 1 + 1/2 + 1/3 + 1/4 + 1/5, i.e. 2.28 

#include<stdio.h>
int main()
{
  int i, N;
  float sum=0;
  printf("Enter N:");
  scanf("%d", &N);
  if(N<1){
  printf("Enter value of N > 0.");
  exit(1);
  }
  for(i=1; i<=N; i++)
  {
   if(i==1)
   {
  sum=1;
  printf("Series = 1");
   }
   else
   {
     sum = sum + (float)1/i;
     printf(" + 1/%d", i);
   }
  }
  printf("\nSum of the series = %0.2f", sum);
  return 0; 
}

Output of program

Enter N:5
Series = 1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of the series = 2.28



Read more C Programming Series Programs here...

< Back to Home Page >