GOEDUHUB Tutorials    ||    AI    ||    ML  ||    NLP    ||    OpenCV    ||    Python    ||    OOPS    ||    DBMS    ||    DSA    ||    JAVA    ||    Linux    ||    C Programming
7976731765 Go To Your University       ||       Placement Preparation       ||        Technical-MCQ and Interview Questions
Online Live Classes Youtube Live Link
0 like 0 dislike
23 views
in Tutorial & Interview questions by Goeduhub's Expert (8.1k points)

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.1k points)
 
Best answer

C Program to find whether an integer is positive or negative

#include
int main()
{
//Fill the code
int num;
scanf(“%d”,&num);
if(num > 0)
printf(“Positive”);
else
printf(“Negative”);
return 0;

Program to Swap Two Numbers Without Using Third Variable

#include<stdio.h>
int main()
{
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);  //consider two numbers as 10 and 20
    a = a + b;  //a = 10 + 20 = 30
    b = a - b;  //b = 30 - 20 = 10
    a = a - b;  //a = 30 - 10 = 20
    printf("Numbers after swapping: %d %d", a, b);

C Program to find HCF and LCM of two numbers

Find the highest common factor and the least common multiple of two integers. HCF is also known as the greatest common divisor (GCD) or the greatest common factor (GCF).

#include <stdio.h>

 int main() {

  int a, b, x, y, t, gcd, lcm;

   printf("Enter two integers\n");

  scanf("%d%d", &x, &y);

   a = x;

  b = y;

   while (b != 0) {

    t = b;

    b = a % b;

    a = t;

  }

   gcd = a;

  lcm = (x*y)/gcd;

   printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);

  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

   return 0;

}

 C Program to count number of digits in an integer 

#include <stdio.h>
int main()
{
int n;
int count = 0;
printf(“\nEnter the number: “);
scanf(“%d”, &n);
while(n != 0)
{
n = n/10;
++count;
}
printf(“\nNumber of digits: %d\n”, count);

C Program to find the sum of digits of a number

# include<stdio.h>

int sum_of_digits(int n)
{
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}

int main()
{
int n;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\nSum of digits of %d is %d\n”, n,sum_of_digits(n));
return 0;

Note- 

  • Take Input the number from user, Declare a variable called sum and initialize it to 0.
  • while (number > 0)
  • Get the rightmost digit of the number using ‘%’ operator by dividing it with 10 and add it to sum
  • Divide the number by 10.
  • Return sum

C Program to find the sum of natural numbers

 #include <stdio.h>

int main()

{
int n;
printf(“\nEnter the number : “);
scanf(“%d”, &n);

int sum = 0;
for(int i = 1; i <= n; i++)
{
sum += i;
}
printf(“\nSum of %d Natural Numbers is %d\n “,n,sum);
return 0;
}

Related questions

0 like 0 dislike
1 answer 92 views
0 like 0 dislike
1 answer 46 views
0 like 0 dislike
1 answer 33 views
0 like 0 dislike
0 answers 22 views

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
...