C PROGRAMMING ASSIGNMENT SOLVED
Question1
#include<stdlib.h>
int main()
{
printf(“\t\Twelcome to c
programming\n”);
return 0;
}
Output
Question 2
#include<stdlib.h>
int main()
{
Int sum,a,b;
printf(“enter the two numbers to be
added respectively\n);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“sum=%d”);
return 0;
}
Output;
Question 3
#include<stdlib.h>
int main()
{
int a,b,product;
float quotient;
printf(“enter the two numbers
respectively\n”);
Scanf(“%d%d”,&a,&b);
product=a*b;
quotient=a/b;
printf(“quotient=%f and
product=%d”,quotient,product);
return 0
}
Out put
Question 4
#include<stdlib.h>
int main(){
Int a;
printf(“enter the number in question\n”);
scanf(“%d”,&a);
if(a%2==0){
printf(“%d is even\n”);}
else if(a/2==1){
printf(“%d is even\n”);
}
return 0;
}
Output
Question 5
#include
<stdio.h>
#include
<conio.h>
int main()
{
int x,y,n;
printf("Enter the number.");
scanf("%d",&x);
y=1;
n=0;
while(y<=x)
{
if((x%y)==0)
{n++;
if((n==2)||(x==1))
{
if(y!=x)
printf("%d is not a prime number",x);
else printf("%d
is a prime number",x);
break;
}
}
y++;
}
return 0;
Output
Question 6
#include
<stdio.h>
#include
<conio.h>
int main()
{
printf("Enter the character(A-Z):");
char letter;
scanf("%c",&letter);
if(letter=='a'||letter=='e'||letter=='i'||letter=='o'||letter=='u'||
letter=='A'||letter=='E'||letter=='I'||letter=='0'||letter=='U')
{
printf("%c is a vowel",letter);
}
else
{
printf("%c is a consonant",letter);
}
return 0;
Output
Question7
#include
<stdio.h>
#include
<conio.h>
int main()
{
int principle,time;
int rate,interest;
printf("enter the principle");
scanf("%d",&principle);
printf("enter the rate\n");
scanf("%f",&rate);
printf("enter the time\n");
scanf("%d",&time);
interest=(principle*rate*time)/100;
printf("simple interest is= %d*%d*%d:%d",interest);
return 0;
}
output
Question 8
#include<stdio.h>
int
main(){
int n,q,result=0,rem;
printf("enter a number:");
scanf("%d",&n);
q=n;
while(q!=0){
rem=q%10;
result=result*10+rem;
q=q/10;
}
if(result==n)
printf("%d is a palindrome",n);
else
printf("%d is not a palindrome",n);
return 0;
}
Output
1
Output
2
QUESTION 9
int main () {
char
word[100];
printf("Enter
a word or sentence: ");
scanf("%s",&word);
printf("\nThe
revere is: %s",strrev(word));
return
0;
}
Output
Question20
#include
<stdio.h>
#include
<conio.h>
int main()
{
int math,english,sst,scie,average,sum;
char grade;
printf("ENTER THE STUDENTS MARKS\n");
printf("ENTER english mark\n");
scanf("%d",&english);
printf("ENTER math mark\n");
scanf("%d",&math);
printf("ENTER sst mark\n");
scanf("%d",&sst);
printf("ENTER scie mark\n");
scanf("%d",&scie);
sum=english+math+sst+scie;
average=sum/4;
printf("average =%d",average);
switch(average/10)
{
case 9:
{
printf("grade= A\n ");
break;
}
case 8:
{
grade='B';
printf("grade= B\n");
break;
}
case 7:
{
printf("grade= C\n");
break;
}
case 6:
{
printf("grade= D\n");
break;
}
case 5:
{
printf("grade= E\n");
break;
}
default:
{
printf("invalid entry fail\n");
}
break;
}
return 0;
}
Output
Question 21
#include
<stdio.h>
#include
<conio.h>
#include
<math.h>
int main()
{
float radius,area,circumference;
float pi=M_PI;
printf("enter radius\n");
scanf("%f",&radius);
circumference=2*pi*radius;
printf("circumference=%.3f\n",circumference);
area=pi*radius*radius;
printf("area =%.3f\n",area);
return 0;}
Output
Question 22
#include
<stdio.h>
#include
<conio.h>
int main()
{
int x,y,*a,*b,temp;
printf("enter the two numbers to be swapped\n");
scanf("%d%d",&x,&y);
printf("before swapping x=%d and y=%c\n",x,y);
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
printf("after swapping X=%d and y=%d\n",x,y);
return 0;
}
Output
Question 23
#include
<stdio.h>
#include
<conio.h>
int main()
{
int n,a[10],i;
printf("enter the decimal number to be converted:\n"); scanf("%d",&n);
printf ("\tthe number is:%d\n",n);
for(i=0; n>0; i=i+1)
{
a[i]=n%2;
n=n/2;
}
for(i=i-1; i>=0; i=i-1)
{
printf("%d",a[i]);
}
return 0;
}
Output
Question 24
#include
<stdio.h>
#include
<conio.h>
int main()
{
struct person
{
char Name[50];
int citizenshipNo;
int payment;
};
struct person emp1;
strcpy(emp1.Name,"Erick");
emp1.citizenshipNo=58574;
emp1.payment=985640000;
printf("Name=%s\n",emp1.Name);
printf("citizenshipNo=%d\n",emp1.citizenshipNo);
printf("payment=%d\n",emp1.payment);
return 0;
}
Output
Question 26
#include
<stdio.h>
#include
<conio.h>
int main()
{
int a,b,c,j;
float x1,x2;
printf("Enter the coefficients of the equation\n"); scanf("%d%d%d",&a,&b,&c);
j=pow(b,2)-4*a*c;
x1=(float)(-b+(sqrt(j)))/2*a;
x2=(float)(-b-(sqrt(j)))/2*a;
printf("x1=%.2f and and x2=%.2f",x1,x2);
return 0;
}
Output
#include<stdio.h>
int main() {
int i;
printf("Enter information of students:\n");
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output:
Enter information of students:
For roll number1,
Enter first name: Opio
Enter marks: 90
For roll number2,
Enter first name: Okello
Enter marks: 98
For roll number3,
Enter first name: Ouma
Enter marks: 98
For roll number4,
Enter first name: Otim
Enter marks: 89
For roll number5,
Enter first name: Olele
Enter marks: 95
Displaying Information:
Roll number: 1
First name: Opio
Marks: 90.0
Roll number: 2
First name: Okello
Marks: 98.0
Roll number: 3
First name: Ouma
Marks: 98.0
Roll number: 4
First name: Otim
Marks: 89.0
Roll number: 5
First name: Olele
Marks: 95.0
Question 26
//To read details of electricity charges
#include <stdio.h>
int main()
{
char name[100];
float unit_used, amount;
printf("Enter the user name: ");
scanf("%s", &name);
printf("Enter unit Consumed: ");
scanf("%f",&unit_used);
if(unit_used<=200)
amount=unit_used*.80;
else if(unit_used<=300)
amount=(unit_used-200)*0.90+160;
else
if(unit_used>300)
amount=(unit_used-300)*1+250;
if(amount>=400)
amount=amount+amount*0.15;
printf("Name: %s\n", name );
printf("Charge: %5.2f",amount);
return 0;
}
Output:
Enter the user name: Opio
Enter unit Consumed: 168
Name:
Opio
Charge:
134.40
Predefined
function for all
include<stdio.h>
#include<math.h>
#include<string.h>
Question 1
#include
<stdio.h>
#include
<conio.h>
int main()
{
int a,b,c,sum; float average;
printf("enter the three numbers\n"); scanf("%d%d%d",&a,&b,&c);
sum=a+b+c; average=sum/3; printf("SUM=%d\tAVERAGE=%.3f\n",sum,average);
return 0;
}
Output
Question2
#include
<stdio.h>
#include
<conio.h>
int main()
{
int a,b,c; float x,g;
printf("enter the coefficients\n");
scanf("%d%d%d",&a,&b,&c);
g=((pow(b,2)-(4*a*c))/2*a);
x=-b+sqrt(g);
printf("x=%.3f",x);
return 0;
}
Output
Please download the full work from the download page.
CHRISTOPHER A TITUS - Kyambogo University BITC
NB: some of the output are not includede such that you try to write and compile the programs to see its effect and output.