C First Assignment Comp
C First Assignment Comp
2. To input any 4-digit number and find the sum of first and last numbers
Ans:
#include<stdio.h>
main(){
int number,num1,num4,sum;
printf("Enter 4-digit number:");
scanf("%d",&number);
num4=number%10;
num1=number/1000;
sum=num1+num4;
printf("the sum of first and last number of four digit number is %d",sum);
}
3. To input the marks of five subjects and calculate the percentage and give the grade as below:
>= 80 distinction, >=65 first division, >=50 second division >= 40 pass, < 40 fail
Ans:
#include<stdio.h>
main(){
double m1,m2,m3,m4,m5,total;
double percent;
printf(" enter the marks in between 0 to 100");
printf("enter the marks of five subject:");
scanf("%lf %lf %lf %lf %lf",&m1,&m2,&m3,&m4,&m5);
total=m1+m2+m3+m4+m5;
percent=(total/500)*100;
if(percent>=80 && percent<=100){
printf("You obtained %.2lf you got distinction", percent);
}
else if(percent>=65 && percent<80){
printf("You obtained %.2lf you got first division",percent);
}
else if(percent>=50 && percent<65){
printf("You obtained %.2lf you got second division", percent);
}
else if(percent>=40 && percent<50){
printf("You obtained %.2lf you got pass division" ,percent);
}
else{
printf("You obtained %.2lf you have failed.", percent);
}
5. NTC charges the following rate for local calls from a customer Calls charges
Find the amount paid by the customer, if the total number of calls is given by the user
Ans:
#include<stdio.h>
main(){
int num,charge,addtional_charge,extra_calls;
double TSC,Vat,Total_amount;
scanf("%d",&num);
if(num<=175){
charge=200;
}
else {
extra_calls=num-175;
addtional_charge=extra_calls*1;
charge=200+addtional_charge;
TSC=0.1*charge;
Vat=0.13*(TSC+charge);
Total_amount=TSC+Vat+charge;
8. To find the sum of digits from 1 to 100 which are exactly divisible by the given number
Ans:
#include<stdio.h>
main(){
int num,i,sum=0;
printf("enter the number:");
scanf("%d",&num);
for(i=1;i<=100;++i){
if(i%num==0){
sum=sum+i;
}
}
printf("The sum of digit from 1 to 100 which are exactly divisible by the given number is
%d",sum);
}
9. To input 3 digits number and find out whether the given number is Armstrong number or not.
E.g., 153 = (1*1*1) +(5*5*5) +(3*3*3)
Ans:
#include<stdio.h>
main(){
int num,rem,num1,sum=0;
printf("enter the number:");
scanf("%d",&num);
num1=num;
while(num!=0){
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if (sum==num1){
printf("the number is armstrong number");
}
else{
printf("the number is not armstrong number");
}
}
10. To print the prime number between 1 to 100
Ans:
#include<stdio.h>
main(){
int i,j,count;
for(i=2;i<=100;++i){
count=0;
for(j=1;j<=100;++j){
if(i%j==0){
count++;
}
}
if(count<=2){
printf("%d\n",i);
}
}
11. To print the pattern given below
12
123
1234
12345
Ans:
#include<stdio.h>
main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%d",j);
printf("\n");