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 >

Wednesday, February 26, 2020

C Programming if statement predict output MCQs Set-1 Answer

C PROGRAMMING IF STATEMENT PREDICT OUTPUT SET-1 ANSWER


Program - 1

#include<stdio.h>
int main()
{
  int score =  100;
  if ( score != 100 )
    printf("You win ");
  else
    printf("You lose ");

  printf("best prize.");

  return 0;
}
A. You win
B. You lose
C. You lose best prize.
D. You win best prize.

ANSWER: C
Explanation: Condition (score != 100) is evaluated to false, hence code inside else part will be executed. Also, last printf is not a part of if..else statement as we are not using any { }, hence final output becomes "You lose best prize."

Program - 2

#include<stdio.h>
int main()
{
    int num1=5, num2=4, num3=3;

    if(num1 > num2 && num1 > num3)
    {
         printf("Number1.");
    }
    if(num2 > num1 || num2 > num3)
    {
         printf("Number2.");
    }
    if(num3 > num1 && num3 > num2)
    {
         printf("Number3.");
    }
    return 0;
}
A. Number1.
B. Number2.
C. Number3.
D. Number1.Number2
E. Number1.Number2.Number3

ANSWER: D

Explanation:
  • Observe conditions of first if -> num1>num2 is TRUE, num1>num3 is also TRUE; TRUE && TRUE results in TRUE, hence first if blok will be executed.
  • Observe second if -> num2 > num1 is FALSE, num2 > num3 is TRUE; FALSE || TRUE results in TRUE, hence second if block will be executed.
  • Observe third if -> num3>num1 is FALSE, num3>num2 is FALSE; FALSE && FALSE results in FALSE, hence third if block will not be executed.
  • Hence final result will be "Number1.Number2"

Program - 3

#include<stdio.h>

int main()
{
    int time=0;
 
    if(time < 12)
    {
        printf("Good Morning...");
    }
    printf("Good Day...");
    return 0;
}
A. Good Morning...
B. Good Day...
C. Good Morning...Good Day...
D. Syntax error

ANSWER: C
Explanation: Condition inside if statement, i.e. time < 12 is true, hence both the printf statements will be executed resulting in output as "Good Morning...Good Day..."

Program - 4

#include<stdio.h>
int main()
{
    int num1=15, num2=10;

    if(num1 > num2)
    {
        printf("Num1 is big..");
    }
    if(num2 > num1)
    {
        printf("Num2 is big..");
    }
    if(num1 = num2)
    {
        printf("Num1 and Num2 are equal.");
    }
    return 0;
}
A. Num1 is big.
B. Num2 is big.
C. Num1 and Num2 are equal.
D. Num1 is big..Num1 and Num2 are equal.
E. Syntax error

ANSWER: D
Explanation: 
  • Condition in first if TRUE, which prints "Num1 is big.."
  • Condition in second if is FALSE, hence second if block will be skipped.
  • Statement inside third if (assignment statement) will be executed, which returns non-zero number. In C, any non-zero number is considered as TRUE, hence code inside if will be executed; which results in final output as "Num1 is big..Num1 and Num2 are equal."
Program - 5

#include<stdio.h>
void main()
{
  int number=0;

  if(number > 0)
     printf("Number is positive.");

  if(number => 0)
     printf("Number is not positive.");
}
A. Number is positive.
B. Number is not positive.
C. Number is positive.Number is not positive.
D. Syntax error

ANSWER: D

Explanation: In second if statement, operator used is "=>" which results in syntax error. The correct operator is "<=".


Read more interesting C Programming MCQs here.

< Back to Home >

Tuesday, February 25, 2020

C Programming if statement predict output MCQs Set-1

C Programming if statement predict output Set-1

Predict output of following C programs based on if statements

Program - 1

#include<stdio.h>
int main()
{
  int score =  100; 
  if ( score != 100 )
    printf("You win ");
  else
    printf("You lose ");

  printf("best prize.");
  return 0;
}
A. You win
B. You lose
C. You lose best prize.
D. You win best prize.

Program - 2

#include<stdio.h>
int main()
{
    int num1=5, num2=4, num3=3;

    if(num1 > num2 && num1 > num3)
    {
         printf("Number1.");
    }
    if(num2 > num1 || num2 > num3)
    {
         printf("Number2.");
    }
    if(num3 > num1 && num3 > num2)
    {
         printf("Number3.");
    }
    return 0;
}
A. Number1.
B. Number2.
C. Number3.
D. Number1.Number2
E. Number1.Number2.Number3

Program - 3

#include<stdio.h>
int main()
{
    int time=0;
   
    if(time < 12)
    {
        printf("Good Morning...");
    }
    printf("Good Day...");
    return 0;
}
A. Good Morning... 
B. Good Day...
C. Good Morning...Good Day...
D. Syntax error

Program - 4

#include<stdio.h>
int main()
{
    int num1=15, num2=10;

    if(num1 > num2)
    {
        printf("Num1 is big..");
    }
    if(num2 > num1)
    {
        printf("Num2 is big..");
    }
    if(num1 = num2)
    {
        printf("Num1 and Num2 are equal.");
    }
    return 0;
}
A. Num1 is big.
B. Num2 is big.
C. Num1 and Num2 are equal.
D. Num1 is big..Num1 and Num2 are equal.
E. Syntax error

Program - 5

#include<stdio.h> 
void main()
{
  int number=0;

  if(number > 0)
     printf("Number is positive.");

  if(number => 0)
     printf("Number is not positive.");
}
A. Number is positive.
B. Number is not positive.
C. Number is positive.Number is not positive.
D. Syntax error

Check your Answer here...


Read more interesting C Programming MCQs here.



Sunday, February 23, 2020

C Programming for loop predict output MCQs Set - 1 Answers

C Programming for loop predict output (MCQs) Set - 1 Answers

1. Predict output of following C program:
#include <stdio.h>
void main()
{
    int c = 0;
    for (c)
        printf("Top C Practicals");
}
A. Compile time error
B. Top C Practicals
C. no output
D. none of these

ANSWER: A
Explanation: C programming for loop needs three expressions/statements inside for loop.

2. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i = 0;
    for ( ; i<5 ; i++)
        printf("%d ", i);
    return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. Compile time error
D. none of these

ANSWER: A
Explanation: Initial value of i will be considered as 0. Hence output will be 0 1 2 3 4

3. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i=0, j=5;
    printf("Hello ");
    for (i; i>j; i++)
        printf("%d ", i);
    printf("Students ");
    return 0;
}
A. Hello 0 1 2 3 4 Students
B. Hello Students
C. Hello 0 1 2 3 4
D. 0 1 2 3 4 5

ANSWER: B
Explanation: Initial value of i is 0 and j is 5; Hence i>j will be evaluated to false. Hence for loop will be executed zero times; thus answer is Hello Students.

4. Predict output of following C program:
#include <stdio.h>
int main()
{
   int i;
   for (i=0; i<10; i++)
       printf("%d ", ++i);
   return 0;
}
A. 1 3 5 7 9
B. 0 1 2 3 4 5 6 7 8 9
C. 2 4 6 8
D. 2 4 6 8 10

ANSWER: A
Explanation: Due to ++i (pre-increment) inside loop, the output will be 1 3 5 7 9

5. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i;
    for (i=65; i<70; i++)
        printf("%c ", i);
    return 0;
}
A. 65 66 67 68 69
B. A B C D E
C. Compile time error
D. Run time error

ANSWER: B
Explanation: %c in the printf will print character associated with the ASCII value of 65 to 69. 

* * * * *



Read more interesting C Programming MCQs here.


c programming for loop predict output MCQs set-1

C Programming for loop predict output (MCQs) Set - 1

1. Predict output of following C program:
#include <stdio.h>
void main()
{
    int c = 0;
    for (c)
        printf("Top C Practicals");
}
A. Compile time error
B. Top C Practicals
C. no output
D. none of these

2. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i = 0;
    for ( ; i<5 ; i++)
        printf("%d ", i);
    return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. Compile time error
D. none of these

3. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i=0, j=5;
    printf("Hello ");
    for (i; i>j; i++)
        printf("%d ", i);
    printf("Students ");
    return 0;
}
A. Hello 0 1 2 3 4 Students
B. Hello Students
C. Hello 0 1 2 3 4
D. 0 1 2 3 4 5

4. Predict output of following C program:
#include <stdio.h>
int main()
{
   int i;
   for (i=0; i<10; i++)
       printf("%d ", ++i);
   return 0;
}
A. 1 3 5 7 9
B. 0 1 2 3 4 5 6 7 8 9
C. 2 4 6 8
D. 2 4 6 8 10

5. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i;
    for (i=65; i<70; i++)
        printf("%c ", i);
    return 0;
}
A. 65 66 67 68 69
B. A B C D E
C. Compile time error
D. Run time error


Check your answer here.
* * * * *



Read more interesting C Programming MCQs here.


Tuesday, January 14, 2020

Printing message at same position on screen

Printing message at same position on Screen

This C program will print message on same position on screen without Graphics library.

#include<stdio.h>
#include<windows.h>

int main(){
  int i;
  for(i=0; i<=100; i++)
  {
    system("cls");
    printf("Software Installation %d %% completed", i);
    Sleep(10);
  }
  getch();
  return 0;
}

Output of program:

Software Installation 100 % completed

Note: This program prints "Software Installation %age [1 to 100] completed" message at same position on screen.


* * * * *


< Back to Home Page>




Sunday, January 12, 2020

Write a student data into a file using the C Structure

File Management using C Structure

This C Program will write a student data into a file using the Structure and File Management concept of C programming.

Source Code

#include <stdio.h>
//Structure to store student data.
struct student
{
   char fname[30];
   char lname[30];
   int  rollno;
   float percentage;
};

void main ()
{
   FILE *fp;
   //declare structure(student) variable
   struct student input;
   // open student file for writing
   fp = fopen ("student.dat","w");
   if (fp == NULL){
      printf("\nFile opening error..\n\n");
      exit (1);
     }

   printf("Enter \"exit\" as First Name to stop reading user input.");

   while (1)
     {
      printf("\nFirst Name: ");
      scanf ("%s", input.fname);
     
      if (strcmp(input.fname, "exit") == 0)
         exit(1);
     
      printf("Last Name : ");
      scanf ("%s", input.lname);
      printf("Roll Number  : ");
      scanf ("%d", &input.rollno);
      printf("Percentage : ");
      scanf ("%f", &input.percentage);

      // write student data to student.dat  file
      fwrite (&input, sizeof(struct student), 1, fp);
     }
}

Output of program 

Enter "exit" as First Name to stop reading user input.
First Name: K
Last Name : Patel
Roll Number  : 101
Percentage : 90.50

First Name: ABC
Last Name : Shah
Roll Number  : 102
Percentage : 95.50

First Name: exit

* * * * *


< Read more about C File Management >

< Read more about C Structure >

< Back to Home Page>