0% found this document useful (0 votes)
2 views5 pages

C Programming Assignment2

This document is an assignment for a computer programming course focused on C language. It consists of three parts: writing programs using loops, finding and correcting errors in provided code snippets, and predicting outputs of given code. The assignment is structured with specific questions and instructions for each part.

Uploaded by

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

C Programming Assignment2

This document is an assignment for a computer programming course focused on C language. It consists of three parts: writing programs using loops, finding and correcting errors in provided code snippets, and predicting outputs of given code. The assignment is structured with specific questions and instructions for each part.

Uploaded by

Ali Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 2

Computer Programming

Name: _______________________________

Roll Number: ________________________

Section: ____________________________

Date: _______________________________

Instructions:
- Attempt all questions
- Use C language only
- Write answers neatly

Part A: Write Programs


Q1. Write a C program using a for loop to print numbers from 1 to 20.

Q2. Write a C program using a while loop to calculate the sum of first 10 natural numbers.

Q3. Write a C program using a do-while loop to print numbers from 10 to 1.

Lecturer: Ms. Marium Ehtesham


Q4. Write a C program using nested loops to print the pattern:
*

**

***

****

*****

Q5. Write a C program using nested loops to print the following number pattern (Take size
from user. Example if user enters 4):
1234
1234
1234
1234

Part B: Find the Errors and Rewrite it correctly.


Q6.
#include<stdio.h>
int main()
{
int i
for(i=1; i<=10; i--)
{
print("%d ", i);
}
return 0
}

Q7.
include<stdio.h>
int main()
{
int i = 1;
while(i <= 5);
{
printf("%d ", i);
j++;
}
return 0;
}

Lecturer: Ms. Marium Ehtesham


Q8.
#include<stdio.h>
int main()
{
int i = 1;
do
{
print("%d ", i);
i++;
}
while(m <= 5)
return 0;
}

Q9.
#include<std.h>
int main()
{
int i, j;
for(i=1; i<=3; i++)
{
for(j=8; j<=3; i++)
{
printf("* );
}
printf("\n");
}
return 0;
}

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

for(int i=1; i<=10; j++)


{
printf("d x %d = %d\n", num, i, num*i)
}

Lecturer: Ms. Marium Ehtesham


return 0;
}

Part C: Find the Output (10 Marks)


Q11.
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=5; i++)
printf("%d ", i);
return 0;
}

Output:________________________________

Q12.
#include<stdio.h>
int main()
{
int i=1;
while(i<=3)
{
printf("Hello ");
i++;
}
return 0;
}

Output:________________________________

Q13.
#include<stdio.h>
int main()
{
int i=5;
do
{
printf("%d ", i);

Lecturer: Ms. Marium Ehtesham


i--;
} while(i>=1);
return 0;
}

Output:________________________________

Q14.
#include<stdio.h>
int main()
{
int i, j;
for(i=1; i<=2; i++)
{
for(j=1; j<=3; j++)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Output: ________________________________

Q15.
#include<stdio.h>
int main()
{
int i, sum=0;
for(i=1; i<=4; i++)
{
sum = sum + i;
}
printf("%d", sum);
return 0;
}

Output:________________________________

Lecturer: Ms. Marium Ehtesham

You might also like