[go: up one dir, main page]

0% found this document useful (0 votes)
38 views33 pages

Full Programs

Uploaded by

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

Full Programs

Uploaded by

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

1

Q1) Write a program to print “hello world”.

#include <stdio.h>
void main ()
{
printf("Hello World");
}
2

Q2) Write a program to take two numbers input from the user
and print sum of the two numbers.

#include <stdio.h>
int main ()
{
int a,b,c;
printf("Enter the first number :");
scanf("%d", &a);
printf("Enter the second number :");
scanf("%d", &b);

c=a+b;
printf("The sum of the two numbers : %d",c);
}
3

Q3) Write a program to take marks input from the users and print
the grade based on the user’s marks.

#include <stdio.h>
int main()
{
int a,b;
printf("\nEnter the total percentage of your exam : ");
scanf("%d",&a);

if (a<=100 && a>= 90)


printf("\nGrade is A+");
else if(a<=89 && a>=80)
printf("\nGrade is A");
else if(a<=79 && a>=70)
printf("\nGrade is B+");
else if(a<=69 && a>=60)
printf("\nGrade is B");
else if(a<=59 && a>=50)
printf("\nGrade is C+");
else if(a<=49 && a>=40)
printf("\nGrade is C");
else if(a<=39 && a>=30)
printf("\nGrade is D");
else
printf("\nFail");

}
4

Q4) Write a program to take number as input from the user and
print whether number is positive or negative.

#include <stdio.h>
int main()
{
int a,i,j,k;
printf("\nEnter the number :");
scanf("%d",&a);

if(a>=0)
printf("\nThe number is positive");
else
printf("\nThe number is negative");
}
5

Q5) Write a program to take input a number from user and print
whether it is even or odd.

#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter a number :");
scanf("%d",&a);

if(a%2==0)
printf("The number is even");
else
printf("The number is odd");
}
6

Q6) Write a program to take two numbers as input from the user
and calculate and print both the quotient and remainder when the
first number is divided by the second number.

#include <stdio.h>
int main()
{
int a,b,quo,rem;
printf("\nEnter the first value : ");
scanf("%d",&a);

printf("\nEnter the second value : ");


scanf("%d",&b);

quo=a/b;
rem=a%b;

printf("\nQuotient is : %d",quo);
printf("\nRemainder is : %d",rem);

}
7

Q7) Write a program to take a number input from the user and
print sum of its digits.

#include <stdio.h>
int main()
{
int n,q,r,s=0;
printf("\nEnter a number : ");
scanf("%d",&n);

if(n<0)
n=n*(-1);
q=n;
while(q!=0)
{
r=q%10;
q=q/10;
s=s+r;
}
printf("\nThe sum of the digits is %d",s);
}
8

Q8) Write a program to take input from the user and check even
or odd. If the number is even then print ‘=’ and if odd then print
‘*’.

#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter a number :");
scanf("%d",&a);

if(a%2==0)
printf("=");
else
printf("*");
}
9

Q9) Write a program to print the sum and product of digits of an


integer.

#include <stdio.h>
int main()
{
int n,q,r,s=0,m=1;
printf("\nEnter a number : ");
scanf("%d",&n);

if(n<0)
n=n*(-1);
q=n;
while(q!=0)
{
r=q%10;
q=q/10;
s=s+r;
m=m*r;
}
printf("\nThe sum of the digits is %d",s);
printf("\nThe product of the digits is %d",m);
}
10

Q10) Write a program to reverse a non-negative integer.

#include <stdio.h>
int main()
{
int a,b,r,q,rev=0;
printf("\nEnter the number to reverse :");
scanf("%d",&a);

if (a<0)
{
a=a*(-1);
}

b=a;
while(b!=0)
{
r=b%10;
q=b/10;
rev=(rev*10)+r;
b=q;
}
printf("\nThe reverse of integer is : %d",rev);

return 0;
}
11

Q11) Write a program to compute the sum of the first n-terms of


the following series.
S=1+1/2+1/3+1/4…………………….

#include <stdio.h>
int main()
{
int a,i,b=1;
float sum=0;
printf("\nEnter the number of terms :");
scanf("%d",&a);

i=a;
while(b<=i)
{
sum=sum+(1.0/b);
b=b+1;
}
printf("The sum of series is : %f\n",sum);
return 0;
}
12

Q12) Write a program to compute the sum of the first n terms of


the following series.
S= 1-2+3-4+5…………………………...

#include <stdio.h>
int main()
{
int a,s_odd=0,s_even=0,i,sum;
printf("\nEnter the number of terms :");
scanf("%d",&a);

i=1;
while(i<=a)
{
if(i%2==0)
{
s_even=s_even+i;
}
else
{
s_odd=s_odd+i;
}
sum=s_odd+(-(s_even));
i=i+1;
}
printf("The sum of the values is : %d\n",sum);
return 0;
}
13

Q13) Write a program prompts the user to enter upper and lower
values. It then prints the even and odd numbers within that range
in separate series.

#include <stdio.h>
int main() {
int a,b,i,r;
printf("\nEnter the upper and lower values : ");
scanf("%d %d",&a,&b);
printf("\nThe even numbers are : ");
i=b;
while(i<=a) {
r=i%2;
if(r==0)
printf("%d ",i);
i=i+1;
}
printf("\n\nThe odd numbers are : ");
i=b;
while(i<=a)
{
r=i%2;
if(r!=0)
printf("%d ", i);
i=i+1;
}
return 0;
}
14

Q14) Write a program prompts the user to enter upper and lower
values, then prints the even and odd numbers within that range
along their respective sums.

#include <stdio.h>
int main() {
int a, b, i, r, sumEven = 0, sumOdd = 0;
printf("\nEnter the upper and lower values: ");
scanf("%d %d", &a, &b);
printf("\nThe even numbers are: ");
i=b;
while(i<=a){
r=i%2;
if(r==0){
printf("%d ", i);
sumEven+=i;}
i=i+1;
}
printf("\n\nThe sum of even numbers is: %d\n", sumEven);
printf("\nThe odd numbers are: ");
i=b;
while(i<=a) {
r=i%2;
if(r!=0){
printf("%d ", i);
sumOdd += i; }
i=i+1;
}
printf("\n\nThe sum of odd numbers is: %d\n", sumOdd);
return 0;
}
15

Q15) Write a program to take input an operator and two numbers


from user and print the result as based on user’s input.

#include<stdio.h>
int main()
{
int n1,n2,r;
char op;
printf("Enter the operator ");
scanf("%c",&op);
printf("Enter the first value ");
scanf("%d",&n1);
printf("Enter the second value ");
scanf("%d",&n2);

switch(op)
{
case '+':
r=n1+n2;
printf("sum is %d\n",r);
break;
case '-':
r=n1-n2;
printf("difference is %d\n",r);
break;
case '*':
r=n1*n2;
printf("product is %d\n",r);
break;
case '/':
r=n1/n2;
16

printf("quotient is %d\n",r);
break;
default:
printf("invalid operator\n");
}
return 0;
}
17

Q16) Write a program that takes two integers inputs and


performs arithmetic operations based on user input. The program
should prompt the user to choose between finding the quotient
(‘q) and remainder (‘r’) of the larger number divided by the
smaller number.

#include <stdio.h>
int main(){
int a,b,m,n,d;
char f;
printf("What do you want q or r : ");
scanf("%c",&f);
printf("Enter the first and second number : ");
scanf("%d %d",&a,&b);
m=a;
n=b;
if(a<b){
n=a;
m=b;}
switch(f)
{
case 'q':
d=m/n;
printf("The quotient is %d", d);
break;
case 'r':
d=m%n;
printf("The remainder is %d", d);
break;
}
}
18

Q17) *****
*****
*****
*****
#include <stdio.h>
int main()
{
int a,i,j;
printf("\nEnter the number of lines do you want to print :");
scanf("%d",&a);

for(i=1;i<=a;i++)
{
for(j=1;j<=a;j++)
{
printf("*");
}
printf("\n");
}
}
19

Q18) 1
12
123
1234
#include <stdio.h>
int main()
{
int a,i,j;
printf("\nEnter the number of lines do you want :");
scanf("%d",&a);

for(i=1;i<=a;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
}
20

Q19) 1
22
333
4444
55555
#include <stdio.h>
int main()
{
int a,i,j;
printf("\nEnter the number of lines do you want :");
scanf("%d",&a);

for(i=1;i<=a;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
}
21

Q20) *
**
***
****
#include <stdio.h>
int main()
{
int n,i,j,k;
printf("\nEnter the no. of lines do you want to print : ");
scanf("%d",&n);

for (i=1;i<=n;i++)
{
for(j=(n-1);j>=i;j--)
printf(" ");
for(k=n;k>n-i;k--)
printf("*");
printf("\n");-
}
}
22

Q21) *
**
***
****
#include <stdio.h>
int main()
{
int a,i,j,k;
printf("\nEnter the number of lines do you want to print :");
scanf("%d",&a);

for(i=1;i<=a;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
23

Q22) *******
* *
* *
* *
* *
* *
*******
#include <stdio.h>
int main()
{
int a,o,i,j,k,m;
printf("\nEnter the number of lines do you want to print : ");
scanf("%d",&a);

for(i=1;i<=a;i++)
{
for(j=1;j<=a;j++)
if(i==1 || i==a || j==1 || j==a)
printf("*");
else
printf(" ");

printf("\n");
}
}
24

Q23) *
***
*****
*******
*********
#include <stdio.h>
int main()
{
int a,i,j,k;
printf("\nEnter the numbers do you want to print : ");
scanf("%d",&a);

for(i=1;i<=a;i++)
{
for(j=a-1;j>=i;j--)
printf(" ");
for(k=1;k<=2*i-1;k=k+1)
printf("*");
printf("\n");
}
}
25

Q24) Write a program to take input a number from the user and
check whether it is a prime number or not.

#include <stdio.h>
int main()
{
int n,i,r,f=1;
printf("Enter a number : ");
scanf("%d", &n);
for(i=2;i<=(n-1);i++)
{
r=n%i;
if(r==0)
{
f=0;
break;
}
}
if(f==0)
printf("The number is not a prime number");
else
printf("The number is a prime number");
}
26

Q1) Write a program to take two numbers input from the user
and print the sum of the two numbers.

n1=readline(prompt="Enter a number : ")


n2=readline(prompt="Enter another number : ")
n3=as.integer(n1)
n4=as.integer(n2)

sum=n3+n4
print(paste("The sum of", n3, "and", n4, "is:", sum))
27

Q2) Write a program to take a number input from the user and
check whether a number is positive, negative or zero.

n = as.integer(readline("Enter a number: "))

if (n > 0) {
cat("The entered number is positive.\n")
} else if (n < 0) {
cat("The entered number is negative.\n")
} else if (n == 0) {
cat("The entered number is zero.\n")
} else {
cat("Invalid input. Please enter a valid number.\n")
}
28

Q3) Write a program that asks the user for a number n and prints
the sum of the number 1 to n.

n = as.integer(readline(prompt = "Enter a number: "))


m=n
s=0

while (n >= 1) {
s=s+n
n=n-1
}

cat("The sum of the numbers from 1 to", m ," is:", s, "\n")


29

Q4) Write a program to take two numbers input from the user
and print the maximum number from them

n1 = as.integer(readline(prompt = "Enter the first number: "))


n2 = as.integer(readline(prompt = "Enter the second number: "))

if (!is.na(num1) && !is.na(num2)) {


max_num = max(n1, n2)
cat("The maximum number of", n1, "and", n2, "is:", max_num,
"\n")
} else {
cat("Please enter valid numeric values.\n")
}
30

Q5) Write a program to take a number input from the user and
check whether a given number is prime number or not.

n = as.integer(readline(prompt = "Enter a number: "))


f=1

if (n > 1) {
for (i in 2:(n-1)) {
r = n %% i
if (r == 0) {
f=0
break
}
}

if (f == 0) {
cat(n," is not a prime number.\n")
} else {
cat(n," is a prime number.\n")
}
} else {
cat("The number is not a prime number.\n")
}
31

Q6) Write a program to take a number input from the user and
print its sum of the square of the digits

num = as.integer(readline(prompt = "Enter a number: "))


digit = 0
sum = 0

if (num >= 0) {
while (num > 0) {
digit = num %% 10
sum = sum + digit^2
num = num %/% 10
}
cat("The sum of the squares of digits is:", sum, "\n")
} else {
cat("Please enter a valid non-negative integer.\n")
}
32
33

You might also like