Pages

Thursday, December 31, 2020

Sum of all numbers between 100 and 200 using do while loop

C Program to find sum of all numbers between 100 and 200 using do while loop.

Expected output is sum of 100 + 101 + 102 + .. + 199 + 200.


Program Code

#include<stdio.h>
int main()
{
  int number=100, sum=0;
  
  do{
    sum = sum + number;
    number = number + 1;
  }while(number<=200);

  printf("Sum of all numbers between 100 to 200:"); 
  printf(" %d", sum);

  return 0;
}

Output of the program

Sum of all numbers between 100 to 200: 15150 

* * * * *


< Back to Looping Page >

< Back to Home >


Wednesday, December 30, 2020

C program to print Z to A using for loop

C Program to print Z to A using for loop


Program Code 1:

#include<stdio.h>
int main()
{
  int i;
  for(i='Z'; i>='A'; i--)
    printf("%c", i);
  
  return 0;
}

Output of the program:

ZYXWVUTSRQPONMLKJIHGFEDCBA

* * * * *

Same program can also be generated using following code.

Program Code 2:

#include<stdio.h>
int main()
{
    int i;
    //90 is ASCII value of Z
    for(i=90; i>=65; i--)
        printf("%c", i);
    
    return 0;
}

Output of the program:

ZYXWVUTSRQPONMLKJIHGFEDCBA

* * * * *



Friday, September 11, 2020

Flowchart to convert uppercase letters into lowercase

 RAPTOR Flowchart to convert Uppercase letters into Lowercase letters

Fig.: Flowchart to convert Uppercase letters into Lowercase letters

Note: 

  • ASCII value of 'A' is 65, and 'a' is 97.
  • If we add 32 into 65, we get 97 which is ASCII value of 'a'. 
  • Hence we add 32 into 'chr' variable in the flowchart. This process we repeat up to 'Z' in the flowchart using loop.

You may like to read C Program to convert uppercase letters into  Lowercase letters here. 


Check more operation flowchart on following link:

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 >

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.