Professional Documents
Culture Documents
C Assingment
C Assingment
C Assingment
Que-1 Implement a program in GNU C to display the message “Hello World!” on screen
Code:
#include<stdio.h>
int main(){
printf("Hello World!");
return 0;
}
Output:
Hello World!
Que-2 Implement a program in GNU C where the user enters an integer from keyboard at run-time. The
entered integer should be displayed on screen.
Code:
#include<stdio.h>
int main(){
int num;
printf("Enter the number :");
scanf("%d",&num);
printf("the number is %d.",num);
return 0;
}
Output:
Enter the number :08
the number is 8.
Que-3 Implement a program in GNU C where the user enters two integers from keyboard at run-time. The sum
of both the integers should be displayed on screen.
Code :
#include<stdio.h>
int main(){
int num1,num2;
printf("\nEnter number-1 here :");
scanf("%d",&num1);
printf("\nEnter number-2 here :");
scanf("%d",&num2);
int num=num1+num2;
printf("\nthe sum of %d and %d is %d.",num1,num2,num);
return 0;
}
Output:
Enter number-1 here :2
Enter number-2 here :3
the sum of 2 and 3 is 5.
Que-4 Implement a program in GNU C where the user enters two integers from keyboard at run-time. The
sum, difference and product of both the integers should be displayed on screen.
Code :
#include<stdio.h>
int main(){
int num1,num2;
printf("\nEnter number-1 here :");
scanf("%d",&num1);
printf("\nEnter number-2 here :");
scanf("%d",&num2);
int sum=num1+num2;
int sub = num1-num2;
int multi = num1*num2;
printf("\nthe sum of %d and %d is %d.",num1,num2,sum);
printf("\nthe sum of %d and %d is %d.",num1,num2,sub);
printf("\nthe sum of %d and %d is %d.",num1,num2,multi);
return 0;
}
Output:
Enter number-1 here :4
Enter number-2 here :3
the sum of 4 and 3 is 7.
the sum of 4 and 3 is 1.
the sum of 4 and 3 is 12.
Que-3 Implement a program in GNU C where the user enters a floating point number (decimal number) from
keyboard at run-time. The entered number should be displayed on screen.
Code:
#include<stdio.h>
int main(){
float num;
printf("Enter floating number here :");
scanf("%f",&num);
printf("the floating number is %f.",num);
return 0;
}
Output:
Enter floating number here :4
the floating number is 4.000000.
Que-6 Implement a program in GNU C where the user enters two floating point numbers from keyboard at
run-time. The sum, difference and product of both the numbers should be displayed on screen.
Code:
#include<stdio.h>
int main(){
float num1,num2;
printf("\nEnter number-1 here :");
scanf("%f",&num1);
printf("\nEnter number-2 here :");
scanf("%f",&num2);
float sum=num1+num2;
float sub = num1-num2;
float multi = num1*num2;
printf("\nthe sum of %f and %f is %f.",num1,num2,sum);
printf("\nthe sum of %f and %f is %f.",num1,num2,sub);
printf("\nthe sum of %f and %f is %f.",num1,num2,multi);
return 0;
}
Output:
Enter number-1 here :4
Enter number-2 here :2
the sum of 4.000000 and 2.000000 is 6.000000.
the sum of 4.000000 and 2.000000 is 2.000000.
the sum of 4.000000 and 2.000000 is 8.000000.
Que-7 Implement a program in GNU C where the user enters a character from keyboard at runtime. The
entered character and its corresponding ASCII value (in decimal) should be displayed on screen.
Code:
#include<stdio.h>
int main(){
char str;
printf("Enter character here :");
scanf("%c",&str);
printf("the floating number is %d.",str);
return 0;
}
Output:
Enter character here :A
the floating number is 65.
Que-8 Implement a program in GNU C where the user enters the radius of a circle from keyboard at run-time.
The circumference and area of the circle should be displayed on screen. DO NOT USE any library function of
math.h for this program.
Code:
#include<stdio.h>
int main(){
int r,area,circumference;
printf("Enter radius here :");
scanf("%d",&r);
area=3.14*(r*r);
circumference=2*3.14*r;
printf("the Area of circle is %d.\n",area);
printf("the circumference of circle is %d.\n",circumference);
return 0;
}
Output:
Enter radius here :4
the Area of circle is 50.
the circumference of circle is 25.
Que-9 Implement a program in GNU C where the user enters the length of a side of a square from keyboard at
run-time. The perimeter and area of the circle should be displayed on screen. Make use of pow() library
function of math.h for this program.
Code:
#include<stdio.h>
#include<math.h>
int main(){
int s,area,circumference;
printf("Enter side of a square here :");
scanf("%d",&s);
area=pow(s,2);
circumference=4*s;
printf("the Area of square is %d.\n",area);
printf("the circumference of square is %d.\n",circumference);
return 0;
}
Output:
Enter side of a square here :2
the Area of square is 4.
the circumference of square is 8.
Que-10 Implement a program in GNU C where the user enters the temperature in celcius unit from keyboard
at run-time. The corresponding temperature in Fahrenheit unit should be displayed on screen.
Code:
#include<stdio.h>
int main(){
int c,f;
printf("Enter the temperature in celcius :");
scanf("%d",&c);
f=(c*(9/5))+32;
printf("Fahrenheit unit is %d.",f);
return 0;
}
Output:
Enter the temperature in celcius :0
Fahrenheit unit is 32.
Que-11 Implement a program in GNU C where the user enters the temperature in Fahrenheit unit from
keyboard at run-time. The corresponding temperature in Celcius unit should be displayed on screen.
Code:
#include<stdio.h>
int main(){
int c,f;
printf("Enter the temperature in Fahrenheit :");
scanf("%d",&f);
c=(f-32)*(9/5);
printf("Celcius unit is %d.",c);
return 0;
}
Output:
Enter the temperature in Fahrenheit :32
Celcius unit is 0.
Que-12 Implement a program in GNU C where the user enters the weight in kilograms unit from keyboard at
run-time. The corresponding weight in grams unit should be displayed on screen.
Code:
#include<stdio.h>
int main(){
int kg;
scanf("%d",&kg);
return 0;
Output:
Que-13 Implement a program in GNU C where the user enters the weight in grams unit from keyboard at run-
time. The corresponding weight in kilograms unit should be displayed on screen.
Code :
#include<stdio.h>
int main(){
float gram;
scanf("%f",&gram);
return 0;
Output:
Que-14 Implement a program in GNU C to swap the values of two variables. The value of both the variables
before swapping and after swapping should be displayed on screen.
Code :
#include<stdio.h>
int main(){
int num1,num2;
scanf("%d",&num1);
scanf("%d",&num2);
return 0;
Output:
Que-15 Implement a program in GNU C where the user enters two integers a and b from keyboard at run-time.
Accordingly, the message “a is greater than b”, “a is less than b” or “a is equal to b” should be displayed on
screen.
Code:
#include<stdio.h>
int main(){
int a,b;
scanf("%d",&a);
scanf("%d",&b);
if(a==b){
}else{
}
return 0;
Output:
a greater than b
Que-16 Implement a program in GNU C where the user enters an integer from keyboard at run-time.
Accordingly, the message “ The entered integer is odd” or “The entered integer is even” should get displayed
on screen.
Code:
#include<stdio.h>
int main(){
int a;
scanf("%d",&a);
if(a%2==0){
}else{
}
return 0;
Output:
a is odd number !
Que-17 Implement a program in GNU C where the user enters the percentage result obtained from keyboard
at run-time. The corresponding grade should be displayed on screen. Assume that the rules of grading are as
follows: for % =40 and <=49, grade is PASS, for % >=50 and <=59 grade is SECOND, for % >=60 and <=69 grade is
FIRST, for % >=70 grade is DISTINCTION. Make use of if /if-else/else constructs for above program.
Code:
#include<stdio.h>
int main(){
int percentage;
scanf("%d",&percentage);
}else{
printf("DISTNCTION !\n");
}
return 0;
Output:
grade is SECOND !
Code :
#include <stdio.h>
int main() {
int percentage;
printf("Enter percentage here: ");
scanf("%d", &percentage);
switch (percentage/10) {
case 4:
printf("Grade is PASS!\n");
break;
case 5:
printf("Grade is SECOND!\n");
break;
case 6:
printf("Grade is FIRST!\n");
break;
default:
printf("DISTINCTION!\n");
break;
}
return 0;
}
Output:
Enter percentage here: 45
Grade is PASS!
Que-19 Implement a program in GNU C where the user enters three integers from keyboard at runtime. The
greatest of all the three numbers should be displayed on screen. DO NOT USE ARRAYS.
Code:
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d",&num2);
scanf("%d",&num3);
greatest = num1;
greatest = num2;
}
greatest = num3;
}
return 0;
Output:
Que-20 Implement a program in GNU C where the user enters an alphabetic character from keyboard at run-
time. The program should classify the entered alphabet as “VOWEL” or “CONSONANT”
Code:
#include<stdio.h>
int main(){
char str;
scanf("%c",&str);
}else{
}
return 0;
Output:
Code:
#include<stdio.h>
int main(){
char str;
scanf("%c",&str);
}else{
}
return 0;
Output:
Que-22 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should extract the last digit of the number and display its value.
Code:
#include<stdio.h>
int main(){
int num,result;
scanf("%d",&num);
result=num%10;
return 0;
}
Output:
Que-23 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should display the total number of digits in that number. Eg. If 23 is entered, the output of the
program should be 2. Assume that no leading zeroes are entered while entering the integer.
Code:
#include<stdio.h>
int main(){
int num,result,i=0;
scanf("%d",&num);
while(num>0){
result=num%10;
num=num/10;
i++;
}
return 0;
Output:
Que-24 Implement a program in GNU C to print the message “Hello World” 20 times, without replicating the
code.
Code:
#include<stdio.h>
int main(){
for(int i=1;i<=15;i++){
printf("%d-Hello World!\n",i);
}
return 0;
}
Output:
1-Hello World!
2-Hello World!
3-Hello World!
4-Hello World!
5-Hello World!
6-Hello World!
7-Hello World!
8-Hello World!
9-Hello World!
10-Hello World!
11-Hello World!
12-Hello World!
13-Hello World!
14-Hello World!
15-Hello World!
Que-25 Implement a program in GNU C where the user enters a natural number from keyboard at run-time.
The program should display the SIGMA or sum of integers starting from 1 up to the given number.
Code:
#include<stdio.h>
int main(){
int num,sum=0;
scanf("%d",&num);
for(int i=1;i<=num;i++){
sum=sum+i;
}
return 0;
Output:
Code:
#include<stdio.h>
int main(){
int num,sum=1;
scanf("%d",&num);
for(int i=1;i<=num;i++){
sum=sum*i;
}
return 0;
Output:
Factorial of 3 is 6.
Que-27 Implement a program in GNU C where the user enters a natural number from keyboard at run-time.
The program should classify the number as a prime, composite or a special number.
Code:
#include<stdio.h>
int main(){
int num ;
scanf("%d",&num);
if(num==1){
}else if(num==2){
else{
for(int i=2;i<num;i++){
if(num%i==0){
break;
}else{
break;
return 0;
Output:
special number !
Que-28 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should display the sum of digits in that number. Eg. If 23 is entered, the output of the program
should be 5.
Code:
#include<stdio.h>
int main() {
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0){
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
Output:
Enter a number:3456
Sum is=18
Que-29 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should display the reversed number. Eg. If 23 is entered, the output of the program should be 32.
For single digit number, the output should remain same as the original number entered.
Code:
#include <stdio.h>
int main() {
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
n /= 10;
}
return 0;
Output:
Enter an integer: 54
Reversed number = 45
Que-30 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display the GCD of the two integers.
Code:
#include <stdio.h>
int main()
gcd = i;
}
return 0;
Output:
G.C.D of 4 and 5 is 1
Que-31 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display the LCM of the two integers
Code:
#include<stdio.h>
int main(){
int num1,num2;
scanf("%d",&num1);
scanf("%d",&num2);
if(num1>num2){
if(num1%num2==0){
printf("%d",num1);
}else{
int num=num1*i;
if(num%num2==0){
break;
}
}
}
}else{
if(num2%num1==0){
printf("%d",num2);
}else{
int num=num2*i;
if(num%num1==0){
break;
}
}
}
}
return 0;
output:
Que-32 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display whether the first integer is exactly divisible by the second integer.
Code:
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
if (num1 % num2 == 0) {
}
else {
}
return 0;
Output:
4 is exactly divisible by 2.
Que-33 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display whether the first integer is a factor of the second integer
Code:
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
if (num1 % num2 == 0) {
}
else {
}
return 0;
Output:
2 is Factor of 4.
Que-34 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display whether the first integer is a multiple of the second integer.
Code:
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
if (num2 % num1 == 0) {
}
else {
}
return 0;
Output:
2 is multiple of 4.
Que-35 Implement a program in GNU C where the user enters a decimal integer from keyboard at run-time.
The program should display the equivalent binary, octal and hexadecimal value of the entered number.
Code:
#include<stdio.h>
int remainder,b[15],i,j,rev=0,a=1;
while(num){
remainder=num%2;
rev=rev+remainder*a;
a=a*10;
num=num/2;
b[i]=remainder;
i++;
}
printf("Binary value =%d.\n",rev);
int remainder,O[15],i,rev=0,a=1;
while(num){
remainder=num%8;
rev=rev+remainder*a;
a=a*10;
num=num/8;
O[i]=remainder;
i++;
int remainder,hex_num,i=0,rev=0,a=1;
char h[50];
while(num){
remainder=num%16;
rev=rev+remainder*a;
a=a*10;
if(remainder<10){
remainder=remainder+48;
i++;
}else{
remainder=remainder+55;
i++;
h[i++]=remainder;
num=num/16;
int main(){
int num;
scanf("%d",&num);
Binary(num);
Octal(num);
Hexa_decimal(num);
return 0;
Output:
Que-36 Implement a program in GNU C where the user enters a binary integer from keyboard at run-time.
The program should display the equivalent decimal, octal and hexadecimal value of the entered number.
Code:
#include <stdio.h>
#include <stdlib.h>
int remainder,hex_num,i=0,rev=0,a=1;
char h[50];
while(num){
remainder=num%16;
rev=rev+remainder*a;
a=a*10;
if(remainder<10){
remainder=remainder+48;
i++;
}else{
remainder=remainder+55;
i++;
h[i++]=remainder;
num=num/16;
printf("%c", h[j]);
int remainder,O[15],i,rev=0,a=1;
while(num){
remainder=num%8;
rev=rev+remainder*a;
a=a*10;
num=num/8;
O[i]=remainder;
i++;
int main() {
scanf("%lld", &binaryNumber);
while (binaryNumber != 0) {
binaryNumber /= 10;
++i;
Hexa_decimal(decimalNumber);
return 0;
Output:
Decimal equivalent: 27
Que-37 Implement a program in GNU C where the user enters a hexadecimal integer from keyboard at run-
time. The program should display the equivalent binary, octal and decimal value of the entered number.
Code:
#include <stdio.h>
#include <stdlib.h>
int remainder,b[15],i,j,rev=0,a=1;
while(num){
remainder=num%2;
rev=rev+remainder*a;
a=a*10;
num=num/2;
b[i]=remainder;
i++;
int remainder,O[15],i,rev=0,a=1;
while(num){
remainder=num%8;
rev=rev+remainder*a;
a=a*10;
num=num/8;
O[i]=remainder;
i++;
int main() {
char hex[20];
int i = 0, j = 0;
scanf("%s", hex);
i++;
Binary(decimal);
Octal(decimal);
return 0;
Output:
Decimal equivalent: 27
Que-38 Implement a program where the user is supposed to enter an even integer. Till the user doesn’t enter
an even integer, the program continuously prompts the user that the entered number is incorrect and to kindly
enter an even integer.
Code:
#include<stdio.h>
int main(){
int num;
while(1){
scanf("%d",&num);
if(num%2==0){
break;
}else{
}
}
return 0;
Output:
Even integer !
Que-39 Implement a program where the user is supposed to enter an even integer. Till the user doesn’t enter
an even integer, the program continuously prompts the user that the entered number is incorrect and to kindly
enter an even integer upto 4 times. After 4th consecutive failure to enter the correct integer, the program
terminates.
Code:
#include<stdio.h>
int main(){
int num,i=0;
while(1){
scanf("%d",&num);
if(num%2==0){
break;
}else{
i++;
}
if(i>4){
break;
}
}
return 0;
Output:
Que-40 Implement a program where the user is supposed to enter 5 odd integers. The program should display
the sum of all the entered odd integers. In case, the user enters an even integer, then the entered even integer
SHOULD NOT BE COUNTED while calculating the sum.
Code:
#include<stdio.h>
int main(){
int num,sum=0;
for(int i=0;i<5;i++){
scanf("%d",&num);
if(num%2!=0){
sum=sum+num;
}
}
return 0;
Output:
Que-41 Implement a program where the user is supposed to enter 5 odd integers. The program should display
the sum of all the entered odd integers. In case, the user enters an even integer, then the program displays the
sum of the entered odd integers up to the last odd integer entered and terminates.
Code:
#include<stdio.h>
int main(){
int num,sum=0;
for(int i=0;i<5;i++){
if(num%2!=0){
sum=sum+num;
}else{
break;
}
}
return 0;
Output:
Que-42 Implement a program which displays the following menu when the program is run. 1-ACCOUNT
OPENING 2-DEPOSIT 3-WITHDRAW 4-BALANCE ENQUIRY 5-ACCOUNT CLOSING 6-ACCOUNT DEACTIVATE
Depending upon the number entered by the user, the corresponding message will be displayed. Eg. If the user
enters 4, the message “BALANCE ENQUIRY” will be displayed.
Code:
#include <stdio.h>
int main() {
int choice;
printf("MENU\n");
printf("1-ACCOUNT OPENING\n");
printf("2-DEPOSIT\n");
printf("3-WITHDRAW\n");
printf("4-BALANCE ENQUIRY\n");
printf("5-ACCOUNT CLOSING\n");
printf("6-ACCOUNT DEACTIVATE\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("ACCOUNT OPENING\n");
break;
case 2:
printf("DEPOSIT\n");
break;
case 3:
printf("WITHDRAW\n");
break;
case 4:
printf("BALANCE ENQUIRY\n");
break;
case 5:
printf("ACCOUNT CLOSING\n");
break;
case 6:
printf("ACCOUNT DEACTIVATE\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
Output:
MENU
1-ACCOUNT OPENING
2-DEPOSIT
3-WITHDRAW
4-BALANCE ENQUIRY
5-ACCOUNT CLOSING
6-ACCOUNT DEACTIVATE
Enter your choice: 4
BALANCE ENQUIRY