Full Programs
Full Programs
#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);
}
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);
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
#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
#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
#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
#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
#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
#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.
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.
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.
while (n >= 1) {
s=s+n
n=n-1
}
Q4) Write a program to take two numbers input from the user
and print the maximum number from them
Q5) Write a program to take a number input from the user and
check whether a given number is prime number or not.
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
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