Results for C Program

C Program to find the sum of digits of a number | Using while loop

C Program to find the sum of digits of a number
In this example, we will learn how to find the sum of all digits of a number in C. Our program will take one number as input from the user, calculate the sum of its digits and print it out. We will use while loop to find the digits of the given number and use one variable to store the sum of all the digits.
For example if user enters the the number 4538, then output i.e. sum of digits will be equal to 20. (4+5+3+8=20)

C Program to find the sum of digits of a number:
#include<stdio.h>  
 int main()    
{    
int n,sum=0,m;    
printf("Please Enter any Number: ");    
scanf("%d",&n);    
while(n>0)    
{    
m=n%10;    
sum=sum+m;    
n=n/10;    
}    
printf("Sum of the digits of Given Number = %d"sum);    
return 0;  
}  

Output:
Please Enter any Number: 4538 Sum of the digits of Given Number = 20

Run Code: If you want run this code copy this code, paste here and run.
Technology Topper Saturday, June 26, 2021
Read more ...

C Program to Swap Two Numbers | without using temporary variable & with using temporary variable

C Program to Swap Two Numbers

Swapping of two numbers means, first number becomes second and second number becomes first. For example, if user enters any two number say 5 and 10. And let's suppose the two variables say a and b holds these two numbers. That is, a=5 and b=10. Then after swapping it will be like a=10 and b=5.
In this example, you will learn to swap two variables by using a temporary variable and, without using a temporary variable.

C Program to Swap Two Numbers without using temporary variable:
#include<stdio.h>  
int main()    
{    
int a=5b=10;      
printf("Before swap a=%d b=%d",a,b);      
a=a+b;    
b=a-b;   
a=a-b;    
printf("\nAfter swap a=%d b=%d",a,b);    
return 0;  
}  

Output:
Before swap a= 5 b= 10
After swap a= 10 b= 5

C Program to Swap Two Numbers with using temporary variable:
#include<stdio.h>  
int main()    
{    
int a=5b=10;    
int temp;  
printf("Before swap a=%d b=%d",a,b);      
temp=a;
a=b;
b=temp
printf("\nAfter swap a=%d b=%d",a,b);    
return 0;  
}  

Output:
Before swap a= 5 b= 10
After swap a= 10 b= 5

Run Code: If you want run this code copy this code, paste here and run.

Technology Topper Thursday, June 24, 2021
Read more ...

C Program to Print Multiplication Table of a given Number

Technology Topper

In this example, you will learn to generate the multiplication table of a number entered by the user. In this program we will use for loop to generate multiplication table of number which is provided by user.

C program:
#include <stdio.h>
int main() {
    int ni;
    printf("Enter the number: ");
    scanf("%d", &n);
    for (i = 1i <= 10; ++i) {
        printf("%d x %d = %d \n"nin * i);
    }
    return 0;
}


Output:
Enter the number: 9 Multiplication Table of 9 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 9 X 10 = 90


Run Code: If you want run this code copy this code, paste here and run.
Technology Topper Monday, March 01, 2021
Read more ...

C Program to Check Whether a Character is a Vowel or Consonant

C Program to Check Vowel or Consonant

In the English language, the letters A, E, I, O, U and a, e, i, o, u are defined as a vowel. Except these all other letters are consonants. We can write a c program to check vowels or consonants using an if-else statement or by using switch-case statements.

C Program to Check Whether a Character
#include <stdio.h>
int main() 
{
    char c;
    int lowercase_voweluppercase_vowel;
    printf("Enter an Alphabet: ");
    scanf("%c", &c);
    lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    if (lowercase_vowel || uppercase_vowel)
        printf("%c is a Vowel."c);
    else
        printf("%c is a Consonant."c);
    return 0;
}

Output:
Enter an Alphabet: E E is a Vowel.
Enter an Alphabet: S
S is a Consonant.

Run Code: If you want run this code copy this code, paste here and run.


Technology Topper Sunday, February 28, 2021
Read more ...

C Program to Find Factorial of a Number | Using for loop, while loop & recursion

C Program
The Factorial is the product of all numbers, which are less than or equal to that number, and greater than 0.

n! = n * (n-1) * (n -2) * …….* 1

For example, Factorial of 5 is represented as 5! = 5 *4 * 3 * 2 * 1 = 120


C Program to Find Factorial of a Number
Using for loop:

#include <stdio.h>

int main()

{

  int i, Number

  long Factorial = 1;

  printf("Please Enter a number to Find Factorial\n");

  scanf("%d", &Number);

  for (i = 1; i <= Number; i++)

   {

     Factorial = Factorial * i;

   }

  printf("Factorial of %d = %d ", Number, Factorial);

  return 0;

}


Using while loop:

#include <stdio.h>

int main()

{

  int Number, i = 1

  long Factorial = 1;

  printf("Please Enter a number to Find Factorial\n");

  scanf("%d", &Number);

  while (i <= Number)

   {

     Factorial = Factorial * i;

     i++;

   }

  printf("Factorial of %d = %d ", Number, Factorial);

  return 0;

}


Using recursion:

#include <stdio.h>

long Calculate_Factorial(int Number)

  if (Number == 0 || Number == 1)  

    return 1;  

  else

    return Number * Calculate_Factorial (Number -1);

int main()

{

 int Number

 long Factorial = 1

 printf("Please Enter a number to Find Factorial\n");

 scanf("%d", &Number);

 Factorial = Calculate_Factorial(Number);

 printf("Factorial of %d = %d ", Number, Factorial);

 return 0;

}


Output:

Please Enter a number to Find Factorial 5

Factorial of 5 = 120

Please Enter a number to Find Factorial 10

Factorial of 10 = 3628800


Run Code: If you want run this code copy this code, paste here and run.

Technology Topper Saturday, January 23, 2021
Read more ...

C Program and Python Interview Questions | Commonly Asked Technical Interview Questions 2021

Interview Questions
In this post, I will discuss the most important C Program and Python Interview Questions in order to make you familiar with the type of questions that can be asked during a job interview related to the C Program and Python.
The below-mentioned C Program and Python interview questions will assist you in clearing your basic concepts. If you find this post helpful then please make sure that you share this post with your friends.

C Program Interview Questions And Answers:
Q. Features of C Programming Language?
Ans: Procedural Language
Fast and Efficient
Modularity
Statically Type
General Purpose Language
Rich set of built in Operators
Libraries with rich Functions
Middle Level Language
Portability
Easy to Extend

Q. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the Machine Code in one shot. On the other hand, Interpreter is used in Java Programming Language and other high-end programming languages. It is designed to compile code in line by line fashion.

Q. What are different storage class specifiers in C?
Ans: auto, register, static, extern

Q. What is NULL pointer?
Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. 

Q. What is Dangling pointer?
Ans: Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

Q. What is Preprocessor?
Ans: A Preprocessor Directive is considered as a built-in predefined function or macro that acts as a directive to the compiler and it gets executed before the actual C Program is executed.

Q. What is an array?
Ans: The array is a simple data structure that stores multiple elements of the same datatype. 

Q. What is typecasting?
Ans: Typecasting is a process of converting one data type into another is known as typecasting.

Q. What is a nested loop?
Ans: A loop that runs within another loop is referred to as a nested loop. The first loop is called the Outer Loop and the inside loop is called the Inner Loop.

Top 5 Best Smartphones Under Rs.30000 ⚡⚡⚡
Must Watch
Python Interview Questions And Answers:
Q. What is Python?
Ans: Python is a high-level, interpreted, interactive, dynamically typed and object-oriented scripting language. Python is designed to be highly readable.

Q. Features in Python?
Ans: Easy to code
Free and Open Source
Object-Oriented Language
GUI Programming Support
High-Level Language
Python is Integrated language
Interpreted Language
Dynamically Typed Language

Q. What are functions in Python?
Ans: A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used.

Q. In Python what is slicing?
Ans: A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.

Q. What is a string in Python?
Ans: The string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string.
Example: str2 = 'hello world' 

Q. What is a Lists in Python?
Ans: Python Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].
example: mylist1 = ["apple", "banana", "cherry"]

Q. What is tuple in Python?
A tuple is a built-in data collection type. It allows us to store values in a sequence. It is immutable, so no change is reflected in the original data. It uses () brackets. 
example: tup1 = ("apple", "banana", "cherry")

Q. What is a dictionary in Python?
Ans: The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.
example: dict={'Country':'India', 'Game':'Cricket', 'Color':'Blue'}

Q. What is the difference between list and tuple?
Ans: The difference between list and tuple is that list is mutable while tuple is not.

Q. What is lambda in Python?
Ans: It is a single expression anonymous function often used as inline function.



Technology Topper Tuesday, January 05, 2021
Read more ...

C Program for Fibonacci Numbers | Using for loop, while loop & recursion

C Program for Fibonacci Numbers

The Fibonacci series is a sequence where the next term is obtained by the sum of previous two terms.
Fibonacci series- 0, 1, 1, 2, 3, 5, 8, 13, 21,....etc. The first two numbers of Fibonacci series are 0 and 1.

C Program to display Fibonacci series:
Using for loop
#include<stdio.h>    
int main()    
{    
    int n1=0,n2=1,n3,i,number;    
    printf("Enter the number of elements: ");    
    scanf("%d",&number);    
    printf("\n%d %d",n1,n2);//printing 0 and 1    
    for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    
    {    
       n3=n1+n2;    
       printf(" %d",n3);    
       n1=n2;    
       n2=n3;    
    }  
    return 0;  
 }  

Using while loop
#include <stdio.h>
int main() {
   int t1 = 0t2 = 1nextTerm = 0n,i=2;
   printf("Enter the number of elements: ");
   scanf("%d", &n);
   // displays the first two terms which is always 0 and 1
   printf("Fibonacci Series: %d %d "t1t2);
   nextTerm = t1 + t2;
   while (i < n
   {
      printf("%d "nextTerm);
      t1 = t2;
      t2 = nextTerm;
      nextTerm = t1 + t2;
      i++;
   }
   return 0;
}

Using recursion
#include<stdio.h>    
void printFibonacci(int n)
{    
    static int n1=0,n2=1,n3;    
    if(n>0)
{    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         printf("%d ",n3);    
         printFibonacci(n-1);    
    }    
}    
int main(){    
    int n;    
    printf("Enter the number of elements: ");    
    scanf("%d",&n);    
    printf("Fibonacci Series: ");    
    printf("%d %d ",0,1);    
    printFibonacci(n-2);//n-2 because 2 numbers are already printed    
  return 0;  
}


Output
Enter the number of elements: 5 Fibonacci Series: 0 1 1 2 3 
Enter the number of elements: 9 Fibonacci Series: 0 1 1 2 3 5 8 13 21

Run Code- If you want run this code copy this code, paste here and run.
Technology Topper Saturday, December 19, 2020
Read more ...