0% found this document useful (0 votes)
58 views

Q1.Accept 10 Number From User and Do Sum of It. (Do Not Use Array)

The document contains 6 questions and their solutions related to basic C programming concepts like accepting input from the user, performing arithmetic operations, conditional statements, loops etc. Each question is about a different concept - summing user input numbers, calculating factorials, counting positive/negative/zero numbers, summing digits of a number, counting digits of a number, counting occurrences of a digit in a number. For each question, 2 solutions/approaches are provided using while loops and for loops.

Uploaded by

kirito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Q1.Accept 10 Number From User and Do Sum of It. (Do Not Use Array)

The document contains 6 questions and their solutions related to basic C programming concepts like accepting input from the user, performing arithmetic operations, conditional statements, loops etc. Each question is about a different concept - summing user input numbers, calculating factorials, counting positive/negative/zero numbers, summing digits of a number, counting digits of a number, counting occurrences of a digit in a number. For each question, 2 solutions/approaches are provided using while loops and for loops.

Uploaded by

kirito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q1.Accept 10 number from user and do sum of it.

(do not use array)

A1. #include<stdio.h>
#include<conio.h>
int main()
{
int sum = 0;
int n = 10;
printf("Enter 10 numbers: ");
while(n--)
{
int x;
scanf("%d", &x);
sum = sum + x;
}
printf("Sum of 10 numbers: %d", sum);
getch();
return 0;
}

A2. #include<stdio.h>
#include<conio.h>
int main()
{
int i, n, sum=0;
printf("Enter 10 numbers: ");
for (i=1;i<=10;i++)
{
scanf("%d",&n);
sum = sum+n;
}
printf("Sum of 10 numbers: %d", sum);
getch();
return 0;
}

Q2.Accept a number from user and find a factorial of a number

A1. #include<stdio.h>
#include<conio.h>
int main()
{
int n,i=1,f=1;
printf("Enter a number: ");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i++;
}
printf("The factorial of %d is %d",n,f);
getch();
return 0;
}

A2. #include<stdio.h>
#include<conio.h>
int main()
{
int n,i,f=1;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("The factorial of %d is %d",n,f);
getch();
return 0;
}
Q3.Accept 10 number from user count how many are positive ,negative , zero

A1. #include<stdio.h>
#include<conio.h>
int main()
{
int x=10, n, positive = 0, negative = 0, zero = 0;
printf("Enter 10 numbers: ");
while(x--)
{
scanf("%d",&n);
if(n>0)
{
positive++;
}
else if(n<0)
{
negative++;
}
else
{
zero++;
}
}
printf("Positive Numbers: %d\n", positive);
printf("Negative Numbers: %d\n", negative);
printf("Number of zero: %d\n", zero);
getch();
return 0;
}

A2. #include<stdio.h>
#include<conio.h>
int main()
{
int x=10, n, i, positive=0, negative=0, zero=0;
printf("Enter 10 numbers: ");
for(i=0; i<x; i++)
{
scanf("%d", &n);
if(n>0) positive++;
else if(n==0) zero++;
else negative++;
}
printf("Positive Numbers: %d\n", positive);
printf("Negative Numbers: %d\n", negative);
printf("Number of zero: %d\n", zero);
getch();
return 0;
}

Q4.Accept a number from user and do sum of digit

A1. #include<stdio.h>
#include<conio.h>
int main()
{
int n,r, sum=0;
printf("Enter the number: ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum of digit: %d\n", sum);
getch();
return 0;
}

A2. #include<stdio.h>
#include<conio.h>
int main()
{
int n, sum = 0, r;
printf("Enter a number: ");
scanf("%d", &n);
for (; n!=0; n=n/10)
{
r = n % 10;
sum = sum + r;
}
printf("The sum of digits: %d", sum);
getch();
return 0;
}

Q5.Accept a number and count number of digit

A1. #include<stdio.h>
#include<conio.h>
int main()
{
int n, count=0;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
n = n/10;
count = count + 1;
}
printf("Number of Digits = %d", count);
getch();
return 0;
}

A2. #include<stdio.h>
#include<conio.h>
int main()
{
int n, count;
printf("Enter a number: ");
scanf("%d",&n);
for(count=0; n!=0; n=n/10)
{
count++;
}
printf("Number of Digits: %d",count);
getch();
return 0;
}

Q6.Accept a number, accept a digit and count occurrence of digit.

A1. #include<stdio.h>
#include<conio.h>
int main()
{
int num,digit,count,quo,rem;
printf("Enter a number: ");
scanf("%d",&num);
printf("Enter a digit: ");
scanf("%d",&digit);
count=0;
quo=num;
while(quo>0)
{
rem=quo%10;
if(rem==digit)
count++;
quo=quo/10;
}
printf("Total occurrence of digit %d is: %d",digit,count);
getch();
return 0;
}

A2. #include<stdio.h>
#include<conio.h>
int main()
{
int quo, rem, digit, num;
int count=0;
printf("Enter the number: ");
scanf("%d",&num);
printf("Enter a digit: ");
scanf("%d",&digit);
quo=num;
for(;quo>0;)
{
rem=quo%10;
if(rem==digit)
{
count++;
}
quo=quo/10;
}
printf("Total occurrence of digit %d is: %d",digit,count);
getch();
return 0;
}

You might also like